javascript - 如何使用javascript更改鼠标悬停时的图像?

标签 javascript jquery html

<img src="http://localhost/wp-content/uploads/2018/01/sample-218x147.png" class="mvThumb" alt="" title="" id="thumb_i84bdg" style="display:block">
<img src="http://localhost/wp-content/uploads/2018/05/poqn-218x147.png" class="mvThumb" alt="" title="" id="thumb_i84xjz">
<img src="http://localhost/wp-content/uploads/2018/05/kpth-218x147.png" class="mvThumb" alt="" title="" id="thumb_i84yrh">
<img src="http://localhost/wp-content/uploads/2018/05/dtyh-218x147.png" class="mvThumb" alt="" title="" id="thumb_i84gpl">
<img src="http://localhost/wp-content/uploads/2018/05/gymr-218x147.png" class="mvThumb" alt="" title="" id="thumb_i84dzo">

是否有任何方法可以像许多视频管网站一样使用 javascript 在鼠标悬停时每秒旋转缩略图?

我有一个视频库,我有多个拇指选项,但我不知道是否可以每秒更改上面列出的图像。

最佳答案

这是一个可能的解决方案:

var i = 0;
var tid = null;
var sec = 1/3; // <- you want 1 here
var images = $("img").map(function () {
  return $(this).attr("src");
}).get();

$("img:gt(0)").remove();
$("img").hover(function () {
  var $this = $(this);
  tid = setInterval(function () {
    i = (i + 1) % images.length;
    $this.attr("src", images[i]);
  }, 1000 * sec);
}, function () {
  clearInterval(tid);
  $(this).attr("src", images[0]);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img height="150" src="/image/BDcMh.gif">
<img height="150" src="/image/vfQCT.gif">
<img height="150" src="/image/MbEgw.gif">
<img height="150" src="/image/uCCEw.gif">
<img height="150" src="/image/iO6QE.gif">

作为替代方案,您可以在静态 GIF 和动画 GIF 之间切换:

var gif = "/image/1IpaB.gif";
var agif = "/image/yYrPT.gif";

$("img").hover(function () {
  $(this).attr("src", agif);
}, function () {
  $(this).attr("src", gif);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img height="150" src="/image/1IpaB.gif">

为了制作您可以在上面的代码片段中看到的 GIF,我转换了一个 WEBP image from Youtube感谢这个在线工具:https://ezgif.com/webp-to-gif .

关于javascript - 如何使用javascript更改鼠标悬停时的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49606783/

相关文章:

video - 如何用 HTML5 <video> 播放 MPEG4 Visual?

html - 当我删除溢出 : hidden property from li{} 时背景颜色消失

javascript - 当我使用 bootstrap 4 时,为什么粘性 header 在表单标签中不起作用?

javascript - Angular 内联编辑表单例

javascript - 德鲁巴 8 : Theme's css and js files are not being loaded

javascript - 使用 Ajax 发送表单 - Spring MVC

javascript - 使用 JQLite 如何在选择框中选择一个或多个项目

javascript - 单击所有单选按钮显示 div

javascript - 如果尚未添加 HTML 元素,Jquery .click() 不起作用

php - JavaScript 与 PHP 组合