SVG 位置在页面重新缩放时移动
SVG position moving on page rescale
我目前正在为一个 uni 项目编写一个网站,而不仅仅是使用传统的形状。我想我会尝试使用 SVG 来制作自定义形状,但我目前遇到的唯一问题是我无法在重新缩放页面时修复其中一个 SVG 路径,而另一个是。
我尝试通过像素和百分比设置 SVG 路径的定位,但 none 似乎可行。我也试过将路径的位置设置为固定的。
Image of the site before scale
Image of rescale
SVG 代码
<div id="midWrapper">
<!--Middle container SVG as it is a custom shape-->
<div id="containerMiddle">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 980 590"><title>Custom shaped rectangle with corner missin</title><path d="M766.29,13.57,967.43,214.71A46.32,46.32,0,0,1,981,247.47V544.68A46.32,46.32,0,0,1,934.68,591H46.32A46.32,46.32,0,0,1,0,544.68V46.32A46.32,46.32,0,0,1,46.32,0H733.53A46.32,46.32,0,0,1,766.29,13.57Z" style="fill:#5f1742; pointer-events: none;"/></svg>
</div>
<!--Top right corner of the middle container, used as a link to call pendle burton-->
<div id="contactTriangle">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 185.99 185.99"><title>Phone Icon in a Triangle used to call the restaurant</title>
<path d="M186,9.92V176.05a9.92,9.92,0,0,1-16.93,7L2.93,16.93A9.92,9.92,0,0,1,9.94,0H176.07A9.92,9.92,0,0,1,186,9.92Z" style="fill:#fff"/>
<path d="M129.27,80.74a3,3,0,0,1-.14,2.54c-1.36,2.2-3,4.24-4.44,6.36-1.94,2.79-2.38,5.58-.64,8.76,3,5.56,5.82,11.26,8.71,16.9,1.8,3.49,5.12,5.07,8.43,3A132.31,132.31,0,0,0,155.11,108c2.38-1.94,2-5,1.76-7.79-.58-7.44-3.2-14.31-6.75-20.73-10.86-19.59-24.34-37.06-42.84-50.07-4.06-2.86-9.06-4.42-13.71-6.38a7,7,0,0,0-7.32,1.31,124.44,124.44,0,0,0-11,9.71C72,37.36,72.55,41,76.09,44,81,48.05,85.88,52,90.64,56.23a7.06,7.06,0,0,0,7.93,1.31c2.91-1.14,5.77-2.38,9-3.71A141.83,141.83,0,0,1,129.27,80.74Z" style="fill:#5f1742"/>
<path d="M107.56,53.83a141.83,141.83,0,0,1,21.71,26.91,3,3,0,0,1-.14,2.54c-1.36,2.2-3,4.24-4.44,6.36-1.94,2.79-2.38,5.58-.64,8.76,3,5.56,5.82,11.26,8.71,16.9,1.8,3.49,5.12,5.07,8.43,3A132.31,132.31,0,0,0,155.11,108c2.38-1.94,2-5,1.76-7.79-.58-7.44-3.2-14.31-6.75-20.73-10.86-19.59-24.34-37.06-42.84-50.07-4.06-2.86-9.06-4.42-13.71-6.38a7,7,0,0,0-7.32,1.31,124.44,124.44,0,0,0-11,9.71C72,37.36,72.55,41,76.09,44,81,48.05,85.88,52,90.64,56.23a7.06,7.06,0,0,0,7.93,1.31C101.48,56.4,104.34,55.16,107.56,53.83Z" style="fill:#5f1742"/>
</svg>
</div>
</div><!--Closing tag for SVG-->
元素的所有样式
#containerMiddle{
width: 980px;
height: 590px;
margin-top: 60px;
pointer-events: none;
margin-left: auto;
margin-right: auto;
}
#contactTriangle{
width: 185px;
height: 185px;
pointer-events: none;
margin-left: 66%;
margin-top: -31%;
}
如有任何关于如何改进此站点以使三角形保持原位并提高站点响应能力的建议,我们将不胜感激。
我认为您应该避免使用边距定位元素。
你总是希望你的角 svg 位于容器的右上角,所以尝试使用 position:absolute
、right:calc(50% - 490px)
和 top:0
- 这将适用于你当前的设置只要你的父容器有 position:relative
。
Fiddle 这里:https://jsfiddle.net/15scjpvd/1/
#midWrapper {
position:relative;
}
#containerMiddle{
width: 980px;
height: 590px;
margin-top: 60px;
pointer-events: none;
margin-left: auto;
margin-right: auto;
}
#contactTriangle{
width: 185px;
height: 185px;
pointer-events: none;
top:0;
right: calc(50% - 490px);
position:absolute;
}
我使用 50% - 490px 的原因是您的容器当前居中对齐 - 50% 代表父容器的中心,490px 是其全宽的一半。
如果您希望您的网站响应更快,而不是使用 width
和 height
设置像素值,您可以使用百分比。
看看这个 fiddle 然后:https://jsfiddle.net/3x6187ov/4/
那么三角形和矩形的宽度分别是父容器宽度的100%和30%。
#midWrapper {
position:relative;
width:100%;
}
#containerMiddle{
width: 100%;
margin-top: 60px;
pointer-events: none;
margin-left: auto;
margin-right: auto;
}
#contactTriangle{
width: 20%;
pointer-events: none;
top:0;
right:0;
position:absolute;
}
如果您不能让您的容器扩展超过某个 width
,您还可以指定 max-width
属性。然后您可以使用以下内容:https://jsfiddle.net/3fva1cqn/2/
#midWrapper {
position:relative;
width:100%;
max-width:960px;
}
#containerMiddle{
width: 100%;
margin-top: 60px;
pointer-events: none;
margin-left: auto;
margin-right: auto;
}
#contactTriangle{
width: 20%;
pointer-events: none;
top:0;
right:0;
position:absolute;
}
此外,在您当前的情况下,您不必同时指定 svg 的宽度和高度,因为只要您不专门将 preserveAspectRatio
设置为 none,默认情况下它们将保持使用 viewBox 声明的纵横比。
我目前正在为一个 uni 项目编写一个网站,而不仅仅是使用传统的形状。我想我会尝试使用 SVG 来制作自定义形状,但我目前遇到的唯一问题是我无法在重新缩放页面时修复其中一个 SVG 路径,而另一个是。
我尝试通过像素和百分比设置 SVG 路径的定位,但 none 似乎可行。我也试过将路径的位置设置为固定的。 Image of the site before scale Image of rescale
SVG 代码
<div id="midWrapper">
<!--Middle container SVG as it is a custom shape-->
<div id="containerMiddle">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 980 590"><title>Custom shaped rectangle with corner missin</title><path d="M766.29,13.57,967.43,214.71A46.32,46.32,0,0,1,981,247.47V544.68A46.32,46.32,0,0,1,934.68,591H46.32A46.32,46.32,0,0,1,0,544.68V46.32A46.32,46.32,0,0,1,46.32,0H733.53A46.32,46.32,0,0,1,766.29,13.57Z" style="fill:#5f1742; pointer-events: none;"/></svg>
</div>
<!--Top right corner of the middle container, used as a link to call pendle burton-->
<div id="contactTriangle">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 185.99 185.99"><title>Phone Icon in a Triangle used to call the restaurant</title>
<path d="M186,9.92V176.05a9.92,9.92,0,0,1-16.93,7L2.93,16.93A9.92,9.92,0,0,1,9.94,0H176.07A9.92,9.92,0,0,1,186,9.92Z" style="fill:#fff"/>
<path d="M129.27,80.74a3,3,0,0,1-.14,2.54c-1.36,2.2-3,4.24-4.44,6.36-1.94,2.79-2.38,5.58-.64,8.76,3,5.56,5.82,11.26,8.71,16.9,1.8,3.49,5.12,5.07,8.43,3A132.31,132.31,0,0,0,155.11,108c2.38-1.94,2-5,1.76-7.79-.58-7.44-3.2-14.31-6.75-20.73-10.86-19.59-24.34-37.06-42.84-50.07-4.06-2.86-9.06-4.42-13.71-6.38a7,7,0,0,0-7.32,1.31,124.44,124.44,0,0,0-11,9.71C72,37.36,72.55,41,76.09,44,81,48.05,85.88,52,90.64,56.23a7.06,7.06,0,0,0,7.93,1.31c2.91-1.14,5.77-2.38,9-3.71A141.83,141.83,0,0,1,129.27,80.74Z" style="fill:#5f1742"/>
<path d="M107.56,53.83a141.83,141.83,0,0,1,21.71,26.91,3,3,0,0,1-.14,2.54c-1.36,2.2-3,4.24-4.44,6.36-1.94,2.79-2.38,5.58-.64,8.76,3,5.56,5.82,11.26,8.71,16.9,1.8,3.49,5.12,5.07,8.43,3A132.31,132.31,0,0,0,155.11,108c2.38-1.94,2-5,1.76-7.79-.58-7.44-3.2-14.31-6.75-20.73-10.86-19.59-24.34-37.06-42.84-50.07-4.06-2.86-9.06-4.42-13.71-6.38a7,7,0,0,0-7.32,1.31,124.44,124.44,0,0,0-11,9.71C72,37.36,72.55,41,76.09,44,81,48.05,85.88,52,90.64,56.23a7.06,7.06,0,0,0,7.93,1.31C101.48,56.4,104.34,55.16,107.56,53.83Z" style="fill:#5f1742"/>
</svg>
</div>
</div><!--Closing tag for SVG-->
元素的所有样式
#containerMiddle{
width: 980px;
height: 590px;
margin-top: 60px;
pointer-events: none;
margin-left: auto;
margin-right: auto;
}
#contactTriangle{
width: 185px;
height: 185px;
pointer-events: none;
margin-left: 66%;
margin-top: -31%;
}
如有任何关于如何改进此站点以使三角形保持原位并提高站点响应能力的建议,我们将不胜感激。
我认为您应该避免使用边距定位元素。
你总是希望你的角 svg 位于容器的右上角,所以尝试使用 position:absolute
、right:calc(50% - 490px)
和 top:0
- 这将适用于你当前的设置只要你的父容器有 position:relative
。
Fiddle 这里:https://jsfiddle.net/15scjpvd/1/
#midWrapper {
position:relative;
}
#containerMiddle{
width: 980px;
height: 590px;
margin-top: 60px;
pointer-events: none;
margin-left: auto;
margin-right: auto;
}
#contactTriangle{
width: 185px;
height: 185px;
pointer-events: none;
top:0;
right: calc(50% - 490px);
position:absolute;
}
我使用 50% - 490px 的原因是您的容器当前居中对齐 - 50% 代表父容器的中心,490px 是其全宽的一半。
如果您希望您的网站响应更快,而不是使用 width
和 height
设置像素值,您可以使用百分比。
看看这个 fiddle 然后:https://jsfiddle.net/3x6187ov/4/ 那么三角形和矩形的宽度分别是父容器宽度的100%和30%。
#midWrapper {
position:relative;
width:100%;
}
#containerMiddle{
width: 100%;
margin-top: 60px;
pointer-events: none;
margin-left: auto;
margin-right: auto;
}
#contactTriangle{
width: 20%;
pointer-events: none;
top:0;
right:0;
position:absolute;
}
如果您不能让您的容器扩展超过某个 width
,您还可以指定 max-width
属性。然后您可以使用以下内容:https://jsfiddle.net/3fva1cqn/2/
#midWrapper {
position:relative;
width:100%;
max-width:960px;
}
#containerMiddle{
width: 100%;
margin-top: 60px;
pointer-events: none;
margin-left: auto;
margin-right: auto;
}
#contactTriangle{
width: 20%;
pointer-events: none;
top:0;
right:0;
position:absolute;
}
此外,在您当前的情况下,您不必同时指定 svg 的宽度和高度,因为只要您不专门将 preserveAspectRatio
设置为 none,默认情况下它们将保持使用 viewBox 声明的纵横比。