在Safari中移动元素时SVG上的条纹

发布于 2025-01-23 12:17:04 字数 1960 浏览 1 评论 0原文

当我尝试在SVG上移动一个孩子Div时,SVG开始具有怪异的垂直条纹。这仅在Safari上发生(我在iPad,iPhone,Mac,PC上测试了Chrome和Safari)。

要查看错误,请将光标放在图像上,然后向右移动。如果没有出现,请缩短屏幕的宽度。

此错误的复制(vercel.app)

    function App() {
      const [delta, setDelta] = useState(0);
    
      const handleTouchMove = (event) => {
        let delta = event.touches[0].clientX;
        setDelta(delta);
      };
    
      const handleMouseMove = (event) => {
        let delta = event.clientX + 40;
        setDelta(delta);
      };
    
    
      return (
        <div
          style={{
            background: "#5f5fc4",
            display: "flex",
            justifyContent: "center",
            alignItems: "center",
            border: "1px solid #413793"
          }}
        >
          <svg
            width="500"
            height="500"
            onTouchMove={handleTouchMove}
            onMouseMove={handleMouseMove}
            style={{ background: "#283593" }}
          >
            <g>
              <foreignObject x="0" y="0" width="500" height="500">
                <picture>
                  <img
                    alt=""
                    width="100%"
                    src="https://i.imgur.com/h1vMJwO.png"
                  />
                </picture>
              </foreignObject>
            </g>
          </svg>
            <div style={{ width: delta, background: "#001064", color: "white", fontSize: '1.2rem' }}>
              Another div here with {delta}px.
            </div>
        </div>
      );
    }

​src/app.js“ rel =“ nofollow noreferrer”>此特定错误的存储库

此错误绝对是Safari的特定特定的:但是,从个人经验来看,我已经通过其他项目看到了这种行为,浏览器不是一个项目出现错误的问题。但是我找不到解决方案或解释。

我该如何修复它,或者至少为什么会发生?

预先感谢。

When I try to move a child div on top of an SVG, the SVG starts to have weird vertical stripes. This only happens on Safari (I tested on iPad, iPhone, Mac, PC both Chrome and Safari).

To see the bug, position your cursor over the image, and move to the right. If it doesn't appear, please just shorten the width of the screen.

Reproduction of this bug (vercel.app)

GIF Preview - This is how appears

    function App() {
      const [delta, setDelta] = useState(0);
    
      const handleTouchMove = (event) => {
        let delta = event.touches[0].clientX;
        setDelta(delta);
      };
    
      const handleMouseMove = (event) => {
        let delta = event.clientX + 40;
        setDelta(delta);
      };
    
    
      return (
        <div
          style={{
            background: "#5f5fc4",
            display: "flex",
            justifyContent: "center",
            alignItems: "center",
            border: "1px solid #413793"
          }}
        >
          <svg
            width="500"
            height="500"
            onTouchMove={handleTouchMove}
            onMouseMove={handleMouseMove}
            style={{ background: "#283593" }}
          >
            <g>
              <foreignObject x="0" y="0" width="500" height="500">
                <picture>
                  <img
                    alt=""
                    width="100%"
                    src="https://i.imgur.com/h1vMJwO.png"
                  />
                </picture>
              </foreignObject>
            </g>
          </svg>
            <div style={{ width: delta, background: "#001064", color: "white", fontSize: '1.2rem' }}>
              Another div here with {delta}px.
            </div>
        </div>
      );
    }

Repository of this specific bug

This bug is definitely specific to Safari: However, from personal experience, I have seen this behavior through other projects where the browser is not a problem for the bug to appear. But I can't find a solution or an explanation.

How can I fix it, or at least why does it happen?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

殤城〤 2025-01-30 12:17:04

非常有趣的情况。
我有一个修复程序,但我不知道为什么会发生。可能是Safari渲染问题。

  1. 删除Align-Items:Center从包装器中
  2. 删除背景色:#001064;从Inner Div和add display:flex; Align-Items:Center;
  3. 将另一个Div添加为文本包装器,然后再次设置背景颜色backick-color:#001064;并添加width:100%
...
  <div
          style={{
            background: "#5f5fc4",
            display: "flex",
            justifyContent: "center",
            border: "1px solid #413793"
          }}
        >
          <svg
            width="500"
            height="500"
            onTouchMove={handleTouchMove}
            onMouseMove={handleMouseMove}
            style={{ background: "#283593" }}
          >
            <g>
              <foreignObject x="0" y="0" width="500" height="500">
                <picture>
                  <img
                    alt=""
                    width="100%"
                    src="https://i.imgur.com/h1vMJwO.png"
                  />
                </picture>
              </foreignObject>
            </g>
          </svg>
            <div style={{ width: delta, color: "white", fontSize: '1.2rem', display:"flex", alignItems:"center" }}>
               <div style={{width:"100%",background: "#001064"}}>
                 Another div here with {delta}px.
               </div>
            </div>
        </div>
...

Very interesting case.
I have a fix, but I don't know why it happen. Probably safari rendering issue.

  1. remove align-items:center from wrapper
  2. remove background-color:#001064; from inner div and add display: flex; align-items: center;
  3. add another div as text wrapper and set again background color background-color:#001064; and add width:100%.
...
  <div
          style={{
            background: "#5f5fc4",
            display: "flex",
            justifyContent: "center",
            border: "1px solid #413793"
          }}
        >
          <svg
            width="500"
            height="500"
            onTouchMove={handleTouchMove}
            onMouseMove={handleMouseMove}
            style={{ background: "#283593" }}
          >
            <g>
              <foreignObject x="0" y="0" width="500" height="500">
                <picture>
                  <img
                    alt=""
                    width="100%"
                    src="https://i.imgur.com/h1vMJwO.png"
                  />
                </picture>
              </foreignObject>
            </g>
          </svg>
            <div style={{ width: delta, color: "white", fontSize: '1.2rem', display:"flex", alignItems:"center" }}>
               <div style={{width:"100%",background: "#001064"}}>
                 Another div here with {delta}px.
               </div>
            </div>
        </div>
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文