Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Nov 2011
    Posts
    154
    Thanks
    5
    Thanked 0 Times in 0 Posts

    detect window scroll position

    How do I detect when the window is set to a certain scroll position? For example, I have this event:

    $('html,body').animate({scrollLeft: $("#div").offset().left-window.innerWidth/2}, 1000)

    How do I execute a function when that specific location has been scrolled to? For example, anytime the window is set to that position, div2 can be clicked? Not sure how to write that if statement?
    Last edited by resin; 02-22-2014 at 11:28 AM.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,203
    Thanks
    23
    Thanked 605 Times in 604 Posts
    This should give you a good place to start:
    Code:
    <script type="text/javascript">
    window.onscroll = scroll;
    
    function scroll () {
    var pos_left = document.getElementById("div2").offsetLeft;
      if(pos_left >= YOU OFFSET HERE){
    	  MAKE IT CLICKABLE
      }
    }
    </script>
    Last edited by sunfighter; 02-22-2014 at 01:12 PM.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,676
    Thanks
    6
    Thanked 1,008 Times in 981 Posts
    And if you’re more into using jQuery read the official documentation, it has all info you ever need:

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,676
    Thanks
    6
    Thanked 1,008 Times in 981 Posts
    Code:
    if($('html,body').scrollTop() === [your value])

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,045
    Thanks
    0
    Thanked 251 Times in 247 Posts
    I think what the OP wants is to execute a function when the window is set to a certain scroll position while the animation is in progress. If so, then you need to use the step callback option of the jQuery animate function.
    Code:
    $('html,body').animate({
        scrollLeft: $("#div").offset().left - ($(window).width()/2)
    }, {
        duration: 1000,
        step: function (now, fx) {
            var x = $(window).scrollLeft();
    
            //check x here
        }
    });


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •