Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery HTML将一个div拖动到另一个div中_Jquery_Html_Css - Fatal编程技术网

Jquery HTML将一个div拖动到另一个div中

Jquery HTML将一个div拖动到另一个div中,jquery,html,css,Jquery,Html,Css,我试图创建一个div(绿色div),它可以在另一个div(带边框的小div)的边界内拖动。 这就是我到现在为止一直在尝试的 我将试着用数字来解释 I want this 绿色div应该可以拖动,但限制应该是所有四个角上的小div(在小div中不应该看到空白),如上图所示。您需要使用.draggable的包含属性,下面是如何操作的示例 $(function() { $("#draggable").draggable({ containment: [-100,-10

我试图创建一个div(绿色div),它可以在另一个div(带边框的小div)的边界内拖动。 这就是我到现在为止一直在尝试的

我将试着用数字来解释

I want this


绿色div应该可以拖动,但限制应该是所有四个角上的小div(在小div中不应该看到空白),如上图所示。

您需要使用
.draggable
包含属性,下面是如何操作的示例

$(function() {
    $("#draggable").draggable({ 
        containment: [-100,-100,0,0]
    });
});
查找
容器中使用的
参数
,它是
偏移量
位置
(我忘了是哪一个)相对于
相对位置
,可以拖动
.draggable
的区域,例如使用此
HTML

<div class="bigDiv">
    <div class="smallDiv" id="draggable">
    </div>
</div>
因为
.bigDiv
相对于其
相对位置的
偏移量
top:0
左:0,您需要将
包含到该[1st_参数,2nd_参数,3rd_参数,4th_参数],其中

  • 1st_参数
    是距离
    相对位置最左侧的
    偏移量
    ,因此 因为
    容器宽度
    200px
    ,而
    .draggable
    300px
    ,您需要将其计算为
    容器宽度
    +
    容器
    左偏移量
    -
    。可拖动宽度
    (200+0-300=-100)
  • 2nd param
    是相对于
    相对位置的最上方的
    偏移量
    ,因此 因为
    容器高度
    200px
    ,而
    .draggable
    300px
    ,您需要将其计算到
    容器高度
    +
    容器高度
    偏置顶部
    -
    。可拖动高度
    (200+0-300=-100)
  • 3rd param
    是相对于
    相对位置的左下方
    偏移量
    (只需将
    容器宽度
    之间的差值添加到
    1st_param
  • 4th参数
    是相对于
    相对位置的最低顶部偏移量
    (只需将
    集装箱高度
    之间的差值添加到
    2th参数
这是答案,如果你有点困惑

这是完整的代码,只需编辑
位置
宽度
,以及
高度
,从
.bigDiv
.smallDiv
上取下就可以了。


<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){

//if the div1 is clicked performed this code
$("#div1").mousedown(function(){

$(window).mousemove(function(event) {

 var a=event.pageX;//get the position of x
 var b= event.pageY;//get the position of y
 $("#div1").css("left",a);//change the position-left of div1 and equal it to a
 $("#div1").css("top",b);//change the position-top of div1 and equal it to b

  //if more than the container do this
   if(a>450)
   {
      $("#div1").css("left",450);
   }
   if(b>260)
   {
      $("#div1").css("top",260);
   }
   //if less than the container do this
   if(a<0)
   {
      $("#div1").css("left",0);
   }

   if(b<0)
   {
      $("#div1").css("top",0);
   }
 });

});

});
</script>
</head>
<body>

<div id="div2" style="background-color:green;width:500px;height:300px; position:absolute;">

<div id="div1" style="background-color:red;width:50px;height:40px; position:absolute;"></div>
</div>

</body>
</html>
$(文档).ready(函数(){ //如果单击div1,则执行此代码 $(“#div1”).mousedown(函数(){ $(窗口).mousemove(函数(事件){ var a=event.pageX;//获取x的位置 var b=event.pageY;//获取y的位置 $(“#div1”).css(“left”,a);//更改div1左侧的位置并将其等于a $(“#div1”).css(“top”,b);//更改div1顶部的位置并使其等于b //如果超过容器,则执行此操作 如果(a>450) { $(#div1”).css(“左”,450); } 如果(b>260) { $(#div1”).css(“top”,260); } //如果小于容器,则执行此操作
如果(aDo您的意思是要使绿色的
div
变小,并在带有黑色边框的
div
内拖动?两个div的大小应保持不变,但绿色div不应拖动到带有黑色边框的小div的边框外。这没有任何意义。因为,绿色
div
的大小大于e
div
带黑色边框时,绿色的
div
总是会溢出。溢出不是问题。在小的div中不应该看到空白。您可以使用
包含属性。显示了一个示例。@rishal,您需要稍微调整代码,因为在当前的div中,
边框
>容器的边界将不包括在内,因此如果您使用
1px
的边界,直到更多,当您将其拖动到最左上角时,您将看到很大的空间,尝试将边界设置为
10px
以查看我的意思是,我已经看到了,我只是想要逻辑,现在我正在创建我的计算,谢谢堆。首先我感到困惑你的公式是200+0-300,应该是-100,但你写了100。
body {
    margin: 0;
    padding: 0;
}
.bigDiv{
    border: solid 1px black;
    position: absolute;
    top: 0;
    left: 0;
    height: 200px;
    width: 200px;
    /*overflow: hidden;*/
}

.smallDiv{
    opacity: 0.5;
    background-color: limegreen;
    position: absolute;
    width: 300px;
    height: 300px;
    /*overflow: hidden;*/
}
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){

//if the div1 is clicked performed this code
$("#div1").mousedown(function(){

$(window).mousemove(function(event) {

 var a=event.pageX;//get the position of x
 var b= event.pageY;//get the position of y
 $("#div1").css("left",a);//change the position-left of div1 and equal it to a
 $("#div1").css("top",b);//change the position-top of div1 and equal it to b

  //if more than the container do this
   if(a>450)
   {
      $("#div1").css("left",450);
   }
   if(b>260)
   {
      $("#div1").css("top",260);
   }
   //if less than the container do this
   if(a<0)
   {
      $("#div1").css("left",0);
   }

   if(b<0)
   {
      $("#div1").css("top",0);
   }
 });

});

});
</script>
</head>
<body>

<div id="div2" style="background-color:green;width:500px;height:300px; position:absolute;">

<div id="div1" style="background-color:red;width:50px;height:40px; position:absolute;"></div>
</div>

</body>
</html>