gpt4 book ai didi

css - SVG 虚线动画在 Chrome 中不起作用

转载 作者:行者123 更新时间:2023-11-28 15:06:16 24 4
gpt4 key购买 nike

在我的动画中使用了 CSSSVGJS

我创建了一些路径。用户可以选择路径,他想看到什么。单击按钮后,所选路径开始绘制。我的动画在 MozzillaEdge 中有效,但在 Chrome 中无效。我的代码如下所示:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<style>
.opcjaApath{
-webkit-animation: dash1 10s linear forwards;
-webkit-animation-iteration-count: 1;
-moz-stroke-dasharray: 2684;
-moz-stroke-dashoffset: 2684;
-moz-animation: dash1 10s linear forwards;
-moz-animation-iteration-count: 1;
-o-stroke-dasharray: 2684;
-o-stroke-dashoffset: 2684;
-o-animation: dash1 10s linear forwards;
-o-animation-iteration-count: 1;
animation: dash1 10s linear forwards;
animation-iteration-count: 1;
stroke-dasharray: 2684;
stroke-dashoffset: 2684;
}

.opcjaBpath{
-webkit-animation: dash1 10s linear forwards;
-webkit-animation-iteration-count: 1;
-moz-animation: dash1 10s linear forwards;
-moz-animation-iteration-count: 1;
-o-animation: dash1 10s linear forwards;
-o-animation-iteration-count: 1;
animation: dash1 10s linear forwards;
animation-iteration-count: 1;
stroke-dasharray: 2684;
stroke-dashoffset: 2684;
}

@-webkit-keyframes dash1 {
0% {
-webkit-stroke-dashoffset: 2684;
}
100% {
-webkit-stroke-dashoffset: 0;
}
}

@-moz-keyframes dash1 {
0% {
-moz-stroke-dashoffset: 2684;
}
100% {
-moz-stroke-dashoffset: 0;
}
}
@-o-keyframes dash1 {
0% {
-o-stroke-dashoffset: 2684;
}
100% {
-o-stroke-dashoffset: 0;
}
}

@keyframes dash1 {
0% {
stroke-dashoffset: 2684;
}
100% {
stroke-dashoffset: 0;
}
}

</style>

<html>
<button id="A"> Option A </button>
<button id="B"> Option B </button>

<svg width="400" height="300">

<g id="optionA">
<path class="opcjaApath" d="M50,30 250,30 250,54 50,54 50,78 250,78 250,102 50,102 50,126 250,126 250,150 50,150 50,174 250,174 250,198 50,198 50,222 250,222 250,246 50,246 50,270 250,270" stroke="red"
stroke-width="3" fill="none"/>
</g>
<g id="optionB">

<path class="opcjaBpath" d="M150,150 150,126 170,126 170,174 130,174 130,102 190,102 190,198 110,198 110,78 210,78 210,222 90,222 90,54 230,54 230,246 70,246 70,30 250,30 250,270 50,270 50,30" stroke="red"
stroke-width="3" fill="none"/>
</g>
</svg>

</html>
    <script>
$("#optionA").hide();
$("#optionB").hide();


$('#A').click(function(){
hidebutton()
$("#optionA").show();
$("#optionB").hide();
setTimeout(showbutton, 10000);
});

$('#B').click(function(){
hidebutton()
$("#optionB").show();
$("#optionA").hide();
setTimeout(showbutton, 10000);
});

function showbutton() {
$("#A").show();
$("#B").show();
}

function hidebutton() {
$("#A").hide();
$("#B").hide();
}
<script>

我不知道,为什么它在 Chrome 中不工作......也许你知道我应该做什么?

最佳答案

这种行为差异似乎是因为 Chrome 会在页面加载后立即启动动画。而 Firefox 和 Edge 仅在 SVG 可见时才触发动画。

我不知道哪种行为是正确的。

但是这个问题的解决方案是不将这些类添加到 <path>元素,直到您希望它们开始播放。

  1. 删除 class="obcjaApath"来自路径的属性
  2. 更改您的点击处理程序,以便它们在 SVG 可见时添加回类:

    $('#A').click(function(){
    hidebutton()
    $("#optionA").show();
    $("#optionA").addClass("opcjaApath");
    $("#optionB").hide();
    $("#optionB").removeClass("opcjaBpath");
    setTimeout(showbutton, 10000);
    });

完整演示:

$("#optionA").hide();
$("#optionB").hide();


$('#A').click(function(){
hidebutton()
$("#optionA").show();
$("#optionA").addClass("opcjaApath");
$("#optionB").hide();
$("#optionB").removeClass("opcjaBpath");
setTimeout(showbutton, 10000);
});

$('#B').click(function(){
hidebutton()
$("#optionB").show();
$("#optionB").addClass("opcjaBpath");
$("#optionA").hide();
$("#optionA").removeClass("opcjaApath");
setTimeout(showbutton, 10000);
});

function showbutton() {
$("#A").show();
$("#B").show();
}

function hidebutton() {
$("#A").hide();
$("#B").hide();
}
.opcjaApath{
-webkit-animation: dash1 10s linear forwards;
-webkit-animation-iteration-count: 1;
-moz-stroke-dasharray: 2684;
-moz-stroke-dashoffset: 2684;
-moz-animation: dash1 10s linear forwards;
-moz-animation-iteration-count: 1;
-o-stroke-dasharray: 2684;
-o-stroke-dashoffset: 2684;
-o-animation: dash1 10s linear forwards;
-o-animation-iteration-count: 1;
animation: dash1 10s linear forwards;
animation-iteration-count: 1;
stroke-dasharray: 2684;
stroke-dashoffset: 2684;
}

.opcjaBpath{
-webkit-animation: dash1 10s linear forwards;
-webkit-animation-iteration-count: 1;
-moz-animation: dash1 10s linear forwards;
-moz-animation-iteration-count: 1;
-o-animation: dash1 10s linear forwards;
-o-animation-iteration-count: 1;
animation: dash1 10s linear forwards;
animation-iteration-count: 1;
stroke-dasharray: 2684;
stroke-dashoffset: 2684;
}

@-webkit-keyframes dash1 {
0% {
-webkit-stroke-dashoffset: 2684;
}
100% {
-webkit-stroke-dashoffset: 0;
}
}

@-moz-keyframes dash1 {
0% {
-moz-stroke-dashoffset: 2684;
}
100% {
-moz-stroke-dashoffset: 0;
}
}
@-o-keyframes dash1 {
0% {
-o-stroke-dashoffset: 2684;
}
100% {
-o-stroke-dashoffset: 0;
}
}

@keyframes dash1 {
0% {
stroke-dashoffset: 2684;
}
100% {
stroke-dashoffset: 0;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<button id="A"> Option A </button>
<button id="B"> Option B </button>

<svg width="400" height="300">

<g id="optionA">
<path d="M50,30 250,30 250,54 50,54 50,78 250,78 250,102 50,102 50,126 250,126 250,150 50,150 50,174 250,174 250,198 50,198 50,222 250,222 250,246 50,246 50,270 250,270" stroke="red"
stroke-width="3" fill="none"/>
</g>
<g id="optionB">

<path d="M150,150 150,126 170,126 170,174 130,174 130,102 190,102 190,198 110,198 110,78 210,78 210,222 90,222 90,54 230,54 230,246 70,246 70,30 250,30 250,270 50,270 50,30" stroke="red"
stroke-width="3" fill="none"/>
</g>
</svg>

关于css - SVG 虚线动画在 Chrome 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56934286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com