Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I have a div with css overflow set to auto. So when the div gets to big, a scrollbar is shown.
Through a jQuery function the div is filled. This is done by a timer. After the timer has ran it scrolls down the div to the bottom. This all works fine. But when I try to scroll up by myself it does not work, because a jQuery function scrolls down again. Sounds logical. So I fixed this by this code:

C#
$('#sb_content').scroll(function () {
        RemoveTimer();
    });


RemoveTimer() removes the timer, so it does not scroll down again and I'm able to scroll by myself.

But when I let the scrollbar go it should resume with the timer and scroll down automaticly again.

How do I capture that? How does jQuery know I have let the scrollbar go and it can resume with the timer again?

Thanks in advance.
Posted
Comments
DaveAuld 9-Oct-11 15:22pm    
onmousedown and onmouseup events not help you?
Ken Elz 10-Oct-11 2:46am    
Nope, tried that, did not work. Events are not fired when used.

1 solution

I have done some testing, and have found that MouseDown does fire, but MouseUp does not when the scroll bar is used. Don't know if this is a bug in Jquery/Javascript or by design so need to find a work around. The best option i came up with is to disable the autoscroll on a MouseDown, but then renable autoscroll when the mouse leaves by capturing the mouseout and/or mouseleave events.

The code below is what i used for testing, although it is not the ideal solution, it is a working working around.

The top text area is an ever growing text area using a continuous timer. the bottom text area is used just to show what events are being fired.

HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>

    <script src="http://code.jquery.com/jquery-1.6.4.min.js" type="text/javascript"></script>

    <script type="text/javascript">

        var line = 1;
        var allowAutoScroll = true;

        $(document).ready(function () {

            $("#textBlock").mouseenter(function () {
                $("#textLog").val($("#textLog").val() + "Mouse Enter.\n");
                textLog.scrollTop = textLog.scrollHeight;
            });

            $("#textBlock").mousedown(function () {
                $("#textLog").val($("#textLog").val() + "Mouse Down.\n");

                textLog.scrollTop = textLog.scrollHeight;

                allowAutoScroll = false;

            });

            $("#textBlock").mouseup(function () {
                $("#textLog").val($("#textLog").val() + "Mouse Up.\n");
                textLog.scrollTop = textLog.scrollHeight;
            });

            $("#textBlock").mouseout(function () {
                $("#textLog").val($("#textLog").val() + "Mouse Out.\n");
                textLog.scrollTop = textLog.scrollHeight;

                allowAutoScroll = true;

            });

            $("#textBlock").mouseleave(function () {
                $("#textLog").val($("#textLog").val() + "Mouse Leave.\n");
                textLog.scrollTop = textLog.scrollHeight;

                allowAutoScroll = true;
            });

            $("#textBlock").scroll(function () {
                $("#textLog").val($("#textLog").val() + "Scroll.\n");
                textLog.scrollTop = textLog.scrollHeight;
            });

            setTimeout(timerTick, 1000);
        });


        function timerTick() {
            //Populate the text area with Timer Events and scroll to bottom after each event added
            $("#textBlock").append("Timer Event " + line.toString() + "\n");
            setTimeout(timerTick, 1000);
            line++;

            if (allowAutoScroll) {
                textBlock.scrollTop = textBlock.scrollHeight;
            }
        }


    </script>

</head>
<body>
<p>
Below is a long text box.<br />
<textarea rows="5" cols="50" id="textBlock">This is the text block across
lots of lines
</textarea>
</p>

<p>
Below is the events fired log.<br />
<textarea rows="5" cols="50" id="textLog">Events
</textarea>
</p>



</body>
</html>
 
Share this answer
 
Comments
Ken Elz 17-Oct-11 9:53am    
Works great! I changed it a little bit, 'cause I'm not working with textboxes (or textarea's). I'm working with div's. Perhaps you can tell me how to auto scroll down in a div? If you can tell me that I'm done with this issue.

For now: Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900