简体   繁体   English

如何根据浏览器宽度更改 SVG?

[英]How to change a SVG based on browser width?

I am searching for the best way to dynamically change an svg shape based on browser width.我正在寻找根据浏览器宽度动态更改 svg 形状的最佳方法。 Setting the width to 100% stretches the shape to follow the width of the browser, but it also scales or distorts the shape... What I am after: sketch将宽度设置为 100% 会拉伸形状以跟随浏览器的宽度,但它也会缩放或扭曲形状......我追求的是:草图

Is it possible to do this with Javascript or is there a better approach?是否可以使用 Javascript 或有更好的方法来做到这一点? HTML canvas? HTML画布? thanks!谢谢!

As you're after an alternative approach involving SVGs, here's one.当您采用涉及 SVG 的替代方法时,这里有一个。 Unfortunately I wouldn't say it's easier than using a canvas though.不幸的是,我不会说它比使用画布更容易。

Anyway - to be able to stretch one part of a shape independently from the other we actually need to divide that shape in two.无论如何 - 为了能够独立地拉伸形状的一部分,我们实际上需要将该形状一分为二。 That way the left part can remain static.这样左边的部分可以保持静止。

If we take a look at the shape you're after:如果我们看一下您所追求的形状:

we can easily see that there's a quarter circle我们可以很容易地看到有一个四分之一圆

and a right trapezoid和一个正确的梯形

Constructing them using the drawing commands is rather straight-forward - simply putting those side-by-side won't do the job though.使用绘图命令构建它们是相当简单的 - 只是将它们并排放置并不能完成这项工作。

The trick here is to make the quarter circle a fixed-size - eg 80 x 80 pixels - and don't give the trapezoid a size at all.这里的技巧是使四分之一圆具有固定大小 - 例如 80 x 80 像素 - 并且根本不给梯形一个大小。 Instead we need to populate the CSS background-image property of a plain <div> element with an URL to the shape.相反,我们需要使用形状的 URL 填充普通<div>元素的 CSS background-image属性。 This can be done in-line too.这也可以在线完成。 To make it stretch to the browser window, we need to make it's width 100% of the remaining space - which can be done by setting the overflow property of the element to hidden .要使其拉伸到浏览器窗口,我们需要使其宽度为剩余空间的 100% - 这可以通过将元素的overflow属性设置为hidden Now just make the background's height equal to the quarter circle's height and set the background-repeat property to no-repeat .现在只需使背景的高度等于四分之一圆的高度并将background-repeat属性设置为no-repeat

Now the browser stretches the background image to whatever the remaining width beside the static quarter circle is and the fixed 80 pixels height.现在浏览器将背景图像拉伸到静态四分之一圆旁边的剩余宽度和固定的 80 像素高度。

Here's an example:下面是一个例子:

 .svgB { overflow: hidden; background-image: url("data:image/svg+xml;utf8,<svg viewBox='0 0 5 5' xmlns='http://www.w3.org/2000/svg' preserveAspectRatio='none'><polygon points='0,0 5,0 5,2.5 0,5' style='fill:grey;'/></svg>"); background-repeat: no-repeat; background-size: 100% 80px; } .svgB>* { color: white; padding-left: 5px; } .svgA { float: left; width: 80px; height: 80px; }
 <div class="svgA"> <svg viewBox="0 0 5 5" xmlns="http://www.w3.org/2000/svg"> <path fill="black" d="M5,5 a5,5 0 0,1 -5,-5 L5,0" /> </svg> </div> <div class="svgB" style="height:80px;"> <p>Here's some text</p> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM