javascript - 使用CSS向javascript添加动画

标签 javascript html css

我正在使用以下代码使用 css 将动画添加到幻灯片。但是动画仅应用于第一张图像,而不应用于其他图像。如何仅使用 css 将过渡添加到其他图像?

<html>
    <style>
        #slideshow{width:310;height:210;border-style:solid;}
        #imge{
            position:absolute;left:15;top:15;
            animation:myfirst 3s;}
        @keyframes myfirst
        {
            from {width:0;}
        to {width:300;}
        }

        @-webkit-keyframes myfirst /* Safari and Chrome */
        {
            from {width:0;}
        to {width:300;}
        }

    </style>
    <body>
        <div id="slideshow">
            <img id="imge" src="img1.jpg" height="200" width="300">
        </div>
        <script>
            var count=1;
            mf();
            function mf()
            {
                document.getElementById("imge").src="img"+count+".jpg";

                if(count<3)
                    count++;
                else
                    count=1;
                setTimeout("mf()",3000);
            }

        </script>
    </body>
</html>

最佳答案

使用CSS3动画属性animation-iteration-count

指定动画播放的次数。默认1,所以取无限

试试这个代码:

Fiddle :

#slideshow{width:310px;height:210px;border-style:solid;}
#imge{
position:absolute;left:15;top:15;
width:310px;height:210px;
animation:myfirst 3s;
animation-iteration-count:infinite;
-webkit-animation:myfirst 5s; /* Safari and Chrome */
-webkit-animation-iteration-count:infinite;}

@keyframes myfirst
{
from {width:0px;}
to {width:300px;}
}

@-webkit-keyframes myfirst /* Safari and Chrome */
{
from {width:0px;}
to {width:300px;}
}

JS:

var count=1;
mf();
function mf()
{
document.getElementById("imge").src="img"+count+".jpg";

if(count<3)
count++;
else
count=1;
setTimeout("mf()",3000);
}

关于javascript - 使用CSS向javascript添加动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21012120/

相关文章:

javascript - 我的悬停弹出窗口在不应该出现的情况下闪烁和移动

javascript - 通过figure id访问figcaption和img

javascript - 在按钮改变高度后使标题保持不变

css - 电子邮件中的格式或样式字符串

html - 如何在导航栏中对齐图标

javascript - Ace Editor 在对话框内的光标位置错误

javascript - if...else 语句

asp.net - 从 Outlook 中的网页发送文件

html - 使用 CSS 隐藏非 anchor 元素

javascript - 是否可以以编程方式检测数据 url 的大小限制?