javascript - 第一次尝试Jquery-Draggable跳转容器

标签 javascript jquery html jquery-ui

我正在使用 JQuery UI 来制作可拖动的弹出窗口并在代码中

在第一个拖动中,弹出窗口跳转到左上角。

$( ".box" ).css({"top": 0,
                 "bottom": 0,
                 "left": 0,
                 "right": 0,
                 "margin": "auto"
                });
                
$( ".box" ).draggable();
.box{
  
    left:0px;
    top:0px;
    right:0px;
    bottom:0px;
    width: 150px;
    height: 150px;
    box-shadow: inset 0 0 20px;
    position:fixed;
}

#containment-wrapper{
   width:300px;
   height:300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="containment-wrapper">
    <div class="box"></div>
</div>

enter link description here

最佳答案

这是因为您将其位置设置为 0,0 删除 CSS 位置将解决您的问题。

/*$( ".box" ).css({"top": 0,
          "bottom": 0,
          "left": 0,
          "right": 0,
          "margin": "auto"
});*/
$(".box").draggable();
.box {
  /*left:0px;
    top:0px;
    right:0px;
    bottom:0px;*/
  float: left;
  margin: auto;
  width: 100px;
  height: 100px;
  box-shadow: inset 0 0 20px;
  position: fixed;
}

#containment-wrapper {
  width: 300px;
  height: 300px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<div id="containment-wrapper">
  <div class="box"></div>
</div>

关于javascript - 第一次尝试Jquery-Draggable跳转容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43391988/

相关文章:

javascript - 如何使用jQuery(或正确地)将变量e 'valCookie'的数据添加到数组中?

Javascript innerhtml 在第二次函数调用未定义函数时不显示

java - 更改 "overflow"样式时如何停止重新加载 Java 小程序

python - 服务器过载或 CGI 脚本中有错误

html - 批量系统信息?

javascript - 如何隐藏表格行中的值并在单击加号时显示它

javascript - AngularJs - 将一个 ng-model 绑定(bind)到具有两个输入的指令

JavaScript/jQuery 插件 : snap to grid

javascript - 删除以前在 jQuery 中添加的元素

javascript - localstorage 在 localstorage[key] += 1 上返回 11