views:

53

answers:

2

When you scroll, are you changing the position of something?

How can I change the position of the scroll? Specifically to match the cursors movement.

What I want to do is scroll the window when I click and drag with the cursor inside the window.

For example: I have a 400px by 400px div with a 900px by 900px div inside of it. I want to scroll around by click and dragging. Note, I do NOT want to move the position of the inside div (easily doable with jQuery UI via draggable), just the scroll position. Moving the actual div position messes with my other javascript applications.

Any help would be appreciated! Thanks!

A: 

Something like

$(window).scrollTop(someNumberHere);

perhaps?

It would help if you could clarify exactly what you mean by "I want to ... scroll the window when I click and drag with the cursor inside the window".

http://api.jquery.com/scrollTop

Matt Ball
Example:If I click in the window and drag left 10px, I want it to scroll right 10px.
Dave
@Dave: drag _what_ by 10px?
Matt Ball
If I click/drag anywhere inside #container or even click/drag on #stuff, I want it to scroll #container.<div id="container" style="width: 400px; height: 400px; overflow: auto;"> <div id="stuff" style="width: 900px; height: 900px;> </div></div>
Dave
I think scrollTop() and scrollLeft() is what I was looking for, trying out now.
Dave
+2  A: 

This should get you going with horizontal scrolling. Vertical would be similar, but with scrollTop().

<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
        <script type="text/javascript">
            var lastPageX;

            $(document).ready(function() {
                $("#inner").mousedown(function(e) {
                    // Reference to the drag pad
                    var inner = $(this);
                    // Position where mouse was pressed
                    lastPageX = e.pageX;
                    // Attach mouse move listener
                    $(inner).mousemove(function(e) {
                        var diff = lastPageX - e.pageX;
                        // Scroll based on the new curson position
                        $("#outer").scrollLeft($("#outer").scrollLeft() + diff);
                        lastPageX = e.pageX;
                    });
                    // Remove mouse move listener
                    $(window).mouseup(function() {
                        $(inner).unbind("mousemove");
                    });
                    return false;
                });
            });
        </script>
        <style>
            #outer {
                height: 400px;
                width: 400px;
                background-color: Lime;
                overflow: scroll;
            }
            #inner {
                height: 900px;
                width: 900px;
                background-color: Yellow;
            }
        </style>
    </head>
    <body>
        <div id="outer">
            <div id="inner">
            </div>
        </div>
    </body>
</html>

Edit: Return false after the mousedown is handled to prevent firefox from grabbing the div.

Ed Saito
[jsfiddle demo](http://jsfiddle.net/8yL7Y/) :)
Matt Ball