Javascript - 设置和控制滚动速度
一直致力于在我的网站中实现滚动新闻行情。用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅仅复制和粘贴 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.