1

In my below code a function to scroll DIV for every set of intervals where when I try to scroll up due to interval refresh scroll bar again coming down.

My code:

   var int = self.setInterval("f2()", 1000);
   function f2() {
   var objDiv = document.getElementById("messages2");
   objDiv.scrollTop = objDiv.scrollHeight;
   }

Now, by using onscroll property for DIV is there anyway to stop scrolling down when holding scroll bar with mouse?

Something like

       var int = self.setInterval("f2()", 1000);
       function f2() {
       var objDiv = document.getElementById("messages2");
       // if(objDiv.onscroll) i.e. when the particular DIV's scroll bar is on hold by cursor.
          {
          return false;
          }
       else
          {
            objDiv.scrollTop = objDiv.scrollHeight;
          }
      }

If the above kind of function can be implemented anybody please correct its syntax. I am a newbie to JavaScript.

Thanks in advance..!

1 Answer 1

6

I’m not sure what you are asking here, but window has a scroll event that you can listen to, and so does any other element that has overflow: auto|scroll set using CSS.

You can’t prevent the default scrolling behavior, but you can use the scrollTo() method or the scrollTop property if you want to do something annoying like:

window.onscroll = function() {
    window.scrollTo(0,0);
};

Here is a nice compat table of the scroll event: http://www.quirksmode.org/dom/events/scroll.html

scrollTo() docs: https://developer.mozilla.org/en/Window.scrollTo

Sign up to request clarification or add additional context in comments.

4 Comments

Is it not possible for DIV layer with overflow:auto ?
Yes it is, any element that has overflow set to auto or scroll.
I am sorry it didn't work. I tried as var objDiv = document.getElementById("messages2"); objDiv.onscroll = function () { objDiv.scrollTo(0, 0); }; If you don't mind can you please provide me the syntax for my div?
The scrollTo() is only available on the window object, if you want to do the same for a HTML element, try scrollTop = 0; Demo: jsfiddle.net/an8Gd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.