Javascript - 设置和控制滚动速度

发布于 2024-11-27 10:33:18 字数 1596 浏览 0 评论 0原文

一直致力于在我的网站中实现滚动新闻行情。用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"));

    });
  });

谢谢

Been working on implementing a scrolling news ticker into my site. Controlling the scrolling function with JS. The code I am using is below, can anyone advise me on how to set and scrolling the speed at which the ticker will scroll?

$(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"));

    });
  });

Thanks

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

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

发布评论

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

评论(1

相对绾红妆 2024-12-04 10:33:18

仅仅复制和粘贴 JavaScript 代码可能是危险的事情。在从博客/教程中提取它之前,请确保您理解它。另外,你自己尝试过什么吗?只需阅读代码就可以清楚地了解答案:

duration = (distance + parseInt(currentItem.css("marginTop"))) / 0.025;

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

Just copying and pasting JavaScript code can be dangerous stuff. Make sure you understand it before yanking it from a blog / tutorial. Also, have you tried anything yourself yet? Just reading the code should make the answer obvious:

duration = (distance + parseInt(currentItem.css("marginTop"))) / 0.025;

Changing 0.025 to something else will change the duration of the animation.

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