SVG矩形在Firefox中组合在一起,但在Chrome / Safari中不是

时间:2019-01-28 16:12:50

标签: javascript css animation firefox svg

我有一个动画的SVG。它可以正常工作(或至少现在还需要),但是,当我在Firefox中查看时,SVG的第二个和第三个矩形被挤压在一起。

不知道为什么,因为在Chrome / Safari中很好。我认为这可能与transform: scaleY有关,但不是100%肯定。

如果有人可以告诉我为什么/如何修复,那就太好了。

旁注:如果有人知道如何在停止/启动时对平滑度进行排序,那就太棒了,但这可能是另一个问题。

Codepen:https://codepen.io/Will5592/pen/WPQQKB

const svg = document.querySelectorAll('rect');

svg.forEach(item => {
  item.addEventListener('click', (e) => {
    svg.forEach(i => i.classList.toggle('animation-on'))
  })
})
body {
  height: 80vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

svg {
  width: 25vw;
  height: 25vh;
  cursor: pointer;
}

.animation-on {
  animation: updown 0.75s infinite linear;
}

rect {
  transform-origin: 0 50%;
  transform: scaleY(0.75);
}

rect:nth-child(1) {
  animation-delay: 0.05s;
}

rect:nth-child(2) {
  animation-delay: 0.075s;
  animation-duration: 0.65s;
}

rect:nth-child(3) {
  animation-delay: 0.10s;
  animation-duration: 0.75s;
}

rect:nth-child(4) {
  animation-delay: 0.125s;
  animation-duration: 0.75s;
}

rect:nth-child(5) {
  animation-delay: 0.15s;
  animation-duration: 0.85s;
}

@keyframes updown {
  0% {
    transform: scaleY(0.5);
  }
  50% {
    transform: scaleY(1);
  }
  100% {
    transform: scaleY(0.5);
  }
}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 14 17.5">
  
  <g id="Layer_2" data-name="Layer 2">
    <g id="Layer_1-2" data-name="Layer 1">
      <defs>
        <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="100%">
          <stop offset="0%" style="stop-color:purple;stop-opacity:1" />
          <stop offset="100%" style="stop-color:indigo;stop-opacity:1" />
        </linearGradient>
      </defs>
      <rect fill="url(#grad1)" rx="2px" ry="1px" x="2.86" y="4" width="2.57" height="9.5"/>
      <rect fill="url(#grad1)" rx="2px" ry="1px"x="8.57" y="1.85" width="2.57" height="13.81"/>
      <rect fill="url(#grad1)" rx="2px" ry="1px"x="11.43" y="5.18" width="2.57" height="7.14"/>
      <rect fill="url(#grad1)" rx="2px" ry="1px"y="6.13" width="2.57" height="5.24"/>
      <rect fill="url(#grad1)" rx="2px" ry="1px"x="5.71" width="2.57" height="17.5"/>
    </g>
  </g>  
</svg>

1 个答案:

答案 0 :(得分:1)

正如我在上面的评论中所述。为该问题使用整数x似乎是解决此问题的方法。

在下面的示例中,我将所有坐标值乘以10并四舍五入x坐标。

const svg = document.querySelectorAll('rect');

svg.forEach(item => {
  item.addEventListener('click', (e) => {
    svg.forEach(i => i.classList.toggle('animation-on'))
  })
})
body {
  height: 80vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

svg {
  width: 25vw;
  height: 25vh;
  cursor: pointer;
}

.animation-on {
  animation: updown 0.75s infinite linear;
}

rect {
  transform-origin: 0 50%;
  transform: scaleY(0.75);
}

rect:nth-child(1) {
  animation-delay: 0.05s;
}

rect:nth-child(2) {
  animation-delay: 0.075s;
  animation-duration: 0.65s;
}

rect:nth-child(3) {
  animation-delay: 0.10s;
  animation-duration: 0.75s;
}

rect:nth-child(4) {
  animation-delay: 0.125s;
  animation-duration: 0.75s;
}

rect:nth-child(5) {
  animation-delay: 0.15s;
  animation-duration: 0.85s;
}

@keyframes updown {
  0% {
    transform: scaleY(0.5);
  }
  50% {
    transform: scaleY(1);
  }
  100% {
    transform: scaleY(0.5);
  }
}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 140 175">
  
  <g id="Layer_2" data-name="Layer 2">
    <g id="Layer_1-2" data-name="Layer 1">
      <defs>
        <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="100%">
          <stop offset="0%" style="stop-color:purple;stop-opacity:1" />
          <stop offset="100%" style="stop-color:indigo;stop-opacity:1" />
        </linearGradient>
      </defs>
      <rect fill="url(#grad1)" rx="20px" ry="1px" x="29" y="40" width="25.7" height="95"/>
      <rect fill="url(#grad1)" rx="20px" ry="1px" x="86" y="18.5" width="25.7" height="138.1"/>
      <rect fill="url(#grad1)" rx="20px" ry="1px" x="114" y="51.8" width="25.7" height="71.4"/>
      <rect fill="url(#grad1)" rx="20px" ry="1px" y="61.3" width="25.7" height="52.4"/>
      <rect fill="url(#grad1)" rx="20px" ry="1px" x="57" width="25.7" height="175"/>
    </g>
  </g>  
</svg>