简体   繁体   English

在父div周围移动框

[英]Move box around parent div

I have an assignment where I have to create two boxes, one inside of the other. 我有一个作业,我必须创建两个盒子,一个在另一个盒子内。 The smaller(inside) box is supposed to be able to move freely with a mousedrag inside of the container box. 较小的(内部)盒子应该能够在容器盒子内部用鼠标拖动自由移动。 The smaller box is not supposed to be able to leave the larger box. 较小的盒子不应离开较大的盒子。 I have figured out the first part(moving the box) but can't quite figure out the second? 我已经弄清楚了第一部分(移动盒子),但不能完全弄清第二部分吗? I'm sure I'm just overlooking something simple, so any tips would be appreciated. 我敢肯定,我只是忽略了一些简单的事情,因此任何提示都将不胜感激。 This is my code. 这是我的代码。

window.onload = init;
var mousePiece = null;

function init() {
    var box = document.getElementById("box");
    container = document.getElementById("container");

    box.style.top = getStyle(box,"top");
    box.style.left = getStyle(box,"left");
    box.style.height = getStyle(box,"height");
    box.style.width = getStyle(box,"width");

    container.style.top = getStyle(container,"top");
    container.style.left = getStyle(container,"left");
    container.style.height = getStyle(container,"height");
    container.style.width = getStyle(container,"width");


    addEvent(box, "mousedown", mouseGrab, false);
}

function mouseGrab(e) {
    var evt = e || window.event;
    mousePiece = evt.target || evt.srcElement;

    addEvent(document, "mousemove", mouseMove, false);
    addEvent(document, "mouseup", mouseDrop, false);
}

function mouseMove(e) {
    var evt = e || window.event;
    var mouseX = evt.clientX;
    var mouseY = evt.clientY;

        mousePiece.style.left = mouseX - 25 + "px";
        mousePiece.style.top = mouseY - 25 + "px";
}

function mouseDrop(e) {
    mousePiece = null;
    removeEvent(document, "mousemove", mouseMove, false);
    removeEvent(document, "mouseup", mouseDrop, false);
}

function addEvent(object, evName, fnName, cap) {
    if (object.attachEvent)
        object.attachEvent("on" + evName, fnName);
    else if (object.addEventListener)
        object.addEventListener(evName, fnName, cap);
}

function removeEvent(object, evName, fnName, cap) {
    if (object.detachEvent)
        object.detachEvent("on" + evName, fnName);
    else if (object.removeEventListener)
        object.removeEventListener(evName, fnName, cap);
}

function getStyle(object, styleName) {
    if (window.getComputedStyle) {
        return document.defaultView.getComputedStyle(object, null).getPropertyValue(styleName);
    } else if (object.currentStyle) {
        return object.currentStyle[styleName]
    }
}

Thanks, Jesse. 谢谢,杰西。

In your mousemove function you must test to see if the new position falls outside of the container, and if it does change the values before assigning it to the box. mousemove函数中,您必须进行测试以查看新位置是否在容器之外,以及是否确实更改了值,然后再将其分配给框。

See http://jsfiddle.net/gaby/eutk9u48/ 参见http://jsfiddle.net/gaby/eutk9u48/


Explanation of fiddle 小提琴的解释

Inside the init function we fill an object named limit ( which is declared at the top ) with the left/top/right/bottom positions of the container ( for easy reference ) init函数内部,我们用容器的左/上/右/下位置填充一个名为limit在顶部声明 )的对象( 以方便参考

// parseInt is used to get the numbers without the 'px' at the end
// so we can perform numeric comparisons with it
limits.top = parseInt(container.style.top); 
limits.left = parseInt(container.style.left);
limits.right = limits.left + parseInt(container.style.width);
limits.bottom = limits.top + parseInt(container.style.height);

Then in the mousemove method we do 然后在mousemove方法中

var newX = mouseX - 25; // store the new X position based on mouse
var newY = mouseY - 25; // store the new Y position based on mouse

// make sure the newX does not go past the right side of the container
// by keeping whichever is smaller 
// if the newX we got from the mouse is larger than the right side of the container
// then we force the newX to become equal to the right side.
newX = Math.min(newX, limits.right); 

// The same principle applies to the left side but we now need to keep the larger value
// of newX or the left side of the container
newX = Math.max(newX, limits.left);

// same logic for top/bottom 
newY = Math.min(newY, limits.bottom);
newY = Math.max(newY, limits.top);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM