【发布时间】:2017-03-29 16:03:44
【问题描述】:
我正在使用 Jquery 并尝试创建悬停效果。最终结果只是将鼠标悬停在 <h2> 元素上,并且应该为相应的元素内容组显示单个特征图像。然而,多个图像一次出现,而它应该只是一个图像。继承人工作pen
HTML
<p>Hover over titles</p>
<div class="content1">
<h2>Title 1 goes here </h2>
<img src="http://lorempixel.com/400/400/sports" alt="Image"/>
</div>
<div class="content2">
<h2>Title 2 goes here </h2>
<img src="http://lorempixel.com/300/300/people" alt="Image"/>
</div>
<div class="content2">
<h2>Title 3 goes here </h2>
<img src="http://lorempixel.com/200/300/technology" alt="Image"/>
</div>
CSS
img {
position: absolute;
display: none;
}
jQuery
$(document).ready(function(){
$('h2').on('mouseenter mousemove', function(evt){
$('h2,img').css({left: evt.pageX+30, top: evt.pageY-15}).show();
$(this).on('mouseleave', function(){
$('img').hide();
});
});
});
为什么会同时显示所有图像?以及如何解决它以仅在上下文中显示一张图片?
谢谢!
【问题讨论】:
-
试试
$(this).next('img').css({left: evt.pageX+30, top: evt.pageY-15}).show(); -
这也有效!谢谢
标签: javascript jquery html css image