gpt4 book ai didi

html - 在鼠标悬停时播放 Gif 并在鼠标悬停时暂停 Gif 而不替换图像?

转载 作者:太空狗 更新时间:2023-10-29 15:24:59 24 4
gpt4 key购买 nike

我正在寻找一个代码示例,允许用户在鼠标悬停时为 gif 动画并在鼠标移开时暂停。我看过很多教程都在谈论这个,但我想要不同的效果。

我注意到大多数 gif 在鼠标移开时会“重置”。也就是说,要么 gif 被通用图像覆盖,要么动画恢复到开始。我想要实现的是一个更加无缝的“暂停”,它允许您在不使用占位符图像的情况下从中断的地方开始。类似于此页面上的示例:

http://www.valhead.com/2013/03/11/animation-play-state/

请注意,当您将鼠标放在图像上时,动画是如何暂停而不替换任何内容,否则会继续播放。

我不知道 gif 是否可行,因为这个例子使用的是基本的 css 形状,但必须有一些方法可以在鼠标移开时暂停 gif 并在鼠标悬停时恢复,而不覆盖循环动画中的图像?如果没有,有没有一种方法可以使用在鼠标悬停时暂停并在鼠标悬停时从停止处播放的电影文件?

谢谢!

编辑:感谢@brbcoding 和他的天才,这个问题已经解决了!有关解决方案的详细信息可以在下面的帖子或他的博客文章中找到:http://codyhenshaw.com/blog/2013/12/17/faux-animated-gifs-with-css3-keyframes/

最佳答案

所以,我考虑了一下......你可以做一些很酷的事情:

首先,将您的 gif 分解为多个图像,然后使用 css 关键帧为它们制作动画。

#faux-gif {
position: absolute;
top: 0; left: 0; right: 0; bottom: 0;
margin: auto;
background-image: url(http://i.imgur.com/E2ee6fI.gif);
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
width: 300px;
height: 300px;
/* animation: giffy 0.5s infinite linear; */
/* no fade between frames */
animation: giffy 0.5s infinite steps(1);
}

#faux-gif:hover {
animation-play-state:paused;
}

@keyframes giffy {
0% { background-image: url('http://i.imgur.com/E2ee6fI.gif'); }
15% { background-image: url('http://i.imgur.com/JIi0uul.gif'); }
30% { background-image: url('http://i.imgur.com/owNGnNN.gif');}
45% { background-image: url('http://i.imgur.com/2Ii6XOz.gif'); }
60% { background-image: url('http://i.imgur.com/ZmQBrQ5.gif'); }
75% { background-image: url('http://i.imgur.com/iAsfHyY.gif'); }
90% { background-image: url('http://i.imgur.com/AJwRblj.gif'); }
100% { background-image: url('http://i.imgur.com/fx5wUNY.gif'); }
}

DEMO

JavaScript 版本...没有经过非常彻底的测试,但这将是基本思想。

window.onload = function() {

function FauxGif(element, frames, speed) {
this.currentFrame = 0,
this.domElement = element,
this.frames = frames || null,
this.speed = speed || 200;
this.interval;
this.init();
}

FauxGif.prototype = {
init: function() {
// set the first one to the first image
console.log(this.frames[0])
this.domElement.style.backgroundImage = "url(" + this.frames[0] + ")";
},
pause: function() {
clearInterval(this.interval);
},
resume: function() {
var that = this;

that.interval = setInterval(function(){
that.currentFrame < that.frames.length - 1 ? that.currentFrame++ : that.currentFrame = 0;
that.domElement.style.backgroundImage = "url(" + that.frames[that.currentFrame] + ")";
}, this.speed);
}
}

var frames = [
'http://i.imgur.com/E2ee6fI.gif',
'http://i.imgur.com/JIi0uul.gif',
'http://i.imgur.com/owNGnNN.gif',
'http://i.imgur.com/2Ii6XOz.gif',
'http://i.imgur.com/ZmQBrQ5.gif',
'http://i.imgur.com/iAsfHyY.gif',
'http://i.imgur.com/AJwRblj.gif',
'http://i.imgur.com/fx5wUNY.gif'
]

var elem = document.querySelector('#faux-gif'),
gif = new FauxGif(elem, frames);


elem.addEventListener('mouseenter', function(){
gif.resume()
});

elem.addEventListener('mouseleave', function() {
gif.pause();
});
}

DEMO

关于html - 在鼠标悬停时播放 Gif 并在鼠标悬停时暂停 Gif 而不替换图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20644209/

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