gpt4 book ai didi

html - 跨设备/屏幕尺寸的 SVG 缩放问题

转载 作者:行者123 更新时间:2023-11-28 05:36:03 25 4
gpt4 key购买 nike

我试图使用 SVG 标记创建一个覆盖我页面的一部分的“异常”多边形。但是,当我缩放页面时,该对象从 View 中消失了。缩放时如何使 SVG 保持可见?

干杯

.about {
/* Keeps the div fixed in one position */
position: fixed;
/* Removes White surrounding boxes */
padding: 0;
margin: 0;
top: 0;
left: 0;
/* Sets the Width and height of the div, and the background colour */
width: 100vw;
height: 100vh;
background: #141311;
}

svg {
width: 100vw;
height: 100vh;
}
<div class="about">
<svg height="100%" width="100%">
<path d="M1000 0 L1280 0 L1280 700 L900 700 Z" fill="#FEFEFA" />
</svg>
</div>

最佳答案

这可以通过一些 javascript 来调整它的宽度/高度和它的 viewBox 值来完成。请参见下面的示例。

<head>
<title>Untitled</title>
<style type="text/css">
<!--
.about {
/* Keeps the div fixed in one position */
position: fixed;
/* Removes White surrounding boxes */
padding: 0;
margin: 0;
top: 0;
left: 0;
/* Sets the Width and height of the div, and the background colour */
width: 100vw;
height: 100vh;
background: blue;
}
-->
</style>
</head>

<body>
<div class="about">
<svg>
<path d="M1000 0 L1280 0 L1280 700 L900 700 Z" fill="red" />
</svg>
</div>
</body>
<script>
//--onload, onresize----
function sizeFull()
{
var mySVG=document.getElementsByTagName("svg")[0]
var svgW=window.innerWidth
var svgH=window.innerHeight
var bb=mySVG.getBBox()
var bbx=bb.x
var bby=bb.y
var bbw=bb.width
var bbh=bb.height

mySVG.setAttribute("viewBox",bbx+" "+bby+" "+bbw+" "+bbh )
mySVG.setAttribute("width",svgW)
mySVG.setAttribute("height",svgH)
}
sizeFull(); //---init window---
document.addEventListener("onload",sizeFull(),false)
window.onresize=sizeFull
</script>

关于html - 跨设备/屏幕尺寸的 SVG 缩放问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38204335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com