使用 jQuery 制作动画时失去悬停(不移动鼠标)

发布于 2024-10-16 10:13:35 字数 2780 浏览 2 评论 0原文

我有这行缩略图,正在使用 jQuery 制作动画。
每个缩略图都有一个悬停和活动类。

它们工作正常,但是当我对列表进行动画处理时,鼠标光标下的新缩略图不应用悬停?每次点击后我都必须稍微移动鼠标吗?

这有点难以解释..我在这里做了一个小提琴: http://jsfiddle.net/nZGYA/
当您在拇指 3 之后开始单击而不移动鼠标时,您就会明白我的意思...

它在 FireFox 中工作正常,而不是 Safari、Chrome、IE 等。
我能做点什么吗?

这里是我的代码供参考:

<style type="text/css">
    .container { position: relative; overflow: hidden; width: 140px; height: 460px; float: left; margin-right: 100px; background: silver;  }            
    ul { position: absolute; top: 10; list-style: none; margin: 10px; padding: 0; }
    li { margin-bottom: 10px; width: 120px; height: 80px; background: gray; }
    #list-2 li a { display: block; width: 120px; height: 80px; outline: none; }
    #list-2 li a:hover { background: teal; }
    #list-2 li a.active { background: navy; }
</style>

$(document).ready(function() {
    var idx_2 = 0;
    $('#list-2 li a')
    .click(function() {
        $('#list-2 > li a').removeClass('active');
        $(this).addClass('active');         
        var id =  $('#list-2 li a.active').data('index') - 2;
        idy = Math.max(0, id * 90);
        $(this).parent().parent().animate({ 'top' : -idy + 'px' });
        return false;
    })
    .each(function() {
        $(this).data('index', idx_2);
        ++idx_2;
    });
});

<div class="container">
    <ul id="list-2">
        <li><a class="active" href="#"></a></li>
        <li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li>
        <li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li>
        <li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li>
        <li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li>
        <li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li>
        <li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li>
    </ul>
</div>

I have this row of thumbnails that I am animating with jQuery.
Each of these thumbnails has a hover and active class.

They work fine but when I animate the list, the new thumbnail under the mousecursor does not apply the hover? I have to move the mouse a little bit after each click?

It's kinda difficult to exaplain.. I have made a fiddle here: http://jsfiddle.net/nZGYA/
When you start clicking after thumb 3 without moving the mouse you see what I mean...

It works fine in FireFox, NOT Safari, Chrome, IE etc.
Is there something I can do about this?

For reference here is my code:

<style type="text/css">
    .container { position: relative; overflow: hidden; width: 140px; height: 460px; float: left; margin-right: 100px; background: silver;  }            
    ul { position: absolute; top: 10; list-style: none; margin: 10px; padding: 0; }
    li { margin-bottom: 10px; width: 120px; height: 80px; background: gray; }
    #list-2 li a { display: block; width: 120px; height: 80px; outline: none; }
    #list-2 li a:hover { background: teal; }
    #list-2 li a.active { background: navy; }
</style>

$(document).ready(function() {
    var idx_2 = 0;
    $('#list-2 li a')
    .click(function() {
        $('#list-2 > li a').removeClass('active');
        $(this).addClass('active');         
        var id =  $('#list-2 li a.active').data('index') - 2;
        idy = Math.max(0, id * 90);
        $(this).parent().parent().animate({ 'top' : -idy + 'px' });
        return false;
    })
    .each(function() {
        $(this).data('index', idx_2);
        ++idx_2;
    });
});

<div class="container">
    <ul id="list-2">
        <li><a class="active" href="#"></a></li>
        <li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li>
        <li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li>
        <li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li>
        <li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li>
        <li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li>
        <li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li><li><a href="#"></a></li>
    </ul>
</div>

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

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

发布评论

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

评论(2

七颜 2024-10-23 10:13:35

我只在最上面的列表上工作过,但我想我已经全部完成了。让我知道这是否是您正在寻找的。

这是 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 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;
});
故人如初 2024-10-23 10:13:35

我有一个适用于 Chrome 和 IE 的解决方案(尚未在 Safari 中测试)。基本上,如果缩略图已移动,我会在 animate() 回调事件中触发鼠标下方元素的 mouseover() 事件。该解决方案仅针对 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;
});

如果你不想在那里测试的话,这里是我在 jsfiddle 中的分支的链接 - http:// /jsfiddle.net/jimmysv/3tzAt/15/

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/

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