admin管理员组

文章数量:1024657

What is the proper way to activate an on scroll listener after a click event?

I'm currently using:

$('.button').click(function (event) {
   $(window).on("scroll", someFunction);
}

someFunction = function() {
   //do stuff 
   $(window).off("scroll"); //disable scroll listener
}

On a click event I enable the scroll listener which runs someFunction. The function does stuff and disables the scroll listener when finished. The scroll listener is enabled again on click.

My concern is that I'm not doing it right. Please advise!

Note: The scroll listener cannot run indefinitely. It starts on click and must finish at the end of myFunction.

Note: I'm not trying to detect when user stops scrolling..

What is the proper way to activate an on scroll listener after a click event?

I'm currently using:

$('.button').click(function (event) {
   $(window).on("scroll", someFunction);
}

someFunction = function() {
   //do stuff 
   $(window).off("scroll"); //disable scroll listener
}

On a click event I enable the scroll listener which runs someFunction. The function does stuff and disables the scroll listener when finished. The scroll listener is enabled again on click.

My concern is that I'm not doing it right. Please advise!

Note: The scroll listener cannot run indefinitely. It starts on click and must finish at the end of myFunction.

Note: I'm not trying to detect when user stops scrolling..

Share Improve this question edited Nov 24, 2015 at 16:44 sergdenisov 8,5929 gold badges51 silver badges66 bronze badges asked May 6, 2015 at 17:23 CyberJunkieCyberJunkie 22.7k61 gold badges154 silver badges219 bronze badges 3
  • Does it work? Looks fine to me really – scniro Commented May 6, 2015 at 17:25
  • possible duplicate of jQuery scroll() detect when user stops scrolling – Jako Commented May 6, 2015 at 17:30
  • 1 @Jako not dublicate. I'm trying to find out how " to activate an on scroll listener after a click event" as posted. Not detect when user stops scrolling. Totally different! – CyberJunkie Commented May 6, 2015 at 17:32
Add a ment  | 

3 Answers 3

Reset to default 6

You could use jQuery .one():

$('.button').on('click', function() {
    $(window).one('scroll', someFunction);
});

Every single click adds an additional scroll event listener. I would encapsulate the binding with an additional variable:

var isScrollBindingActive = false;
$('.button').click(function (event) {
    if (!isScrollBindingActive) {
        isScrollBindingActive = true;
        $(window).on("scroll", someFunction);
    }
}

someFunction = function() {
   //do stuff 
   $(window).off("scroll"); //disable scroll listener
   isScrollBindingActive = false; // allow binding again if wished
}

You can do it the following way:

$('.button').click(function (event) {
   $(window).bind("scroll", someFunction);
}

someFunction = function() {
   //do stuff 
   $(window).unbind("scroll"); // remove scroll listener
}

What is the proper way to activate an on scroll listener after a click event?

I'm currently using:

$('.button').click(function (event) {
   $(window).on("scroll", someFunction);
}

someFunction = function() {
   //do stuff 
   $(window).off("scroll"); //disable scroll listener
}

On a click event I enable the scroll listener which runs someFunction. The function does stuff and disables the scroll listener when finished. The scroll listener is enabled again on click.

My concern is that I'm not doing it right. Please advise!

Note: The scroll listener cannot run indefinitely. It starts on click and must finish at the end of myFunction.

Note: I'm not trying to detect when user stops scrolling..

What is the proper way to activate an on scroll listener after a click event?

I'm currently using:

$('.button').click(function (event) {
   $(window).on("scroll", someFunction);
}

someFunction = function() {
   //do stuff 
   $(window).off("scroll"); //disable scroll listener
}

On a click event I enable the scroll listener which runs someFunction. The function does stuff and disables the scroll listener when finished. The scroll listener is enabled again on click.

My concern is that I'm not doing it right. Please advise!

Note: The scroll listener cannot run indefinitely. It starts on click and must finish at the end of myFunction.

Note: I'm not trying to detect when user stops scrolling..

Share Improve this question edited Nov 24, 2015 at 16:44 sergdenisov 8,5929 gold badges51 silver badges66 bronze badges asked May 6, 2015 at 17:23 CyberJunkieCyberJunkie 22.7k61 gold badges154 silver badges219 bronze badges 3
  • Does it work? Looks fine to me really – scniro Commented May 6, 2015 at 17:25
  • possible duplicate of jQuery scroll() detect when user stops scrolling – Jako Commented May 6, 2015 at 17:30
  • 1 @Jako not dublicate. I'm trying to find out how " to activate an on scroll listener after a click event" as posted. Not detect when user stops scrolling. Totally different! – CyberJunkie Commented May 6, 2015 at 17:32
Add a ment  | 

3 Answers 3

Reset to default 6

You could use jQuery .one():

$('.button').on('click', function() {
    $(window).one('scroll', someFunction);
});

Every single click adds an additional scroll event listener. I would encapsulate the binding with an additional variable:

var isScrollBindingActive = false;
$('.button').click(function (event) {
    if (!isScrollBindingActive) {
        isScrollBindingActive = true;
        $(window).on("scroll", someFunction);
    }
}

someFunction = function() {
   //do stuff 
   $(window).off("scroll"); //disable scroll listener
   isScrollBindingActive = false; // allow binding again if wished
}

You can do it the following way:

$('.button').click(function (event) {
   $(window).bind("scroll", someFunction);
}

someFunction = function() {
   //do stuff 
   $(window).unbind("scroll"); // remove scroll listener
}

本文标签: javascriptjQuery on scroll listener after click eventStack Overflow