【问题标题】:Changing image on hover using Jquery?使用 Jquery 更改悬停图像?
【发布时间】:2014-09-12 01:35:33
【问题描述】:

我在图像滑块上有一个箭头图标,当我将鼠标悬停在图像上时,我只想改变图像。我目前一直在玩各种 jquery,但似乎没有任何效果。我不需要任何花哨的东西,例如淡出,只需在悬停时更改图像。

HTML

<div class="next"><img src="next.png"></div>
<div class="prev"><img src="prev.png"></div>

CSS

.next img{
position: absolute;
height: 50px;
width: 50px;
top: 315px;
right: 570px;
z-index: 99;
}

.prev img{
position: absolute;
height: 50px;
width: 50px;
top: 315px;
left: 550px;
z-index: 99;

}

jQuery

$('.next').hover(function(){
$(this).attr('src','prev.png');
},function(){
 $(this).attr('src', 'next.png'); 
});

【问题讨论】:

标签: jquery html css


【解决方案1】:

这没有javascript:

<div class="next"></div>
<div class="prev"></div>

.next {
  background: url(next.png) no-repeat;
}
.next:hover {
  background: url(prev.png) no-repeat;
}

.prev {
  background: url(prev.png) no-repeat;
}
.prev:hover {
  background: url(next.png) no-repeat;
}

【讨论】:

  • 其他然后将其全部保存在 css 中并避免使用 jquery,不使用 jquery 有什么好处。我只是想知道未来?
  • 当然 - CSS 是“静态”声明,而不是像 js/jquery 那样解释的“动态”。它很快
【解决方案2】:

nextdiv 而不是img 所以你需要在next 中找到img 然后更改它的src

$('.next').hover(function () {
    $(this).find('img').attr('src', 'prev.png');
}, function () {
    $(this).find('img').attr('src', 'next.png');
});

演示:Fiddle

【讨论】:

  • 啊哈)))总是为新的悬停事件找到图像)))
猜你喜欢
  • 1970-01-01
  • 2015-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 2013-08-07
  • 2010-11-26
  • 1970-01-01
相关资源
最近更新 更多