如何在链接上使用动画gif并在悬停和重置时播放它

时间:2015-01-03 06:52:13

标签: css html5 animation reset

首先感谢这个页面,它一直在帮助我!但在这一点上,我有一个问题,我无法找到符合我想要的答案(也许它无法以我的方式实现)。

我希望有一个静态图像的链接,当用户将光标移动到链接上时,我想要一个动画gif播放(动画gif设置为不循环,所以它只播放一次)。当用户移出时返回静态图像,如果用户再次进入,则gif应该从头开始播放。

我使用html5结合CSS来创建我的网页(我正在用它同时学习)。我过去使用C ++和类似程序编程,但从未在Web环境中编程。

到目前为止,这是我尝试过的:

CSS:

.img-acu
{
float: left;
width: 450px;
height: 264px;
background:transparent url("acu.gif") center top no-repeat;
}

.img-acu:hover
{
background-image: url("acusel.gif");
}

HTML:

<a href="../example.html" class="img-acu" title="ACU Project link"></a>

但什么都没有出现:( 奇怪的是,我使用了两个静态图像(png格式)的相同示例,它工作正常,但由于某些原因,动画GIF它不想工作。

我试过这个:

CSS:

#test
{
width: 450px;
height: 264px;
background-image: url("acu.gif");
background-repeat: no-repeat;
background-position: left;
margin-left: 75px;
}

#test:hover
{
background-image: url("acusel.gif");
}

HTML:

<div id="test"></div>

这很有效,只是链接不起作用,当动画gif到达最后一帧时,它永远不会重置(除非我重新加载页面)。

您知道在HTML5 + CSS中是否有任何方法可以正确实现这一点?我应该使用javascript还是php?

我真的很感激任何帮助!

非常感谢!!

4 个答案:

答案 0 :(得分:1)

这可以通过使用静态图像和你的gif图像来实现(嘿,9gag怎么做!)

基本脚本可能是这样的:

<img id="myImg" src="staticImg.png" />

<script>
    $(function() {
        $("#myImg").hover(
            function() {
                $(this).attr("src", "animatedImg.gif");
            },
            function() {
                $(this).attr("src", "staticImg.gif");
            }                         
        );                  
    });
</script>

答案 1 :(得分:1)

答案 2 :(得分:0)

如果你可以使用canvas,请试试这个:

var lst = new List<string>();
var dic = new Dictionary<int, int>();
string s = "Hello!";
object obj1 = new { Id = 10 };
object obj2 = null;

// True
Console.Write(lst.GetType().IsGenericType && lst is IEnumerable);
// True
Console.Write(dic.GetType().IsGenericType && dic is IEnumerable);
// False
Console.Write(s.GetType().IsGenericType && s is IEnumerable);
// False
Console.Write(obj1.GetType().IsGenericType && obj1 is IEnumerable);
// NullReferenceException: Object reference not set to an instance of 
// an object, so you need to check for null cases too
Console.Write(obj2.GetType().IsGenericType && obj2 is IEnumerable);

答案 3 :(得分:0)