我有一个简单的使用案例。我有一个外部div和内部的svg。如下所示:
<div>
<svg>
...
...
</svg>
</div>
我正在尝试让svg在div内滚动,但是一直不成功:( 我尝试设置:
div {
position: relative;
width: 100%;
overflow: scroll;
}
svg {
width: 100%;
}
但是它不起作用,你们能帮我吗?感谢您的帮助!
我有一个简单的使用案例。我有一个外部div和内部的svg。如下所示:
<div>
<svg>
...
...
</svg>
</div>
我正在尝试让svg在div内滚动,但是一直不成功:( 我尝试设置:
div {
position: relative;
width: 100%;
overflow: scroll;
}
svg {
width: 100%;
}
你需要定义容器的高度,否则容器的高度将根据svg的高度进行调整。
HTML:
<div>
<svg viewbox="0 0 400 400">
<path d="M 200 100 l 100 200 l -200 0 Z" stroke-width="5" stroke="red"></path>
</svg>
</div>
CSS:
div {
position: relative;
width: 100%;
height: 400px;
overflow-y: scroll;
}
#outbox_div {
border: 3px solid grey;
border-radius:2px;
height:200px;
width:80%;
overflow-y:scroll;
}
<div class="container">
<svg></svg>
</div>
<style>
.container {
overflow: scroll;
}
</style>
并且一些代码用于在运行时确定宽度并将其设置为CSS属性
var $svg = $("svg"),
bBox = $svg[0].getBBox();
$svg.css("width", bBox.width + "px");
因此,它最终变成了类似于:
<div class="container" style="overflow:scroll">
<svg style="width:123px;"></svg>
</div>