JavaScript Getting scrollTop() to perform only once

I want scrollTop to trigger only once, so that the red box doesn't appear again after it has been closed with the button.

I know there is .one(), but I have no idea how to apply it to my code. Below is my code and jsfiddle.

<div id="box-pop">This is a red box.
    <button class="box-close">Close</button>
</div>


$(document).scroll(function () {
    if ($(this).scrollTop() > 100) {
        $('#box-pop').fadeIn(500);
    }
});
$(document).ready(function () {
    $(".box-close").click(function () {
        $("#box-pop").fadeOut(200)
    });

});

http://jsfiddle.net/3LG6t/3/

Answer:1

For a better performance in your page, what about this?

$("#box-pop").fadeOut(200,function(){
    $(this).remove();
});

Demo: http://jsfiddle.net/3LG6t/5/

And take a look at this topics:

  1. Differences between detach(), hide() and remove() - jQuery
  2. jQuery detach() v/s remove() v/s hide()
  3. Why remove() or detach() instead of hide()?
Answer:2

One is used the same way bind or on is. Scroll itself really isn't an actual function but a shortcut to on that performs the following:

.on('scroll', function () {

}

However you need to get something to work once after a condition, so you would use off after using on.

<div id="box-pop">This is a red box.
    <button class="box-close">Close</button>
</div>


$(document).on('scroll', function fadeBoxIn() {
    if ($(this).scrollTop() > 100) {
        $('#box-pop').fadeIn(500);
        $(document).off('scroll', fadeBoxIn);

    }
});
$(document).ready(function () {
    $(".box-close").click(function () {
        $("#box-pop").fadeOut(200)
    });

});

See the fiddle.

Answer:3

Try $( "document").unbind( "scroll" ); :

$(document).scroll(function () {
  if ($(this).scrollTop() > 100) {
    $('#box-pop').fadeIn(500);
  }
});
$(document).ready(function () {
  $(".box-close").click(function () {
    $("#box-pop").fadeOut(200)
    $(document).unbind("scroll");        
  });
});

or conditionally rebind the scroll function within the $(".box-close").click().

Answer:4

Below is the code in which changeme is the text i want to change on the click of the button. HTML code and JS is below: 1) whatever user input in field will be capture and replace with changeme if ...

Below is the code in which changeme is the text i want to change on the click of the button. HTML code and JS is below: 1) whatever user input in field will be capture and replace with changeme if ...

  1. click button change text javascript
  2. click button change image javascript
  3. click button change color
  4. click button change background color css
  5. click button change text jquery
  6. click button change color css
  7. click button change image
  8. click button change color jquery

I'm looking to use a map to map a string to an integer value but allowing the int to be manipulated within the map. E.g. var Map : map<string, int>; Map["foo"] = 5; Map["foo"] = Map["foo"] * ...

I'm looking to use a map to map a string to an integer value but allowing the int to be manipulated within the map. E.g. var Map : map<string, int>; Map["foo"] = 5; Map["foo"] = Map["foo"] * ...

I want to write a code which can toggle two sentences fading in and fading out. But I want to toggle the sentences at the same location ie one text fades and the other starts coming in place of the ...

I want to write a code which can toggle two sentences fading in and fading out. But I want to toggle the sentences at the same location ie one text fades and the other starts coming in place of the ...

  1. fade in fade out text premiere pro
  2. fade in fade out text css
  3. fade in fade out text
  4. fade in fade out text slider
  5. fade in fade out text after effects
  6. fade in fade out text unity
  7. fade in fade out text animation css
  8. fade in fade out text animation
  9. fade in fade out text jquery
  10. fade in fade out text javascript
  11. fade in fade out text animation jquery
  12. fade in fade out text effect jquery

i would like to query mysql database for some result using PHP and display the result in an alert dialog box using javascript. I can connect and query the database ontop on displaying the result in a ...

i would like to query mysql database for some result using PHP and display the result in an alert dialog box using javascript. I can connect and query the database ontop on displaying the result in a ...

  1. display mysql result php
  2. display mysql results in table php
  3. display mysql results in html table
  4. display mysql results in table
  5. echo mysql result
  6. echo mysql result php
  7. display mysql query in html table
  8. display mysql query error php
  9. echo mysql result array
  10. display mysql query in wordpress
  11. display mysql query
  12. mysql display results vertically
  13. mysql display results horizontally
  14. mysql display results
  15. mysql display results in columns
  16. mysql display results in one row
  17. mysql display query time
  18. mysql display query error
  19. display mysql select results in php