Javascript - 设置&控制滚动速度

时间:2011-07-29 09:57:12

标签: javascript scripting

一直致力于在我的网站上实施滚动新闻自动收报机。用JS控制滚动功能。我正在使用的代码如下,任何人都可以告诉我如何设置和滚动自动收报机滚动的速度?

$(function() {

    //cache the ticker to improve the performance of the page.  
    var ticker = $("#ticker");

    //wrap dt:dd pairs in divs to allow us to smooth our the animations
    ticker.children().filter("dt").each(function() {

      var dt = $(this),
        container = $("<div>");

      dt.next().appendTo(container);
      dt.prependTo(container);

      container.appendTo(ticker);
    });

    //hide the scrollbar
    ticker.css("overflow", "hidden");

    //animator function
    function animator(currentItem) {

      //work out new anim duration
      var distance = currentItem.height();
        duration = (distance + parseInt(currentItem.css("marginTop"))) / 0.025;

      //animate the first child of the ticker
      currentItem.animate({ marginTop: -distance }, duration, "linear", function() {

        //move current item to the bottom
        currentItem.appendTo(currentItem.parent()).css("marginTop", 0);

        //recurse
        animator(currentItem.parent().children(":first"));
      }); 
    };

    // start the news section scrolling
    animator(ticker.children(":first"));

    // users mouse enters the scrolling view
    ticker.mouseenter(function() {

      // Im stopping the animation when the mouse enters
      ticker.children().stop();

    });

    // the mouse now leaves the view
    ticker.mouseleave(function() {

      // begin the animating again of the scroller
      animator(ticker.children(":first"));

    });
  });

由于

1 个答案:

答案 0 :(得分:2)

只是复制和粘贴JavaScript代码可能是危险的。在从博客/教程中删除它之前,请确保您理解它。还有,你自己尝试了什么吗?只是阅读代码应该明确答案:

持续时间=(距离+ parseInt(currentItem.css(“marginTop”)))/ 0.025;

将0.025更改为其他内容将更改动画的持续时间。