css 为什么我的svg动画在safari显示器上不起作用?

pcww981p  于 2023-05-23  发布在  其他
关注(0)|答案(2)|浏览(148)

我有一个勾号动画,在chrome浏览器上工作得很好。然而,当涉及到safari时,svg根本不显示。

$('#checkmark-svg').on('click', function(){
  svg = $(this);
  svg.removeClass('run-animation').width();
  svg.addClass('run-animation');
  return false;
})
.cls-1{
  fill:none;
  stroke:#231f20;
  stroke-miterlimit:10;
  stroke-width:5px;
}

.svg-check {
  position: inherit;
}

svg {
  margin: auto;
  width: 50px;
  height: 50px;
  display: block;
  
  .cls-1 {
    stroke: #2d77e3;
  }
}

.circle {
  stroke-dasharray: 700;
  stroke-dashoffset: 700;
}

.checkmark {
  stroke-dasharray: 150;
  stroke-dashoffset: 150;
}

.run-animation {  
  .circle {
    animation: 2.5s circleDraw forwards;
  }

  .checkmark {
    animation: 0.75s checkmarkDraw forwards;
    animation-delay: 1s;
  }
}

@keyframes circleDraw {
  from {
    stroke-dashoffset: 700;
  }
  to {
    stroke-dashoffset: 0;
  }
}

@keyframes checkmarkDraw {
  from {
    stroke-dashoffset: 150;
  }
  to {
    stroke-dashoffset: 0;
  }
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

        <svg id="checkmark-svg" class="run-animation svg-check" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 193.3 193.3">
            <circle class="cls-1 circle" cx="96.65" cy="96.65" r="94.15"/>
            <polyline class="cls-1 checkmark" points="46.9 101.4 76.9 131.4 146.4 61.9"/>
        </svg>

JS代码只是点击时动画的刷新,可能会被省略。此外,没有可能影响其行为的后台css类。

pvabu6sv

pvabu6sv1#

您正在使用最新的CSS nesting功能,即still not widely supported(仅限基于Chromium的浏览器和最新的Safari技术预览版)。
通过取消嵌套所有CSS规则,您的代码将在所有浏览器中工作,包括Safari和Firefox:

$('#checkmark-svg').on('click', function() {
  svg = $(this);
  svg.removeClass('run-animation').width();
  svg.addClass('run-animation');
  return false;
})
.cls-1 {
  fill: none;
  stroke: #231f20;
  stroke-miterlimit: 10;
  stroke-width: 5px;
}

.svg-check {
  position: inherit;
}

svg {
  margin: auto;
  width: 50px;
  height: 50px;
  display: block;
}
svg .cls-1 {
  stroke: #2d77e3;
}

.circle {
  stroke-dasharray: 700;
  stroke-dashoffset: 700;
}

.checkmark {
  stroke-dasharray: 150;
  stroke-dashoffset: 150;
}

.run-animation .circle {
  animation: 2.5s circleDraw forwards;
}
.run-animation .checkmark {
  animation: 0.75s checkmarkDraw forwards;
  animation-delay: 1s;
}

@keyframes circleDraw {
  from {
    stroke-dashoffset: 700;
  }
  to {
    stroke-dashoffset: 0;
  }
}

@keyframes checkmarkDraw {
  from {
    stroke-dashoffset: 150;
  }
  to {
    stroke-dashoffset: 0;
  }
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<svg id="checkmark-svg" class="run-animation svg-check" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 193.3 193.3">
    <circle class="cls-1 circle" cx="96.65" cy="96.65" r="94.15"/>
    <polyline class="cls-1 checkmark" points="46.9 101.4 76.9 131.4 146.4 61.9"/>
</svg>

请注意,您可以使用@supports规则来检测CSS嵌套:

.elem::after {
  content: "Your browser does not support CSS nesting";
  color: red;
}
@supports (selector(&)) {
  .elem::after {
    content: "Your browser supports CSS nesting";
    color: green;
  }
}
<span class=elem></span>

但这意味着您必须为那些不支持它的对象解除嵌套,所以我不相信这对您有多大用处。

3bygqnnd

3bygqnnd2#

please use webkit for animation in css

.run-animation {  
  .circle {
    -webkit-animation: 2.5s circleDraw forwards;
            animation: 2.5s circleDraw forwards;
  }

  .checkmark {
    -webkit-animation: 0.75s checkmarkDraw forwards;
            animation: 0.75s checkmarkDraw forwards;
    -webkit-animation-delay: 1s;
            animation-delay: 1s;
  }
}

@-webkit-keyframes circleDraw {
  from {
    stroke-dashoffset: 700;
  }
  to {
    stroke-dashoffset: 0;
  }
}

@keyframes circleDraw {
  from {
    stroke-dashoffset: 700;
  }
  to {
    stroke-dashoffset: 0;
  }
}

@-webkit-keyframes checkmarkDraw {
  from {
    stroke-dashoffset: 150;
  }
  to {
    stroke-dashoffset: 0;
  }
}

@keyframes checkmarkDraw {
  from {
    stroke-dashoffset: 150;
  }
  to {`enter code here`
    stroke-dashoffset: 0;
  }
}

相关问题