javascript - 如何平滑滚动页面直到用户释放鼠标按钮
简短的问题 - 在电脑上打开的页面,触摸屏显示不太好。我创建了 2 个大箭头,但不知道如何使用 JS/jQuery 对其进行编程。
第一次尝试:onClick->scroll - 它可以工作,但用户必须点击多次才能滚动文章。 第二:
var scrolling = false;
$("#scUp").mouseup(function(){
$(this).css("opacity", 0.3);
scrolling = false;
}).mousedown(function(){
$(this).css("opacity", 1);
scrolling = true;
while(scrolling) {
$('html, body').stop().animate({ scrollTop: 50 }, 500);
}
event.preventDefault();
});
不起作用;) 我正在尝试模拟真实的浏览器滚动箭头 - 直到您保持预置的鼠标按钮页面向下(或向上)滚动。
Shor question - page opened on pc with not-so-good touch-screen display. I created 2 big arrows and dont know how to program it using JS/jQuery.
First try: onClick->scroll - it works but user must tap many times to scroll article.
Second:
var scrolling = false;
$("#scUp").mouseup(function(){
$(this).css("opacity", 0.3);
scrolling = false;
}).mousedown(function(){
$(this).css("opacity", 1);
scrolling = true;
while(scrolling) {
$('html, body').stop().animate({ scrollTop: 50 }, 500);
}
event.preventDefault();
});
Doesnt work ;)
I`m trying to simulate real browser scroll arrows - until you keep preesed mouse button page scrolls down (or up).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我回答了这个问题一段时间......基本上它当鼠标按下时设置一个标志,当鼠标抬起时清除。然后
setTimeout
循环直到标志被清除。此外,它还具有鼠标滚轮和拖放功能。查看演示
I answered this question some time back... basically it sets a flag when the mouse is down and clears when the mouse is up. Then a
setTimeout
loops until the flag clears. Also, it has mousewheel and drag-and-drop functionality.Check out the demo
上面的代码不起作用,因为 JavaScript 不是多线程的。也就是说,您的 while 循环正在占用 CPU 并可能阻止其他代码运行(即 mouseup 事件)。
不久前我做了类似的事情。请随时查看我的博客文章。
另外,不确定您是否这样做,但请确保将所有 JavaScript 代码放入 jQuery 的 read() 函数中;否则,jQuery 可能找不到#scUp 元素。
这是我旧博客文章中的相关代码:
...以及链接:
http://blake-miner.blogspot.com /2010/08/javascript-sticky-footer-and-scroll.html
希望这有帮助!
Your code above doesn't work because JavaScript is not multi-threaded. That is, your while loop is eating CPU and probably preventing other code from running (i.e. the mouseup event).
I did something like this not too long back. Please feel free to check out my blog post.
Also, not sure if you're doing this or not, but make sure that you place all of your JavaScript code in jQuery's ready() function; otherwise, jQuery may not find the #scUp element.
Here's the relevant code from my old blog post:
...and the link:
http://blake-miner.blogspot.com/2010/08/javascript-sticky-footer-and-scroll.html
Hope this helps!
与此同时,我写了这段代码:
它可以工作,但不能在 Opera 上...有趣的事情 - kiosk 基于 Opera 浏览器,所以,有什么解决方案吗?
顺便提一句。有没有关于在 Linux 上使用 FF 构建 kiosk(对于 FF 1.x 版本来说还没有过时)的材料(Linux 对我来说没问题,但我正在寻找 FF 的安全插件)。
In meantime I wrote this code:
It works BUT not on Opera ... funny thing - kiosk is based on Opera browser, so, any solution?
btw. Is there any materials about building kiosk (not outdated for 1.x version of FF) on Linux with FF (linux is no problem for me but I searching for security-plugin for FF).