Track Scroll Events.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    Track Scroll Events.

    How do i track the scroll events in JS. Is there any standard way to do it ?
    Please help me ... ;)
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use the onscroll event. It's not part of any standards, but should be reasonably well supported.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by acoder
      Use the onscroll event. It's not part of any standards, but should be reasonably well supported.
      I test this code in Mozilla...
      Code:
      <script type="text/javascript">
      var golbal_var = 0;
      function test(){
      	alert('');
      	document.getElementById('text_id').value = ++golbal_var;
      }
      </script>
      </head>
      
      <body onScroll=test()>
      <input type='text' id='text_id'/>
      <div style='width:2000px;height:2000px'/>
      In Mozilla it's working ..but not in IE ......what should i be doing ??
      Last edited by dmjpro; Nov 4 '08, 10:13 AM. Reason: Conception was wrong

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        For Mozilla compatibility, see MDC.

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by acoder
          For Mozilla compatibility, see MDC.
          Hi ... Acoder thanks!
          In IE i had to do .... window.onscroll = function_ref.
          Now in both it's working ..... ;)

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Originally posted by dmjpro
            Hi ... Acoder thanks!
            In IE i had to do .... window.onscroll = function_ref.
            Now in both it's working ..... ;)
            Hey Acoder.....
            A typical problem appears ...
            It's
            Code:
            alert(document.body.scrollLeft+'\t'+document.body.scrollTop);
            always showing value 0 ..no matter which scroll bars scrolled ....
            What's wrong with it ..

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Which browser? A quick test in Firefox works.

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                Originally posted by acoder
                Which browser? A quick test in Firefox works.
                I am having this HTML code .....
                Code:
                <body onLoad = _test()>
                <input type='text' id='text_id'/>
                <div style='width:2000px;height:2000px' id='div_id'>
                </div>
                </body>
                And this is my JavaScript code ..
                Code:
                function test(){
                	alert(document.body.scrollLeft+'\t'+document.body.scrollTop);
                }
                function _test(){
                	window.onscroll = test;
                }
                When horizontal or vertical scroll bars gets scrolled then the both of the values come 0 ...... in Mozilla and in IE both ..
                What's wrong with it .... ?

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Again, your code works too in FF, at least. Have you got anything else on the page?

                  Comment

                  • dmjpro
                    Top Contributor
                    • Jan 2007
                    • 2476

                    #10
                    Originally posted by acoder
                    Again, your code works too in FF, at least. Have you got anything else on the page?
                    How strange ..i am getting an alert box with two values ..and both are 0 .....
                    believe me :(

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      I tested with this version of your code:
                      [html]<html>
                      <head>
                      <script>
                      function test(){
                      alert(document. body.scrollLeft +'\t'+document. body.scrollTop) ;
                      }
                      function _test(){
                      window.onscroll = test;
                      }
                      </script>
                      </head>

                      <body onload = '_test()'>
                      <input type='text' id='text_id'>
                      <div style='width:20 00px;height:200 0px' id='div_id'>
                      </div>
                      </body>
                      </html>[/html]Works in Firefox 3.0.3

                      Comment

                      • dmjpro
                        Top Contributor
                        • Jan 2007
                        • 2476

                        #12
                        Originally posted by acoder
                        I tested with this version of your code:
                        [html]<html>
                        <head>
                        <script>
                        function test(){
                        alert(document. body.scrollLeft +'\t'+document. body.scrollTop) ;
                        }
                        function _test(){
                        window.onscroll = test;
                        }
                        </script>
                        </head>

                        <body onload = '_test()'>
                        <input type='text' id='text_id'>
                        <div style='width:20 00px;height:200 0px' id='div_id'>
                        </div>
                        </body>
                        </html>[/html]Works in Firefox 3.0.3

                        But mine was Firefox 2.
                        Whatever it is ...i just wanted to know ..is there any way to position an element at the right most of the page ..... that means according to the position of the scroll bars ...could you guide me to do this ....?
                        Please ... ;)

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Use "position: fixed" - a simple solution. Set the right property to 0 for it to be fixed to the right side of the window.

                          Note that position: fixed is not supported in IE 6, but you can make it work using the fix described here.

                          Comment

                          • dmjpro
                            Top Contributor
                            • Jan 2007
                            • 2476

                            #14
                            Originally posted by acoder
                            Use "position: fixed" - a simple solution. Set the right property to 0 for it to be fixed to the right side of the window.

                            Note that position: fixed is not supported in IE 6, but you can make it work using the fix described here.
                            No no ..... if i scroll down or up ..or scroll horizontally ..at every situation the element should be at the rightmost corner of the page ..i think now u got my point ... ;)
                            That's why i tried to track the scroll event ... :)

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              Then you didn't understand what position: fixed does. That's exactly what you need.

                              See this modified example:
                              Code:
                              <html>
                              <head>
                              <style type="text/css">
                              #text_id {
                                position:fixed;
                                top:0px;
                                right:0px;
                              }
                              </style>
                              </head>
                              
                              <body>
                              <input type='text' id='text_id'/>
                              <div style='width:2000px;height:2000px' id='div_id'>
                              </div>
                              </body>
                              </html>

                              Comment

                              Working...