拉伸
我正在尝试在 DOM 内拉伸 svg 文档以适应窗口大小。
像这样:
<div id="y">
<div id="button"> click to zoom</div>
<embed id="x" src="s17.svg" >
<script>
var btn= document.getElementById("button");
btn.addEventListener('click',function(){
var z= document.getElementsByTagName("embed")[0];
var y = z.getSVGDocument();
y.lastChild.setAttribute("viewBox","0 0 "+window.innerWidth+" "+window.innerHeight);
},false);
</script>
</div>
css:
#x{
height:100%;
width:100%;
overflow:hidden;
}
#y{
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
overflow:hidden;
}
这不起作用...我做错了什么?
I am trying to stretch an svg document inside an DOM in order to fit the window size.
like so:
<div id="y">
<div id="button"> click to zoom</div>
<embed id="x" src="s17.svg" >
<script>
var btn= document.getElementById("button");
btn.addEventListener('click',function(){
var z= document.getElementsByTagName("embed")[0];
var y = z.getSVGDocument();
y.lastChild.setAttribute("viewBox","0 0 "+window.innerWidth+" "+window.innerHeight);
},false);
</script>
</div>
css:
#x{
height:100%;
width:100%;
overflow:hidden;
}
#y{
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
overflow:hidden;
}
This isn't working... What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有浏览器都应该能够很好地处理这个问题:
preserveAspectRatio="none"
到 svg 元素以使其拉伸,即使 css-viewport 宽高比与 viewBox 宽高比不匹配。如果您不想拉伸,那么您也可以执行
preserveAspectRatio="xMidYMid slice"
(填充整个视口,必要时切掉部分)或preserveAspectRatio="xMidYMid meet"
(这是默认设置,将 svg 居中视口并保持纵横比)。All browsers should be able to handle this just fine:
preserveAspectRatio="none"
to the svg element to make it stretch even if the css-viewport aspect ratio doesn't match the viewBox aspect ratio.If you don't want stretching then you can also do
preserveAspectRatio="xMidYMid slice"
(fill whole viewport, slicing away parts if necessary) orpreserveAspectRatio="xMidYMid meet"
(this is the default, center the svg in the viewport and maintain the aspect ratio).所有浏览器处理 SVG 支持的方式都完全不同。我认为最好的选择是使用
object
标签而不是embed
,并且您仍然需要进行一些修改才能使其在每个浏览器上看起来正确。 此链接和此链接提供了一些使其跨浏览器工作的有用信息。All browsers handle SVG support completely differently. I think your best bet is to use an
object
tag instead ofembed
, and you still have to do some hacking to get it to look right on each browser. This link and this link have some useful information for getting it to work cross-browser.