【问题标题】:SVG Animation does not fill container in SafariSVG 动画不填充 Safari 中的容器
【发布时间】:2021-07-01 07:48:58
【问题描述】:

我遇到了 SVG 动画无法在 Safari 浏览器上填充整个容器的问题。由于某种原因,我的 SVG 的 Safari 宽度似乎是随机的,但它在 Chrome 和其他浏览器上运行良好。

预期行为:

  • 点击菜单图标后,SVG 背景动画应该用红色边框填充容器。

实际行为

  • 在 Safari Mac / iOS 上动画不会填满整个容器

Codepen

下面的简化代码:

import React, { useEffect, useState } from "react";
import styled from "styled-components";

function MenuOverlay({ isOpen, firstLoad }) {
  const controls = useAnimation();

  const variants = {
    open: {
      d: [
        "M 0 24 L 0 0 L 1 0 C 1 8 1 16 1 24 L 0 24 Z",
        "M 0 24 L 0 0 L 9.143 0 C 18.286 0 18.286 24 9.143 24 L 0 24 Z",
        "M 0 24 L 0 0 L 24 0 C 24 8 24 16 24 24 L 0 24 Z",
      ],
      scaleX: [0, 1, 1],
      transition: {
        ease: ["easeIn", "easeOut"],
      },
    },
    close: {
      d: [
        "M 0 24 L 0 0 L 24 0 C 24 8 24 16 24 24 L 0 24 Z",
        "M 0 24 L 0 0 L 16.5 0 C 9 5.5 8.5 17.5 16.5 24 L 0 24 Z",
        "M 0 24 L 0 0 L 1 0 C 1 8 1 16 1 24 L 0 24 Z",
      ],
      scaleX: [1, 1, 0],
      transition: {
        ease: ["easeIn", "easeOut"],
      },
    },
  };

  // Sequence animation
  async function open() {
    await controls.start("open");
  }
  async function close() {
    await controls.start("close");
  }

  if (!firstLoad) {
    isOpen ? open() : close();
  }

  return (
    <LazyMotion features={domAnimation}>
      <Container>
        <Svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" preserveAspectRatio="none">
          <Path animate={controls} variants={variants} />
        </Svg>
      </Container>
    </LazyMotion>
  );
}

export default MenuOverlay;

// Styled Components
const Container = styled(m.div)`
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  width: 100vw;
  pointer-events: none;
  z-index: 0;
`;

const Svg = styled.svg`
  border: 1px solid red;
  height: 100vh;
  /* Mobile */
  width: 100vw;
  transform: scaleX(-1);
  /* Tablet */
  @media screen and (min-width: ${({ theme }) => theme.breakpoints[0]}) {
    width: 75vw;
    transform: unset;
  }
  /* Desktop */
  @media screen and (min-width: ${({ theme }) => theme.breakpoints[1]}) {
    width: 50vw;
  }
`;
const Path = styled(m.path)`
  fill: ${({ theme }) => theme.black[1]};
`;

【问题讨论】:

    标签: javascript html css svg framer-motion


    【解决方案1】:

    尝试在转换之前添加 WebKit 版本的转换:scaleX(-1);

    -webkit-transform: scaleX(-1);
    

    您能否向我们提供一个 codepen 或指向您拥有它的链接,以便我们更好地了解行为?

    好的,在查看您的代码后尝试修改您的 SVG 视图框

    <Svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" preserveAspectRatio="none">
    

    <Svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" preserveAspectRatio="none">
    

    【讨论】:

    • 我已经用演示链接更新了问题,其中出现了这种行为。我也尝试过您的解决方案,但不幸的是它不能解决我的问题。我很快就会用 codepen 更新这个线程。演示:piwnice-prod.netlify.app/menu-animation 谢谢
    • 我做了一个codepen的例子,链接如下:codesandbox.io/s/safari-broken-svg-animation-yzfe3
    • 修改了我上面的响应,也在浏览器缓存后测试它是否可以与您的代码一起正常工作,但是当我清除缓存时它再次执行此操作,请尝试我在上面添加的视图框更改,即更多 safari友好,让我知道。
    • 谢谢!我实际上解决了我的问题,虽然我不明白为什么 safari 之前渲染它如此奇怪?
    • 使用 SVG
    猜你喜欢
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 2019-11-10
    • 2018-05-20
    • 2015-12-12
    • 2020-04-27
    相关资源
    最近更新 更多