简体   繁体   English

jQuery悬停,具有背景图像过渡/缓和效果

[英]jQuery hover with background image transition/ease effect

I'm wondering and have searched all around and can't find the answer. 我想知道并已四处搜寻,找不到答案。 Right now I'm changing a background image using jQuery, on hover. 现在,我正在悬停时使用jQuery更改背景图片。 Is it possible to add a transition time in it? 是否可以在其中添加过渡时间? Like a fade? 像褪色一样? ie8 compatible? ie8兼容吗?

Here's the code: 这是代码:

$('.logo').hover(function() {
    $(this).css("background","url('url') no-repeat"); 
},function() {
    $(this).css("background","url('url_hover') no-repeat"); 
});

Since the background image source isn't a property of gradation, maybe make two mock backgrounds positioned behind all elements and fade one out on hover to reveal the one behind it. 由于背景图像源不是渐变的属性,因此可以在所有元素后面放置两个模拟背景,并在悬停时淡出一个背景,以显示出其背后的背景。

You could do something like this - jsFiddle Demo 你可以做这样的事情-jsFiddle演示

HTML 的HTML

<div class="bgFadeToggle">
    <h1>Hello World</h1>
    <div class="bg"><img src="image-1.jpg" /></div>
    <div class="bg"><img src="image-2.jpg" /></div>
</div>

JS JS

$('.bgFadeToggle').hover(function(){
    $(this).find('.bg:first').stop(true).fadeToggle();
});

CSS 的CSS

.bgFadeToggle {
    position: relative;
    z-index: 9999;
}

.bgFadeToggle .bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

.bgFadeToggle .bg  {
    z-index: -9998;
}

.bgFadeToggle .bg + .bg {
    z-index: -9999;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM