jQuery滚动和计时器问题 [英] jquery scrolling and timer issue
问题描述
你好,
我有一个CSS溢出设置为自动的div.因此,当div变大时,将显示一个滚动条.
通过jQuery函数填充div.这是由计时器完成的.计时器运行后,它将div向下滚动到底部.这一切都很好.但是当我尝试自己向上滚动时,它不起作用,因为jQuery函数再次向下滚动.听起来合乎逻辑.所以我通过以下代码解决了这个问题:
Hello,
I have a div with css overflow set to auto. So when the div gets to big, a scrollbar is shown.
Through a jQuery function the div is filled. This is done by a timer. After the timer has ran it scrolls down the div to the bottom. This all works fine. But when I try to scroll up by myself it does not work, because a jQuery function scrolls down again. Sounds logical. So I fixed this by this code:
$('#sb_content').scroll(function () {
RemoveTimer();
});
RemoveTimer()删除计时器,因此它不会再次向下滚动,我可以自己滚动.
但是当我放开滚动条时,它应该与计时器一起恢复并再次自动向下滚动.
我该如何捕捉呢? jQuery如何知道我放开了滚动条,它可以再次使用计时器恢复?
在此先感谢.
RemoveTimer() removes the timer, so it does not scroll down again and I''m able to scroll by myself.
But when I let the scrollbar go it should resume with the timer and scroll down automaticly again.
How do I capture that? How does jQuery know I have let the scrollbar go and it can resume with the timer again?
Thanks in advance.
推荐答案
(' #sb_content' ).scroll(function(){ RemoveTimer(); });
('#sb_content').scroll(function () { RemoveTimer(); });
RemoveTimer()删除计时器,因此它不会再次向下滚动,我可以自己滚动.
但是当我放开滚动条时,它应该与计时器一起恢复并再次自动向下滚动.
我该如何捕捉呢? jQuery如何知道我放开了滚动条,并且它可以再次使用计时器恢复?
在此先感谢您.
RemoveTimer() removes the timer, so it does not scroll down again and I''m able to scroll by myself.
But when I let the scrollbar go it should resume with the timer and scroll down automaticly again.
How do I capture that? How does jQuery know I have let the scrollbar go and it can resume with the timer again?
Thanks in advance.
我做了一些测试,发现MouseDown确实可以启动,但是使用滚动条时MouseUp却没有.不知道这是Jquery/Javascript中的错误还是设计上的错误,因此需要找到解决方法.我想到的最好的选择是禁用MouseDown上的自动滚动,但是当鼠标离开时通过捕获mouseout和/或mouseleave事件来启用自动滚动.
下面的代码是我用于测试的代码,尽管它不是理想的解决方案,但却可以解决.
顶部文本区域是使用连续计时器的不断增长的文本区域.底部的文本区域仅用于显示正在触发的事件.
I have done some testing, and have found that MouseDown does fire, but MouseUp does not when the scroll bar is used. Don''t know if this is a bug in Jquery/Javascript or by design so need to find a work around. The best option i came up with is to disable the autoscroll on a MouseDown, but then renable autoscroll when the mouse leaves by capturing the mouseout and/or mouseleave events.
The code below is what i used for testing, although it is not the ideal solution, it is a working working around.
The top text area is an ever growing text area using a continuous timer. the bottom text area is used just to show what events are being fired.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
<script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
var line = 1;
var allowAutoScroll = true;
(功能(){
这篇关于jQuery滚动和计时器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!