如何使用jquery直接淡出到另一个图像(没有任何延迟)?

发布于 2024-12-02 18:04:13 字数 680 浏览 10 评论 0原文

我的代码可以工作,但它不会直接淡入第二个图像:淡出第一个图像 ->延迟->在第二个中消失。如何直接淡入第二张图像?

我的 jquery 代码

$(document).ready(function() {
    var std = $(".fadeim").attr("src");
    var hover = std.replace(".png", "-hover.png");
    $(".fadeim")
        .mouseover(function() {   
            $(this).fadeOut("fast",function(){  
                $(this).attr("src", hover);  
                $(this).fadeIn("fast");  
            });  
        })  
        .mouseout(function() {  
            $(this).fadeOut("fast",function(){  
                $(this).attr("src", std);  
                $(this).fadeIn("fast");           
            });  
        })  
    }); 
});

My code works but it doesn't fade to second image directly: fades out first image -> delay -> fades in second one. How do I fade directly to second image?

My jquery code

$(document).ready(function() {
    var std = $(".fadeim").attr("src");
    var hover = std.replace(".png", "-hover.png");
    $(".fadeim")
        .mouseover(function() {   
            $(this).fadeOut("fast",function(){  
                $(this).attr("src", hover);  
                $(this).fadeIn("fast");  
            });  
        })  
        .mouseout(function() {  
            $(this).fadeOut("fast",function(){  
                $(this).attr("src", std);  
                $(this).fadeIn("fast");           
            });  
        })  
    }); 
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尤怨 2024-12-09 18:04:13

您可以执行以下操作[我通常做的事情]:

您可以查看我的测试链接:http://jsfiddle。 net/fJFmx/1/

我为图像设置了一个容器[固定宽度和高度]。将图像的样式设置为“位置:绝对”并具有固定尺寸。

HTML 示例:

<div id='slideContainer'>
    <img src='http://www.ct4me.net/images/dmbtest.gif' />
    <img src='http://pievscake.com/images/test.jpg' />
</div>

然后是 Jquery:

$(function() {

    $('#slideContainer img:first').fadeIn();

    $('#slideContainer').hover(function() {
        $('#slideContainer img:first').fadeOut();
        $('#slideContainer img:last').fadeIn();
    }, function() {
        $('#slideContainer img:first').fadeIn();
        $('#slideContainer img:last').fadeOut();
    });

});

CSS

#slideContainer {border:1px solid #ffcc00;width:50px; height:50px;}
#slideContainer img {position:absolute; width:50px; height:50px; display:none;}

希望这会有所帮助。

You can do the following [what i normally do]:

You can check out my testing link : http://jsfiddle.net/fJFmx/1/

I set a container [fixed width&height] for images. Set the style of for the images to position:absolute and with fixed dimensions.

HTML example:

<div id='slideContainer'>
    <img src='http://www.ct4me.net/images/dmbtest.gif' />
    <img src='http://pievscake.com/images/test.jpg' />
</div>

Then the Jquery:

$(function() {

    $('#slideContainer img:first').fadeIn();

    $('#slideContainer').hover(function() {
        $('#slideContainer img:first').fadeOut();
        $('#slideContainer img:last').fadeIn();
    }, function() {
        $('#slideContainer img:first').fadeIn();
        $('#slideContainer img:last').fadeOut();
    });

});

CSS

#slideContainer {border:1px solid #ffcc00;width:50px; height:50px;}
#slideContainer img {position:absolute; width:50px; height:50px; display:none;}

Hope this helps.

表情可笑 2024-12-09 18:04:13

您可能想使用 CSS3 来完成您想要做的事情,因为如果有人关闭了 JavaScript,图像将不会改变。试试这个CSS:

.fadeim {
    width: 200px;
    height: 100px;
    background: url(std.png) center center no-repeat;
    -webkit-transition: background 500ms linear;
    -moz-transition: background 500ms linear;
    transition: background 500ms linear;
}

.fadeim:hover {
    background: url(std-hover.png) center center no-repeat;
}

上面的代码假设你的图像是200×100px;您应该将宽度和高度设置更改为与图像相同的尺寸。因此,如果您打开应用了此 CSS 的页面,您将看到该图像,并且当您将鼠标悬停时,该图像应淡入另一个页面。目前,此功能仅适用于 WebKit(Chrome 和 Safari)和 Mozilla(Firefox),因此如果有人使用不受支持的浏览器,页面的行为将完全相同,但图像会立即发生变化而不会褪色。

广告@m

You might want to use CSS3 for what you're trying to do instead, as the image will not change if someone has JavaScript turned off. Try this CSS:

.fadeim {
    width: 200px;
    height: 100px;
    background: url(std.png) center center no-repeat;
    -webkit-transition: background 500ms linear;
    -moz-transition: background 500ms linear;
    transition: background 500ms linear;
}

.fadeim:hover {
    background: url(std-hover.png) center center no-repeat;
}

The above code assumes your image is 200×100px; you should change the width and height settings to the same size as the image. So if you open a page with this CSS applied, you'll see the image, and when you hover, the image should fade into the other one. This currently only works in WebKit (Chrome and Safari) and Mozilla (Firefox), so if someone uses an unsupported browser, the page will act exactly the same, but the image will immediately change without fading.

Ad@m

¢蛋碎的人ぎ生 2024-12-09 18:04:13

您需要将两个元素放在一起(通过 CSS 绝对/相对定位),然后您将能够降低顶部元素的不透明度,使其看起来像是淡入底部元素。

淡出完成后,快速切换图像的SRC属性(用户不会注意到)并准备另一个周期。

我建议使用以下 HTML(因为您只在一处设置宽度和高度,子级将适应父级尺寸):

<div id='slideContainer' style='width:50px; height:50px; position: relative;'>
     <img src='hover.jpg' style='position:absolute; top:0px; right:0px; bottom:0px; left: 0px; display:none;' />
     <img src='original.jpg' style='position:absolute; top:0px; right:0px; bottom:0px; left: 0px; display:none;' />
</div>

You need to place two elements over one another (via CSS absolute/relative positioning), then you will be able to decrease the opacity of the top one to make it look like it is fading into the bottom one.

After the fadeout is complete, quickly switch the SRC atributes of the images (user won't notice) and prepare for another cycle.

I suggest this HTML (because you only set width&height in one place and children will adapt to the parent size):

<div id='slideContainer' style='width:50px; height:50px; position: relative;'>
     <img src='hover.jpg' style='position:absolute; top:0px; right:0px; bottom:0px; left: 0px; display:none;' />
     <img src='original.jpg' style='position:absolute; top:0px; right:0px; bottom:0px; left: 0px; display:none;' />
</div>
萌化 2024-12-09 18:04:13

当您将鼠标悬停在“第一个”图像时,第二个图像尚未加载。

尝试以下方式:

$('.fadeim').each(function() {

    var std = $(this).attr("src");
    var hover = std.replace(".png", "-hover.png");  

    $(this).clone().insertAfter(this).attr('src', hover).removeClass('fadeim').siblings().css({
        zIndex: '1',
        position:'absolute'
    });
    $(this).mouseenter(function() {
        $(this).stop().fadeTo(600, 0);
    }).mouseleave(function() {
        $(this).stop().fadeTo(600, 1);
    });

});

FIDDLE DEMO

While you hover the 'first' image, the second one is NOT yet loaded.

Try this way:

$('.fadeim').each(function() {

    var std = $(this).attr("src");
    var hover = std.replace(".png", "-hover.png");  

    $(this).clone().insertAfter(this).attr('src', hover).removeClass('fadeim').siblings().css({
        zIndex: '1',
        position:'absolute'
    });
    $(this).mouseenter(function() {
        $(this).stop().fadeTo(600, 0);
    }).mouseleave(function() {
        $(this).stop().fadeTo(600, 1);
    });

});

FIDDLE DEMO

你在我安 2024-12-09 18:04:13

解决问题的方法:

像Rok Kralj所说的那样预加载第二张图片,不要在需要时加载它。

否则你将不得不等待浏览器从服务器获取它

更好的方法

将一张图片设置为src,另一张设置为parentNode的背景图像。仅对图像进行动画处理。

因为用户不需要空白,他们只需要改造以获得更好的体验

The way to solve your problem:

pre load the second picture like Rok Kralj said,don't load it the second you need it.

otherwise you would have to wait for the browser fetching it from server

A better way

set one picture as src,other one as parentNode's background-image. only animate the image.

Because users don't need the blank gap,they just need the transforming for better experience

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文