【问题标题】:How to make img draggable and wrap move too如何使 img 也可以拖动和换行
【发布时间】:2013-01-29 09:06:33
【问题描述】:

下面的代码尝试使img可拖动(关于this),它可以工作,但我不知道如果img move all wrap drag_wp move together. http://jsfiddle.net/cAeKG/8/ 有什么建议吗?

js

function enableDraggin(el){
    var dragging = dragging || false, x, y, ox, oy, current;
    el.onmousedown = function(e){
        e.preventDefault();
        current = e.target;
        dragging = true;
        x = e.clientX;
        y = e.clientY;
        ox = current.offsetLeft;
        oy = current.offsetTop;
        window.onmousemove = function(e) {
            if (dragging == true) {
                var sx = e.clientX - x + ox,
                sy = e.clientY - y + oy;
                current.style.left = sx + "px";
                current.style.top = sy + "px";
                return false;
            }
        };
        window.onmouseup = function(e) {
            dragging && (dragging = false);
        };
    };
};
var el = $('.drag_wp');
for(var i = 0; i < el.length; i++){
    enableDragging(el[i]);
};

html & css

<div class="drag_wp">
    <img src="..." class="drag_el">
    <div></div>
    ....// other div
</div>
.drag_wp{
    position: absolute;
    width: auto;
    height: auto;
    background:red;
}
.drag_el{
    position: absolute;
    width: 200px;
    height: auto;
}

【问题讨论】:

  • 请注意,您有一个错字:函数名为enableDraggin,在for循环中您有enableDragging(末尾为g

标签: javascript jquery


【解决方案1】:
function enableDraggin(el){
    var dragging = dragging || false, x, y, ox, oy, current;
    el.onmousedown = function(e){
        e.preventDefault();
        current = e.target;
        //just add this
        if(!$(current).hasClass(".drag_wp"))
             current = $(current).closest(".drag_wp")[0];
        //just add this
        dragging = true;
        x = e.clientX;
        y = e.clientY;
        ox = current.offsetLeft;
        oy = current.offsetTop;
        window.onmousemove = function(e) {
            if (dragging == true) {
                var sx = e.clientX - x + ox,
                sy = e.clientY - y + oy;
                current.style.left = sx + "px";
                current.style.top = sy + "px";
                return false;
            }
        };
        window.onmouseup = function(e) {
            dragging && (dragging = false);
        };
    };
};

e.target 返回被点击的元素(大多数情况下为您的 HTML 的图像)。如果它没有类.drag_wp,只需找到与该类最近的父类并将其用作当前类。

演示:http://jsfiddle.net/fTeXL/

另外,由于您已经在使用 jquery,您可能会发现使用 jQuery UI 中的相应插件会更好:http://jqueryui.com/draggable/

【讨论】:

  • 感谢您的回复。有用!!我正在学习尝试让它在没有 ui 或插件的情况下工作。
  • 如果单击内部div之一并且所有包装不会移动,是否可以制作?因为我尝试绑定另一个函数单击 1 div 来旋转或调整 wp 的大小。
  • 是的。你可以用 hasClass 之类的东西检查它,如果目标有那个类,说“resize_rotate”,而不是执行拖动代码。但另一种选择是将return false; 放在第二个函数中或使用e.stopPropagation()api.jquery.com/event.stopPropagation,因此将永远不会在附加了拖动代码的.drag_wp 元素上触发事件。
  • 非常感谢,我把e.stopPropagation放在了鼠标按下后的旋转功能中,效果很好!
【解决方案2】:

我对你的代码做了一些修改:

function enableDragging(el) {
    var dragging = dragging || false,
        x, y, ox, oy, current;
    el.onmousedown = function (e) {
        e.preventDefault();
        current = e.target.parentNode; // <--- current should be wrapper, not image
        dragging = true;
        x = e.clientX;
        y = e.clientY;
        ox = current.offsetLeft;
        oy = current.offsetTop;
        window.onmousemove = function (e) {
            if (dragging == true) {
                var sx = e.clientX - x + ox,
                    sy = e.clientY - y + oy;
                current.style.left = sx + "px";
                current.style.top = sy + "px";
                return false;
            }
        };
        window.onmouseup = function (e) {
            dragging && (dragging = false);
        };
    };
};

http://jsfiddle.net/cAeKG/9/

【讨论】:

  • 如果图像不是.drag_wp 的直接子代,e.target.parentNode 将不起作用。就像mousedown 直接发生在.drag_wp 上一样。
猜你喜欢
  • 2013-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-07
  • 2023-02-09
  • 2019-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多