JQuery Draggable not positioning scrollbars correctly

I need to make an image viewer that allows you to load large images into a container and then drag and drop inside the container so that the entire image is visible, but the image is never stretched out. The code below works fine, except that the scroll bars are not exactly synchronized with the position of the dragged image and allow you to scroll the image outside. How to synchronize scrollbars with image while dragging it?

Edit:

Here is a working example.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js"></script>
        <style>
            .container{margin: auto;cursor: move;width: 80%; position: relative; min-width:885px;}
            #screen{overflow:auto; width: 80%; height: 600px; clear: both; border: 1px solid black; background-color: #CCCCCC; float:left; margin-right: 15px;}
        </style>
    </head>
    <body>
          <div class="container">
                <div id="screen">
                    <img class="drag-image" id="draggable" />
                </div>
          </div>
    </body>
    </html>

    <script type="text/javascript">

        $(function () {

            $('#draggable').attr('src', 'http://i.imgur.com/uPjIz.jpg').load(function () {
                CreateDraggablePicture();
            });

        });

        function CreateDraggablePicture() {

            var x = ($('#draggable').width() - $('#screen').width() - $('#screen').offset().left) * -1;
            var y = ($('#draggable').height() - $('#screen').height() - $('#screen').offset().top) * -1;
            var x2 = $('#screen').offset().left;
            var y2 = $('#screen').offset().top;

            $("#draggable").draggable({ containment: [x, y, x2, y2], scroll: true });

        }

    </script>
+5
source share
2 answers

jQuery. . , , css css padding.

,

0

All Articles