Losing hover when animating with jQuery (without moving mouse)

好久不见. 提交于 2019-12-01 04:11:06

I only worked on the top list but I think I got it all working. let me know if it is what you are looking for.

Here is the fiddler

var idx = 0;
$('#list-1 li').hover(function() {
    $(this).addClass('hover');
}, function() {
    $(this).removeClass('hover');
}).click

(function() {
    var currentindex = $('.active').index();
    var selectedindex = $(this).index();
    var nexthoverindex = selectedindex + (selectedindex - currentindex);


//counter for starting on index 1
if(currentindex === 1 && selectedindex > 2){
    nexthoverindex = nexthoverindex - 1;
}

//counter for starting on index 0
if(currentindex === 0 && selectedindex > 2){
    nexthoverindex = nexthoverindex - 2;
}

//make sure the selection never goes below 0
if(nexthoverindex < 0){
    nexthoverindex = 0;
}

$('#list-1 > li').removeClass('active');
$(this).addClass('active');
var id = $('#list-1 li.active').data('index') - 2; // take scroll param and subtract 2 to keep selected image in the middle
idy = Math.max(0, id * 90);
$(this).parent().animate({
    'top': -idy + 'px'
},200, function(){           
    $('.hover').removeClass('hover');
    if(currentindex > 2 || selectedindex > 2){
    $('#list-1 > li').eq(nexthoverindex).addClass('hover');
    }
});
return false;
}).css('cursor', 'pointer').each(function() {
    $(this).data('index', idx);
    ++idx;
});

I've got a solution that works in Chrome and IE (haven't tested in Safari). Basically I trigger the mouseover() event of the element under the mouse in the animate() callback event if the thumbnails have moved. The solution is only implemented for list-1.

// list 1
var idx = 0;
$('#list-1 li').hover(function() {
    $(this).addClass('hover');
}, function() {
    $(this).removeClass('hover');
}).click(function() {
    $('#list-1 > li').removeClass('active');
    var $active = $(this);
    $active.addClass('active');
    var id = $('#list-1 li.active').data('index') - 2; // take scroll param and subtract 2 to keep selected image in the middle

    var moveAmount = 90;
    idy = Math.max(0, id * moveAmount);
    var oldPos = $active.parent().position().top;

    $active.parent().animate({
        'top': -idy + 'px'
    }, function(){

        var newPos = $(this).position().top;

        // Check if we moved
        if(oldPos - newPos != 0)
        {
            var movement = (oldPos - newPos) / moveAmount;
            $($(this).children()[$active.index() + movement])
                .trigger("mouseover");

        }

    });
    return false;
}).css('cursor', 'pointer').each(function() {
    $(this).data('index', idx);
    ++idx;
});

And here's the link to my fork in jsfiddle if you wan't to test it out over there - http://jsfiddle.net/jimmysv/3tzAt/15/

hellodally

Hover binds mouseEnter() and mouseLeave() to the object. I think you'll have better luck with mouseOver() in this case, though I haven't tried it.

What is the difference between the mouseover and mouseenter events?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!