admin管理员组文章数量:1334909
I want to call a function right after a scroll ends. I have tried a load of different things, none of which have quite worked, and then I came upon this solution in SO:
How do I know when I've stopped scrolling Javascript
I took a good look at the second answer and tried it - it works. Then I tried to change it slightly for my purposes. So now the whole shebang looks like this:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Do something after scroll ends</title>
<style type="text/css">
#scrollableDiv{
width:500px;
height:100px;
overflow:scroll;
}
#someContent{
width:600px;
height:200px;
}
</style>
<script type="text/javascript" src="scripts/jquery-1.5.1-min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#scrollableDiv').bind('scroll', bodyScroll);
var scrollTimer = -1;
function bodyScroll() {
console.log('scrolling.');
if (scrollTimer != -1)
clearTimeout(scrollTimer);
scrollTimer = window.setTimeout('scrollEnded()', 500);
};
function scrollEnded(){
console.log('scrollEnded. Now do something astounding.');
};
});
</script>
</head>
<body>
<div id="scrollableDiv">
<div id="someContent">
Scroll me.
</div>
</div>
</body>
</html>
When I run this, the scroll event triggers (great!).
Then, instead of calling my "scrollEnded()" function, an error is generated: "scrollEnded is not defined". I've tried to figure out where this es from by going into debug mode and stepping through the script, but then I land up in an "anonymous function" - which is where I hit the limits of my current understanding of this problem. I've tried moving the scrollEnded() function to the top. I've tried all sorts of things...
What can I do to make this work? (Can this thing work?)
I want to call a function right after a scroll ends. I have tried a load of different things, none of which have quite worked, and then I came upon this solution in SO:
How do I know when I've stopped scrolling Javascript
I took a good look at the second answer and tried it - it works. Then I tried to change it slightly for my purposes. So now the whole shebang looks like this:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>Do something after scroll ends</title>
<style type="text/css">
#scrollableDiv{
width:500px;
height:100px;
overflow:scroll;
}
#someContent{
width:600px;
height:200px;
}
</style>
<script type="text/javascript" src="scripts/jquery-1.5.1-min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#scrollableDiv').bind('scroll', bodyScroll);
var scrollTimer = -1;
function bodyScroll() {
console.log('scrolling.');
if (scrollTimer != -1)
clearTimeout(scrollTimer);
scrollTimer = window.setTimeout('scrollEnded()', 500);
};
function scrollEnded(){
console.log('scrollEnded. Now do something astounding.');
};
});
</script>
</head>
<body>
<div id="scrollableDiv">
<div id="someContent">
Scroll me.
</div>
</div>
</body>
</html>
When I run this, the scroll event triggers (great!).
Then, instead of calling my "scrollEnded()" function, an error is generated: "scrollEnded is not defined". I've tried to figure out where this es from by going into debug mode and stepping through the script, but then I land up in an "anonymous function" - which is where I hit the limits of my current understanding of this problem. I've tried moving the scrollEnded() function to the top. I've tried all sorts of things...
What can I do to make this work? (Can this thing work?)
Share Improve this question edited May 23, 2017 at 10:28 CommunityBot 11 silver badge asked Apr 1, 2011 at 15:34 user255594user255594 1-
2
Please don't pass strings to
setTimeout
andsetInterval
. It'seval
in disguise, and every time youeval
, a ninja kills a cute baby animal. – Matt Ball Commented Apr 1, 2011 at 15:38
4 Answers
Reset to default 1As an addition to all this answers I would only suggest setting 200ms timeout instead of 500. 200ms is the average human eye reaction between finger move and the eyes expecting to see the effect. 200ms timeout should be short enough to not be noticed by user and long enough for javascript to fire and finish the event. I've tried 200ms before and it seems to work just fine, even on slower PC's.
The jQuery throttle/debounce plugin is perfect for this. Use at_begin = false
to execute the callback at the end.
var scrollEnded = $.debounce(500, false, function ()
{
console.log('scroll ended');
});
$('#scrollableDiv').scroll(scrollEnded);
Demo
have you tried putting scrollEnded() on top of bodyScroll()?
function scrollEnded(){
console.log('scrollEnded. Now do something astounding.');
};
function bodyScroll() {
console.log('scrolling.');
if (scrollTimer != -1)
clearTimeout(scrollTimer);
scrollTimer = window.setTimeout('scrollEnded()', 500);
};
Sorry, I pletely ignored the fact that the function was within the closure, and therefore not available to the page in my earlier response. You could make it work via something like:
window.scrollEnded = function () { console.log('scrollEnded. No do something astounding'); }
followed by a
window.setTimeout(window.scrollEnded, 500);
That should remove the eval of the string, as well as put it in a global scope to where it is available. There are other oddities about that in terms of namespace pollution, but for the time being I'm trying to be somewhat simple and help you get it work.
本文标签: javascriptHow to call a function after scroll has endedStack Overflow
版权声明:本文标题:javascript - How to call a function after scroll has ended? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742326415a2453817.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论