代码之家  ›  专栏  ›  技术社区  ›  Federico

加速滚动文本

  •  1
  • Federico  · 技术社区  · 7 年前

    我有一个从右到左滚动文本的函数。 我想加速滚动,我试着修改 1/100 但一切都没变。


    先谢谢你。

    $(document).ready(function() {
        var marquee = $('div.marquee');
        console.log(marquee);
        marquee.each(function() {
            var mar = $(this),indent = mar.width();
            mar.marquee = function() {
                indent--;
                mar.css('text-indent',indent);
                if (indent < -1 * mar.children('p').width()) {
                    indent = mar.width();
                }
            };
            mar.data('interval',setInterval(mar.marquee,1/100));
        });
    });
    .marquee {
        position:fixed;
        top:0;
        right:0;
        padding:10px 0;
        height:1.2em;
        width:100vw;
        font-family:'super';
    }
    .marquee p {
    	display:inline;
        white-space:nowrap;
        color:#000;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div class=marquee>
    <p>1 2 3 4 5 6 7 8 9 0</p>
    </div>
    1 回复  |  直到 7 年前
        1
  •  1
  •   Adrian Pop    7 年前

    改变字幕移动速度的是 indent-- indent = indent - K

    下面是一个小例子。我加了两个按钮来帮助你控制速度。希望有帮助!干杯!

    var marqueeSpeed = 2;
    
    $(document).ready(function() {
        var marquee = $('div.marquee');
        marquee.each(function() {
            var mar = $(this),indent = mar.width();
            mar.marquee = function() {
                indent = indent - marqueeSpeed;
                mar.css('text-indent',indent);
                if (indent < -1 * mar.children('p').width()) {
                    indent = mar.width();
                }
            };
            
            mar.data('interval', setInterval(mar.marquee, 1/100));
        });
    });
    
    var increase = function() {
        ++marqueeSpeed;
    }
    
    var decrease = function() {
        if (marqueeSpeed > 0) {
            --marqueeSpeed;
        }
    }
    .marquee {
        position:fixed;
        top:0;
        right:0;
        padding:10px 0;
        height:1.2em;
        width:100vw;
        font-family:'super';
    }
    .marquee p {
    	display:inline;
        white-space:nowrap;
        color:#000;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div class=marquee>
    <p>1 2 3 4 5 6 7 8 9 0</p>
    </div>
    
    <br>
    <br>
    <button onclick="increase()">Increase speed</button>
    <button onclick="decrease()">Decrease speed</button>