首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery环形滚动环

问jQuery环形滚动环
EN

Stack Overflow用户
提问于 2011-12-11 20:30:19
回答 1查看 3.2K关注 0票数 2

我有一个关于jsFiddle的项目:

jsFiddle链路

JavaScript:

代码语言:javascript
复制
var scroller = function() {
    $('#holder div').animate({
        left: ($t.attr('id') == 'prev' ? '-=' : '+=') + 3
    }, 10, 'linear', function() {
        if ($(this).position().left < -$('li:first-child', this).width()) {
            w = $('li:first-child', this).totalWidth();
            $('li:first-child', this).appendTo('ul', this);
            $(this).css('left', $(this).position().left + w);
        }
    });
};

$.fn.extend({
    totalWidth: function() {
        return this.outerWidth() + parseInt(this.css('margin-left'), 10) + parseInt(this.css('margin-right'), 10);
    }
});
wdth = 0;
$('#marquee ul li').each(function() {
    wdth += $(this).totalWidth();
});
$('#holder div').width(wdth);
var to;
$('#prev, #next').css('top', ($('#marquee').outerHeight() - $('#prev').outerHeight()) / 2).live('mousedown mouseup', function(e) {
    $t = $(this);
    if (e.type == 'mousedown') {
        to = setInterval(scroller, 15);
    }
    else {
        clearInterval(to);
    }
});

HTML:

代码语言:javascript
复制
<div id="marquee">
    <div id="prev"><</div>
    <div id="next">></div>
    <div id="holder">
        <div>
        <ul>
            <li>Part 1</li>
            <li>Part 2</li>
            <li>Part 3</li>
            <li>Part 4</li>
            <li>Part 5</li>
            <li>Part 6</li>
            <li>Part 7</li>
            <li>Part 8</li>
            <li>Part 9</li>
            <li>Part 10</li>
        </ul>
    </div>
    </div>
</div>

CSS:

代码语言:javascript
复制
* {
    font-family: verdana;
    font-size: 12px;
}
#marquee {
    top: 50px;
    position: relative;
    width: 80%;
    height: 34px;
    background-color: #CCC;
    margin: 0 auto;
    padding: 0;
}
#holder {
    overflow: hidden;
position: absolute;
    left: 5px;
    right: 5px;
    top: 5px;
    bottom: 5px;
}
#holder div {
    position: absolute;
}
#marquee ul li {
    display: inline-block;
    float: left;
    margin-left: 5px;
    padding: 5px 7px;
    background-color: #555;
}
#marquee ul li:nth-child(2n+1) {
background-color: #888;
}
#marquee ul li:first-child {
    margin-left: 0;
}
#prev, #next {
    position: absolute;
    top: 10px;
    background-color: #66F;
    padding: 2px;
    cursor: pointer;
    z-index: 9002;
}
#prev {
    left: -13px;
    border-radius: 5px 0 0 5px;
}
#next {
    right : -13px;
    border-radius: 0 5px 5px 0;
}

我想要实现的是鼠标向下滚动循环,鼠标向上停止。

我已经能够使它滚动,并循环,但它“跳跃”在每一个循环的变化。

有人有什么想法吗?

我已经编辑了小提琴以删除CSS规则,也稍微编辑了代码。

最初,当它向左滚动时,它向后跳了大约20 by,而CSS规则使它看起来更糟了!

另外,动画以10毫秒的速度进行,但它每15毫秒循环一次,因为在鼠标向上时,循环会持续一段时间。

右边不循环,因为我不知道怎么做左边,所以我不会浪费时间让它在准备好的时候正确地向右滚动。

我不使用插件,因为我想学习如何自己做(顽固!)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-17 06:21:21

分叉你的小提琴,增加代码a),以消除滚动时的跳跃;( b)检查并添加第一个<li>到列表的末尾(如果有空格),同时向右滚动(下一个)

看看这把小提琴:http://jsfiddle.net/CRy4Q/15/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8467216

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档