2011-07-29 214 views
0

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

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

    }); 
    }); 

感谢

回答

2

只是复制和粘贴的JavaScript代码可能是危险的东西。确保你在博客/教程之前了解它。另外,你有没有尝试过自己呢?只读代码应该使答案显而易见:

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

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

+0

感谢您的回复,我认为从'教程'网站使用代码学习并不是坏事,不是每个人都可以成为专家程序员。是的,我曾尝试过使用相同部分的代码尝试失败,但我会再试一次。谢谢 –