html - SVG 位置在页面重新缩放时移动

标签 html css web svg resize

我目前正在为一个 uni 元素编写一个网站,而不仅仅是使用传统的形状。我想我会尝试使用 SVG 来制作自定义形状,但我目前遇到的唯一问题是我无法在重新缩放页面时修复其中一个 SVG 路径,而另一个是。

我曾尝试通过 px 和百分比来设置 SVG 路径的定位,但似乎都不起作用。我也试过将路径的位置设置为固定的。 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%;
}

如有任何关于如何改进此站点以使三 Angular 形保持原位并提高站点响应能力的建议,我们将不胜感激。

最佳答案

我认为您应该避免使用边距定位元素。

你总是希望你的 Angular 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 是其全宽的一半。

如果您希望您的网站响应更快,而不是使用具有设置像素值的 widthheight,您可以使用百分比。

然后看看这个 fiddle :https://jsfiddle.net/3x6187ov/4/ 那么三 Angular 形和矩形的宽度分别是父容器宽度的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;
}

如果您不能让您的容器展开超过某个宽度,您还可以指定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 声明的纵横比。

关于html - SVG 位置在页面重新缩放时移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55443514/

相关文章:

html - 在 CSS 中的粘性页脚上方水平和垂直居中

html - 如何使页脚对齐到最底部?

html - 如何设置水平 <li> 之间的空间以使其恢复 100% 的页面

Firefox 无法绕笛卡尔平面上象限 2 和象限 4 中的轴进行三维旋转

css - 如何将 css 类添加到现有的 Woocommerce/Wordpress 小部件

javascript - jQuery:内部缓存问题

javascript - 如何在鼠标悬停时使图像变暗并显示按钮?

css - 如何使导航链接响应浏览器调整大小

python - 在网站上运行 Python 文件

javascript - 在选项卡处于后台时使用 d3.js 绘制 SVG