简体   繁体   English

jQuery可拖动,使div不会超出其他div框架

[英]jquery draggable, making a div not to go outside of the other div frame

i need a draggable div not to go outside of second div frame, so far i managed to make a "collision" basically returning true or false if draggable div is inside the frame of other div. 我需要一个可拖动的div不能超出第二个div框架,到目前为止,如果可拖动的div在另一个div的框架内,我设法使“碰撞”基本上返回true或false。 So the thing now is that i cant get this to work, i was trying to get it to ax = 90(example) when it hits the frame and few more examples , but i just can't get this to work. 所以现在的问题是,我无法使它正常工作,当它碰到框架和更多示例时,我试图将其设为ax = 90(example),但我只是无法使其正常工作。 The draggable div doesn't want to go back to position. 可拖动的div不想回到原位。

Here is a JSFiddle: https://jsfiddle.net/kojaa/x80wL1mj/2/ 这是一个JSFiddle: https://jsfiddle.net/kojaa/x80wL1mj/2/ ://jsfiddle.net/kojaa/x80wL1mj/2/

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="style.css">
    <title>Catch a ball</title>
</head>
<body>
    <div class="catcherMovableArea" id="catcherMovableArea">
        <div id="catcher" class="catcher"></div>
    </div>
    <script src="script.js"></script>
</body>
</html>

.catcherMovableArea {
    position: absolute;
    border: 1px solid black;
    width: 1900px;
    top: 90%;
    height: 50px;
}

.catcher {
    position: absolute;
    width: 250px;
    height: 30px;
    border-radius: 10px;
    background-color: black;
    top: 20%;
}


let catcher = $("#catcher");

$(document).mousemove(function(e) {
    catcher.position({
      my: "left-50% bottom+50%",
      of: e,
      collision: "fit"
    });

    let catcherOffset = $(catcher).offset();
    let CxPos = catcherOffset.left;
    let CyPos = catcherOffset.top;

    let catcherYInMovableArea, catcherXInMovableArea = true;

    while(!isCatcherYinMovableArea(CyPos)){
        catcherYInMovableArea = false;
        break;
    }

    while(!isCatcherXinMovableArea(CxPos)){
        catcherXInMovableArea = false;
        break;
    }
});

function isCatcherYinMovableArea(ypos){
    if(ypos < 849.5999755859375 || ypos > 870.5999755859375) {
        return false;
    } else {
        return true;
    }
}

function isCatcherXinMovableArea(xpos){
    if(xpos < 8 || xpos > 1655 ) {
        return false;
    } else {
        return true;
    }
}

By default, the collision option will prevent the element from being placed outside of the window . 默认情况下, collision选项将阻止将元素放置在window之外。 You want to prevent it from moving outside of a specific element. 您要防止它移到特定元素之外。

To do this, use the within option to select which element should be used for containment. 为此,请使用“ within选项来选择应将哪个元素用于容纳。

Example: 例:

 let draggable = $("#draggable"); $(document).mousemove(function(e) { draggable.position({ my: "left-50% bottom+50%", of: e, collision: "fit", within: "#container" }); }); 
 #container { width: 200px; height: 100px; border-style: solid } #draggable { width: 50px; height: 50px; background-color: black } 
 <script src="https://code.jquery.com/jquery-3.3.1.js"></script> <script src="https://code.jquery.com/ui/1.12.0-rc.1/jquery-ui.js"></script> <div id="container"> <div id="draggable"></div> </div> 

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

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