【发布时间】:2016-02-26 08:40:05
【问题描述】:
我正在制作一个简单的插件,如果我将鼠标悬停在图像上,它的背景颜色应该变为半透明黑色。
为此,我在其上方放置了一个带有类名overlay 的标签以产生效果。由于每个图像的高度和宽度都会不同,因此我从图像中获取高度/宽度并动态地将其提供给overlay 类。最后,当鼠标熄灭时,我只是让背景颜色透明。
这是代码
<div class="overlay"></div>
<a href="" class="box">
<img src="http://www.cars.co.za/images/specials/ncs-red-renault.jpg" alt="" />
</a>
(function($) {
$.fn.imageOverlay = function() {
return this.each(function() {
var $this = $(this);
var width = $this.width();
var height = $this.height();
$this.hover(function(){
$('.overlay').css({
width: width,
height: height,
backgroundColor: 'rgba(0,0,0,0.5)'
});
console.log('in');
}, function(){
$('.overlay').css({
width: 0,
height: 0,
backgroundColor: 'rgba(0,0,0,0)'
});
console.log('out');
});
});
}
}(jQuery));
(function($){
$('.box').imageOverlay();
}(jQuery));
.overlay {
position: absolute;
display: inline-block;
}
这是有效的,但在进出开始并且永不停止时不是应该的;有点循环。
有什么解决办法吗?或者是否有正确的方法来实现与插件相同的功能?
【问题讨论】:
-
不,它不一样的问题无限循环
-
这是一个更新的小提琴jsfiddle.net/hkd4fgkh/2
标签: javascript jquery html jquery-plugins position