Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 悬停时的FadeIn背景图像_Jquery_Html_Fadein - Fatal编程技术网

Jquery 悬停时的FadeIn背景图像

Jquery 悬停时的FadeIn背景图像,jquery,html,fadein,Jquery,Html,Fadein,我正在试图弄清楚是否可以淡入一个div的背景图像,我目前有一个函数,可以在悬停状态下显示相关div的背景图像,但是显示/隐藏有点苛刻,我希望它淡入/淡出,但我不知道怎么做 这是我的密码: jQuery(document).ready(function(){ jQuery(".thumbnail").hover(function(){ var imgurl = jQuery(this).data("hoverimage"); jQuery(this).css

我正在试图弄清楚是否可以淡入一个div的背景图像,我目前有一个函数,可以在悬停状态下显示相关div的背景图像,但是显示/隐藏有点苛刻,我希望它淡入/淡出,但我不知道怎么做

这是我的密码:

jQuery(document).ready(function(){
    jQuery(".thumbnail").hover(function(){
        var imgurl = jQuery(this).data("hoverimage");
        jQuery(this).css("background-image", "url(" + imgurl + ")");
    }, function(){
        jQuery(this).css("background-image", "");
    });
});

此函数仅在mouseenter上显示背景图像,并在mouseleave上再次隐藏背景图像,是否可以在鼠标悬停在相关div上时淡入淡出,而在离开div时淡出淡出?

只能在背景图像中淡入淡出。可以做的是淡入具有相同偏移量的元素,该偏移量与背景图像相同(甚至是图像元素本身)。只需添加另一个具有
位置的元素:fixed
(如果父元素是相对的,则为absolute),该元素具有您想要的图像

$("#hasbg").append($("<div>").css({
    position: 'fixed',
    top: $("#hasbg").offset().top,
    left: $("#hasbg").offset().left,
    display: 'none',
    background: 'url(http://static.adzerk.net/Advertisers/bd294ce7ff4c43b6aad4aa4169fb819b.jpg)',
    width: $("#hasbg").width(),
    height: $("#hasbg").height()
}).attr('id', 'isbg')).on('mouseenter', function () {
    console.log($("#isbg").length);
    $("#isbg").stop().fadeIn();
}).on('mouseleave', function () {
    $("#isbg").stop().fadeOut();  
});
$(“#hasbg”).append($(“”).css({
位置:'固定',
顶部:$(“#hasbg”).offset()顶部,
左:$(“#hasbg”).offset()。左,
显示:“无”,
背景:“url”(http://static.adzerk.net/Advertisers/bd294ce7ff4c43b6aad4aa4169fb819b.jpg)',
宽度:$(“#hasbg”).width(),
高度:$(“#hasbg”)。高度()
}).attr('id','isbg'))。on('mouseenter',function(){
console.log($(“#isbg”).length);
$(“#isbg”).stop().fadeIn();
}).on('mouseleave',函数(){
$(“#isbg”).stop().fadeOut();
});

这相当棘手,我的印象是$(“.blah”).animate({})有颜色属性,但事实并非如此(至少据我所知)


您可能会在这里找到出路:
http://api.jquery.com/fadeIn/
(最后一段代码)。

您不能在
背景图像中淡入淡出。你需要在它后面放一个div。可能的重复:你可以看看MoonTools的Fx演示,这可能会给你一些启示