简体   繁体   English

jQuery图像淡出animate()和多个鼠标事件

[英]jquery image fade animate() and multiple mouse events

Fairly new to JavaScript and very new to jQuery. 对JavaScript来说是相当新的东西,对jQuery来说还很新。 Can someone have a look at the following code and see where I am going wrong. 有人可以看看下面的代码,看看我要去哪里了。

This is the main part of the jQuery code: 这是jQuery代码的主要部分:

$(document).on("hover", ".crrightcontainer img", function() { /* trigger event on hover on an img in class crrightcontainer */
    var src = $(this).attr('src'); // get full path and filename from thumbnail
    var index = src.lastIndexOf('/') + 1; // get index to last occurrenace of file path delimiter "/"
    var fullsizeimgname = src.substr(index); // get actual filename only i.e. "cs1.jpg"
    fullsizeimgname = "/painted/fp-images/" + fullsizeimgname; // add path to give full path to the full sized image.
    $('.crleftcontainer img').animate({opacity: 0.0},1000); // fade out old full size image
    $('.crleftcontainer img').attr("src",fullsizeimgname).animate({opacity: 1.0},1000); // set full size image in browser and fade in
 });

http://jsfiddle.net/deanflyer/CfxyJ/1 http://jsfiddle.net/deanflyer/CfxyJ/1

It works, it just seems to fire off multiple mouse events. 它有效,似乎可以触发多个鼠标事件。 Just move the mouse a few times over the thumbnail images and youll see what I mean, giving multiple fades. 只需将鼠标移到缩略图上几次,您就会明白我的意思了,并提供了多种渐变效果。

I've tried using .stop() on the main image with animate() but this just stops everything. 我试过在主图像上使用.stop()和animate(),但这只会停止所有操作。

Many Thanks. 非常感谢。

Is something like this your requirement try this: http://jsfiddle.net/CfxyJ/13/ 是这样的,您的要求请尝试以下方法: http : //jsfiddle.net/CfxyJ/13/

    $('.crrightcontainer img').css('opacity', 0.7); 

$('.crrightcontainer img').mouseenter(function () { 
    $(this).stop().animate({opacity: 1.0}, 600);
    var src = $(this).attr('src'); 
    var index = src.lastIndexOf('/') + 1; 
    var fullsizeimgname = src.substr(index);
    fullsizeimgname = "http://thepaintedtree.co.uk/fp-images/" + fullsizeimgname;  
    $('.crleftcontainer img').fadeOut('slow', function(){
        $('.crleftcontainer img').attr("src", fullsizeimgname).fadeIn('slow');
    });
});

$('.crrightcontainer img').mouseleave(function () { //fadeout
    $(this).stop().animate({
        opacity: 0.7
    }, 600);

});

Try using mouseenter event. 尝试使用mouseenter事件。 See updated fiddle http://jsfiddle.net/CfxyJ/25/ . 参见更新的小提琴http://jsfiddle.net/CfxyJ/25/ Is it what you are looking for? 是您要找的东西吗?

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

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