3

Is it possible to write a JavaScript to make an action when a DIV Layer's scroll bar is manually scrolled up or scrolled down?

If so please give me a hint to implement a simple such as alert box saying you scrolled up and you scrolled down.

Pooja Kedar
  • 439
  • 2
  • 10
  • 23
Mad coder.
  • 2,175
  • 8
  • 38
  • 53

2 Answers2

8

You can simple use onscroll event of java-script.

OnScroll Event Reference : http://www.w3schools.com/jquery/event_scroll.asp

Here is example,

<head>
    <script type="text/javascript">
        function OnScrollDiv (div) {
            var info = document.getElementById ("info");
            info.innerHTML = "Horizontal: " + div.scrollLeft
                            + "px<br/>Vertical: " + div.scrollTop + "px";
        }
    </script>
</head>
<body>
    <div style="width:200px;height:200px; overflow:auto;" onscroll="OnScrollDiv (this)">
        Please scroll this field!
        <div style="height:300px; width:2000px; background-color:#a08080;"></div>
        Please scroll this field!
        <div style="height:300px; width:2000px; background-color:#a08080;"></div>
        Please scroll this field!
    </div>
    <br /><br />
    Current scroll amounts:
    <div id="info"></div>
</body>

Working copy here : http://jsbin.com/iledat/2/edit

Sreekumar P
  • 5,900
  • 11
  • 57
  • 82
-5

Try jquery scroll event.

$('div').scroll(function(){alert('scrolled!')})
Litek
  • 4,888
  • 1
  • 24
  • 28