javascript - jqueryui 可拖动移动图像与处理程序

标签 javascript jquery jquery-ui jquery-ui-draggable

如何使用处理程序获得可拖动的图像?我读过处理程序应该位于可拖动的内部,但我实际上不能在图像中包含另一个元素。

我这里有一个 fiddle ,可以更好地演示:

http://jsfiddle.net/index/bfpWv/10/

问题是我应该如何在#viewport内拖动并仍然移动#image

编辑

我认为这个问题有点模糊:

因为现在(在 fiddle 上)当您首先单击#image并拖动时,它会拖动#image。但是当您首先单击#viewport时,却没有。这就是我想要发生的事情,当您首先单击 #viewport 并拖动时,它仍然会拖动 #image

最佳答案

看来jquerydraggable需要将处理程序放在draggable中。您只需几行代码即可编写自己的可拖动对象。查看我的演示 here

HTML:

<div id="container">
    <img id="image" src="http://thechive.files.wordpress.com/2011/03/hot-sexy-redheads-12.jpg" />
    <div id="viewport"></div>
</div>

JS:

$(document).ready(function(){
    var isDragging, 
        top = 0, left = 0,
        curX, curY;

    $("#image").mousedown(function (e) {
        e.preventDefault();
    });

    $("#container").mousedown(function (e) {
        isDragging = true;

        curX = e.pageX;
        curY = e.pageY;

        left = Number($("#image").css("margin-left").
                      toString().replace("px", ""));
        top = Number($("#image").css("margin-top").
                     toString().replace("px", ""));
    });

    $(document).mouseup(function () {
        if (isDragging){
            // reset
            isDragging = false;
            top = 0;
            left = 0;
        }
    });

    $("#container").mousemove(function(e){
        if (!isDragging) {
            return;
        }

        left += e.pageX - curX;
        top += e.pageY - curY;

        // set the position
        $("#image").css("margin-left", left + "px").
            css("margin-top", top + "px");

        curX = e.pageX;
        curY = e.pageY;  
    });
});

关于javascript - jqueryui 可拖动移动图像与处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14887839/

相关文章:

javascript - react native : How to set background color for the whole app and individual scene in react-native-router-flux?

javascript - 你如何从 server/app.js 服务 client/index.html?

javascript - 构造动态变量 - AngularJS

ios - 停止 jQuery Mobile 滑动事件双冒泡

javascript - 当字段中有对象时,网格空表

javascript - 如何从控制台访问 Backbone 对象?

jquery - 从左侧滑动 DIV

javascript - jquery的对话框模式可以改变模式内的页面吗?

javascript - 与调用 JQuery 自定义函数感到困惑

javascript - 使用 Jquery DatePicker IN n month n year 格式显示仅月年范围持续时间的差异