为了账号安全,请及时绑定邮箱和手机立即绑定

鼠标移开之后,自动切换默认是之前的,并不是鼠标之后的下一位

jQuery(document).ready(function() {
    var t = $(".dox li"),
        b = $(".dox div");
    var index = 0;
    var timer = null;
    // if (t.length != b.length) return;
    for (var i = 0; i < t.length; i++) {
        t[i].id = i;
        $(t[i]).mouseover(function() {
            var that = this;
            clearInterval(timer);
            timer = setTimeout(function() {
                for (var j = 0; j < b.length; j++) {
                    b.css("display", "none");
                    t.removeClass();
                };
                $(that).addClass('hover');
                $(b[that.id]).css("display", "block");
            }, 150);

        });
        $(t[i]).mouseout(function() {
            timer = setInterval(function() {
                index++;
                if (index >= t.length) {
                    index = 0;
                }
                for (var j = 0; j < b.length; j++) {
                    b.css("display", "none");
                    t.removeClass();
                };
                $(t[index]).addClass('hover');
                $(b[index]).css("display", "block");
            }, 2000);
        });
    };
    if (timer) {
        clearInterval(timer);
        timer = null;
    }
    timer = setInterval(function() {
        index++;
        if (index >= t.length) {
            index = 0;
        }
        for (var j = 0; j < b.length; j++) {
            b.css("display", "none");
            t.removeClass();
        };
        $(t[index]).addClass('hover');
        $(b[index]).css("display", "block");
        console.log(index);
    }, 2000);
});

正在回答

2 回答

 $(t[i]).mouseover(function() {
            var that = this;
            clearInterval(timer);
            timer = setTimeout(function() {
                for (var j = 0; j < b.length; j++) {
                    b.css("display", "none");
                    t.removeClass();
                };
                $(that).addClass('hover');
                $(b[that.id]).css("display", "block");
                index = that.id;
            }, 150);

鼠标划过标签的时候并没有修改index的值,这样当鼠标离开当前标签后index的值是上次自动播放的index+1。解决办法是在鼠标划过标签后,把index的值修改为标签的id值,这样下次自动播放时就会从划过标签的下一个标签开始。代码如上,添加的代码在倒数第二行。

0 回复 有任何疑惑可以回复我~
#1

Hakuna_Matata 提问者

非常感谢!
2016-02-26 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

鼠标移开之后,自动切换默认是之前的,并不是鼠标之后的下一位

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信