【问题标题】:Jquery mouseover image on image with a link带有链接的图像上的 Jquery 鼠标悬停图像
【发布时间】:2011-04-21 18:24:52
【问题描述】:
【问题讨论】:
标签:
jquery
image
mouseover
【解决方案3】:
您链接到的网站使用的图片位于图片顶部并带有点击事件
类似的东西
<a href="#" class="quicklink"><img src="item1.jpg" /></a>
<a href="#" class="quicklink"><img src="item2.jpg" /></a>
<a href="#" class="quicklink"><img src="item3.jpg" /></a>
...
<img src="button.jpg" id="button" style="dispaly:none;" />
使用类似的脚本:
function handler(){
//possible event for button
}
$(function(){
$('a.quicklook').mouseover(function(){
$('#button').css({position:'absolute',top:$(this).offset().top+'px',right:$(this).offset().right+'px'})
.show().bind('click',handler);
}).mouseout(function(){
$('#button').hide();
});
});
但是有更多的位置信息,因为它只是放在 a 标签的左上角(希望如此)。
我会使用类似的东西,但可能在与链接相同的容器内使用样式和链接,这样我就可以在隐藏它和添加脚本之前对其进行样式设置,如果它们仍然存在,也可以使用向后兼容的非 JavaScript 浏览器(我知道你可以随时关闭它)
【解决方案4】:
最简单的方法是使用 CSS。
HTML:
<div class="product">
<img src="product.jpg" alt="a product" />
<a class="buy-button" href="javascript:alert('bought');">Buy now</a>
</div>
CSS:
.product .buy-button { display: none; }
.product:hover .buy-button { display: inline; }
现在这在旧版本的 IE 中不起作用。如果这是一个问题,您可以使用 jQuery 添加一个类,并在 CSS 中使用它。
JS:
$('.product').hover(
function(){ $(this).addClass('hover'); },
function(){ $(this).removeClass('hover'); }
);
CSS:
.product.hover .buy-button { display: inline; }