我有一个很大的SVG图像,希望它能在不同的场景下进行良好的缩放(请参见代码片段)。
最初,我使用svg的width
和height
属性,并在屏幕大小调整时设置这些值。
然后,我发现了viewBox。
我不明白的是,如果没有svg的宽度和高度属性,我该将viewBox的宽度和高度参数设置为什么。
我有这个SVG文档:
document.addEventListener("DOMContentLoaded",function(){
var container = document.querySelector('.chart');
container.addEventListener('click', popOut, false)
});
function popOut() {
var container = document.querySelector('.chart');
if (container.className.split(/\s+/).indexOf("popper") === -1) {
container.classList.add('popper');
}
var svg = document.querySelector('svg');
}
html {
min-height: 100%;
height: 100%;
margin: 0;
}
div {
border: 1px solid gray;
}
body {
height: 100%;
font-size: 20px;
}
path {
stroke: #000;
}
.container {
display: flex;
flex-direction: column;
flex: 1;
height: 100%;
}
.container>div {
flex: 1;
}
.row {
display: flex;
}
.item {
flex: 1;
padding: 8px;
}
.chart {
display: inline-block;
position: relative;
width: 100%;
height: 100%;
vertical-align: top;
overflow: hidden;
"
}
.popper {
z-index: 1000 !important;
width: 100% !important;
height: 100% !important;
position: fixed !important;
top: 0 !important;
left: 0;
background-color: #fff;
}
<html>
<body>
<div class="container">
<div class="item">Div One</div>
<div class="row">
<div class="item chart">
<button type="button" onClick="window.popOut()">
Pop Out
</button>
<svg preserveAspectRatio="xMinYMin meet" viewBox="0 0 1200 800"><g class="vx-group vx-tree" transform="translate(30, 37)"><g transform="matrix(1,0,0,1,0,0)"><g class="vx-group" transform="translate(0, 0)"><path class="vx-link-vertical link__node" d="M189.27272727272728,353.5C189.27272727272728,176.75,567.8181818181819,176.75,567.8181818181819,0" percent="20.5" stroke-width="1" stroke-opacity="0.2" fill="none"></path><path class="vx-link-vertical link__node" d="M567.8181818181819,353.5C567.8181818181819,176.75,567.8181818181819,176.75,567.8181818181819,0" percent="20.5" stroke-width="1" stroke-opacity="0.2" fill="none"></path><path class="vx-link-vertical link__node" d="M946.3636363636364,353.5C946.3636363636364,176.75,567.8181818181819,176.75,567.8181818181819,0" percent="20.5" stroke-width="1" stroke-opacity="0.2" fill="none"></path><path class="vx-link-vertical link__node" d="M189.27272727272728,353.5C189.27272727272728,530.25,94.63636363636364,530.25,94.63636363636364,707" percent="20.5" stroke-width="1" stroke-opacity="0.2" fill="none"></path><path class="vx-link-vertical link__node" d="M189.27272727272728,353.5C189.27272727272728,530.25,283.90909090909093,530.25,283.90909090909093,707" percent="20.5" stroke-width="1" stroke-opacity="0.2" fill="none"></path></g><g class="vx-group" transform="translate(0, 0)"><g class="vx-group node__0" transform="translate(567.8181818181819, 0)" opacity="1"><g transform="matrix(1,0,0,1,0,0)" class="node__container"><svg class="node__inactive" x="0" y="0" style="overflow: visible;"><polygon points="25.98076211353316,-14.999999999999998 25.98076211353316,14.999999999999998 1.83697019872103e-15,30 -25.98076211353316,14.999999999999998 -25.980762113533157,-15.000000000000004 -5.510910596163089e-15,-30" class="node__hexagon undefined"></polygon></svg>
<g class="node__plan undefined node__inactive">
<use xlink:href="#icon-Plan"></use>
</g>
<g><svg x="0" y="0" style="overflow: visible;"><text class="label-down__item__name" width="150" y="48.63100051879883" x="0" text-anchor="middle" style="pointer-events: none;"><tspan x="0" dy="0em">Finance BCP</tspan></text></svg><svg x="0" y="0"
style="overflow: visible;"><text class="label-down__item__type" width="150" y="68.63100051879883" x="0" text-anchor="middle" style="pointer-events: none;"><tspan x="0" dy="0.71em">Plan</tspan></text></svg></g>
</g>
</g>
<g class="vx-group node__1" transform="translate(189.27272727272728, 353.5)" opacity="1">
<g transform="matrix(1,0,0,1,0,0)" class="node__container"><svg class="" x="0" y="0" style="overflow: visible;"><polygon points="25.98076211353316,-14.999999999999998 25.98076211353316,14.999999999999998 1.83697019872103e-15,30 -25.98076211353316,14.999999999999998 -25.980762113533157,-15.000000000000004 -5.510910596163089e-15,-30" class="node__hexagon node__active"></polygon></svg>
<g class="node__service node__active">
<use xlink:href="#icon-Service"></use>
</g>
<g><svg x="0" y="0" style="overflow: visible;"><text class="label-down__item__name" width="150" y="48.92399978637695" x="0" text-anchor="middle" style="pointer-events: none;"><tspan x="0" dy="0em">Accounts Receivable</tspan></text></svg>
<svg x="0" y="0" style="overflow: visible;"><text class="label-down__item__type" width="150" y="68.92399978637695" x="0" text-anchor="middle" style="pointer-events: none;"><tspan x="0" dy="0.71em">Service</tspan></text></svg>
</g><svg class="zoomout__container" height="20" width="20" viewBox="15 55 30 20"><g><circle class="node__shape-fill" cx="15" cy="10" r="10" stroke-width="2"></circle><line class="node__shape-fill" x1="8.5" y1="10" x2="21" y2="10" stroke-width="2"></line></g></svg></g>
</g>
<g class="vx-group node__2" transform="translate(567.8181818181819, 353.5)" opacity="1">
<g transform="matrix(1,0,0,1,0,0)" class="node__container"><svg class="node__inactive" x="0" y="0" style="overflow: visible;"><polygon points="25.98076211353316,-14.999999999999998 25.98076211353316,14.999999999999998 1.83697019872103e-15,30 -25.98076211353316,14.999999999999998 -25.980762113533157,-15.000000000000004 -5.510910596163089e-15,-30" class="node__hexagon"></polygon></svg>
<g class="node__service node__inactive">
<use xlink:href="#icon-Service"></use>
</g>
<g><svg x="0" y="0" style="overflow: visible;"><text class="label-down__item__name" width="150" y="48.92399978637695" x="0" text-anchor="middle" style="pointer-events: none;"><tspan x="0" dy="0em">General Accounting</tspan></text></svg>
<svg x="0" y="0" style="overflow: visible;"><text class="label-down__item__type" width="150" y="68.92399978637695" x="0" text-anchor="middle" style="pointer-events: none;"><tspan x="0" dy="0.71em">Service</tspan></text></svg>
</g><svg class="zoomout__container" height="20" width="20" viewBox="15 55 30 20"><g><circle class="node__shape-fill" cx="15" cy="10" r="10" stroke-width="2"></circle><line class="node__shape-fill" x1="8.5" y1="10" x2="21" y2="10" stroke-width="2"></line><line x1="16" y1="5" x2="16" y2="15" stroke-width="2"></line></g></svg></g>
</g>
<g class="vx-group node__3" transform="translate(946.3636363636364, 353.5)" opacity="1">
<g transform="matrix(1,0,0,1,0,0)" class="node__container"><svg class="node__inactive" x="0" y="0" style="overflow: visible;"><polygon points="25.98076211353316,-14.999999999999998 25.98076211353316,14.999999999999998 1.83697019872103e-15,30 -25.98076211353316,14.999999999999998 -25.980762113533157,-15.000000000000004 -5.510910596163089e-15,-30" class="node__hexagon"></polygon></svg>
<g class="node__service node__inactive">
<use xlink:href="#icon-Service"></use>
</g>
<g><svg x="0" y="0" style="overflow: visible;"><text class="label-down__item__name" width="150" y="48.92399978637695" x="0" text-anchor="middle" style="pointer-events: none;"><tspan x="0" dy="0em">Accounts Payable</tspan></text></svg>
<svg x="0" y="0" style="overflow: visible;"><text class="label-down__item__type" width="150" y="68.92399978637695" x="0" text-anchor="middle" style="pointer-events: none;"><tspan x="0" dy="0.71em">Service</tspan></text></svg>
</g><svg class="zoomout__container" height="20" width="20" viewBox="15 55 30 20"><g><circle class="node__shape-fill" cx="15" cy="10" r="10" stroke-width="2"></circle><line class="node__shape-fill" x1="8.5" y1="10" x2="21" y2="10" stroke-width="2"></line><line x1="16" y1="5" x2="16" y2="15" stroke-width="2"></line></g></svg></g>
</g>
<g class="vx-group node__4" transform="translate(94.63636363636364, 707)" opacity="1">
<g transform="matrix(1,0,0,1,0,0)" class="node__container"><svg class="node__inactive" x="0" y="0" style="overflow: visible;"><polygon points="25.98076211353316,-14.999999999999998 25.98076211353316,14.999999999999998 1.83697019872103e-15,30 -25.98076211353316,14.999999999999998 -25.980762113533157,-15.000000000000004 -5.510910596163089e-15,-30" class="node__hexagon"></polygon></svg>
<g class="node__activity node__inactive">
<use xlink:href="#icon-Activity"></use>
</g>
<g><svg x="0" y="0" style="overflow: visible;"><text class="label-down__item__name" width="150" y="52.00199890136719" x="0" text-anchor="middle" style="pointer-events: none;"><tspan x="0" dy="-1em">General Ledger Group</tspan><tspan x="0" dy="1em">Accounting</tspan></text></svg>
<svg x="0" y="0" style="overflow: visible;"><text class="label-down__item__type" width="150" y="72.00199890136719" x="0" text-anchor="middle" style="pointer-events: none;"><tspan x="0" dy="0.71em">Activity</tspan></text></svg>
</g>0</g>
</g>
<g class="vx-group node__5" transform="translate(283.90909090909093, 707)" opacity="1">
<g transform="matrix(1,0,0,1,0,0)" class="node__container"><svg class="node__inactive" x="0" y="0" style="overflow: visible;"><polygon points="25.98076211353316,-14.999999999999998 25.98076211353316,14.999999999999998 1.83697019872103e-15,30 -25.98076211353316,14.999999999999998 -25.980762113533157,-15.000000000000004 -5.510910596163089e-15,-30" class="node__hexagon"></polygon></svg>
<g class="node__activity node__inactive">
<use xlink:href="#icon-Activity"></use>
</g>
<g><svg x="0" y="0" style="overflow: visible;"><text class="label-down__item__name" width="150" y="52.00199890136719" x="0" text-anchor="middle" style="pointer-events: none;"><tspan x="0" dy="0em">Accounts Receivable</tspan></text></svg>
<svg x="0" y="0" style="overflow: visible;"><text class="label-down__item__type" width="150" y="72.00199890136719" x="0" text-anchor="middle" style="pointer-events: none;"><tspan x="0" dy="0.71em">Activity</tspan></text></svg>
</g>0</g>
</g>
</g>
</g>
</g>
</svg>
</div>
<div class="item">Column two</div>
<div class="item">Column Tree</div>
</div>
</div>
</body>
</html>
我已将preserveAspectRatio设置为默认值xMinYMin meet
,但我不知道要将宽度和高度的初始值设为什么。
在我的示例中,svg图表从左下角开始作为小图像,然后可以弹出并占据整个页面。
我真的不明白如何获得这些值。
我只是使用试错法来获取初始viewBox值,我想以编程方式根据父元素或其他方式获取它们。
阅读了很多文章,但仍然不清楚第一个值和svg扩展到整个页面时应该是什么初始值。
<svg>
转换成svg文件
,然后使用background: url('some.svg');
来包含它,使用background-size: cover/contain/value's
来设置样式。当需要 "弹出" 时,只需扩展column 1
并将其放置到其他 div 的上方,使用position:fixed
或absolute
并设置z-index
即可。 - Ramon de Vries