【发布时间】:2016-11-21 18:32:56
【问题描述】:
我有 3 张图片正在为其创建效果,将鼠标悬停在其中时,图片会切换到不同的图片。当尝试使用 .animate() 方法为过渡添加持续时间时,整个效果被禁用。有没有比使用 .animate() 更好的方法,或者我的代码中是否存在阻止 .animate() 工作的问题?
干杯
HTML
<img class="navbarimg" data-alt-src="images/icons/facebookhover.png" src="images/icons/facebook.png" alt="Facebook Page">
<img class="navbarimg" data-alt-src="images/icons/instagramhover.png" src="images/icons/instagram.png" alt="Instagram Page">
<img class="navbarimg" data-alt-src="images/icons/githubhover.png" src="images/icons/github.png" alt="Github Page">
jQuery
/* Hover effect for the nav bar */
/* Section A - Function to make the alt src be used instead of the default source */
var sourceSwap = function () {
var $this = $(this);
var newSource = $this.data('alt-src');
$this.data('alt-src', $this.attr('src'));
$this.attr('src', newSource);
}
/* Section B - Runs the function to switch the src's */
$(function () {
/* Finds images wiht the 'navbarimg' class, and runs the function for those images */
$('img.navbarimg').hover(sourceSwap, sourceSwap);
});
【问题讨论】:
-
我觉得你做不到,你在看什么样的动画?你可以有不透明的东西
-
我会尝试更好地解释它,但基本上,当用户将鼠标悬停在图像上时,第一张图像会在一段时间内过渡到第二张图像。
标签: javascript jquery html css image