首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置可拖动元素的位置

问设置可拖动元素的位置
EN

Stack Overflow用户
提问于 2021-03-28 21:06:07
回答 2查看 50关注 0票数 1

我有一个jquery函数,它允许元素是可拖动和可调整大小的,它工作得很好。我想要添加的是设置元素可以拖动和调整大小的位置。更具体地说,我想让我的元素在代码中两行(v1和vh)的空格之间可拖动。有什么方法可以做到这一点吗?任何帮助都是非常感谢的。提前谢谢。

​

代码语言:javascript
复制
    $(".box").mouseup(function () {
        $(this).find('iframe').fadeIn('slow');
    }).mousedown(function () {
        $(this).find('iframe').hide();
    });

    $(function () {
        $(".box")
            .resizable()
            .draggable();
    });
代码语言:javascript
复制
.box {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 32%;
    height: 30%;
    background-color: #2b2d2f;
    color: black;
    position: absolute;
    top: 1%;    
}

.box h4{
    color: white;
    font-size: 20px;
    text-align:center;   
}

.vl {
    height: 100%;
    position: absolute;
    right: 5%;
    top: 0;
    background-color: red;
    width: 2.5px;
    height: 100%;
    overflow: hidden;
}

.vh {
    width: 100%;
    position: absolute;
    top: 75%;
    background-color: red;
    height: 2.5px;
    width: 100%;
    overflow: hidden;
}
代码语言:javascript
复制
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
        integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"
        integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
    <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
 
</head>
<body>

<div class="box">
<h4>box</h4>
</div>

    <div class="vl">
    </div>
    <div class="vh">
    </div>

</body>
</html>

​

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-03-28 21:20:44

draggable() jQueryUI组件有一个containment参数。它的一个签名允许您提供4个坐标,这些坐标等同于应限制可拖动元素的边界框。

要计算这一点,可以分别检索垂直线和水平线的left和top,减去.box的宽度/高度。试试这个:

​

代码语言:javascript
复制
jQuery($ => {
  let $box = $('.box');
  let $vl = $('.vl');
  let $vh = $('.vh');

  $box
    .resizable()
    .draggable({
      containment: [
        8, // default body padding, amend as necessary
        8, // default body padding, amend as necessary
        Math.ceil(parseFloat($vl.css('left'))) - Math.floor(parseFloat($box.width())),
        Math.ceil(parseFloat($vh.css('top'))) - Math.floor(parseFloat($box.height()))
      ]
    });
});
代码语言:javascript
复制
.box {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 32%;
  height: 30%;
  background-color: #2b2d2f;
  color: black;
  position: absolute;
  top: 1%;
}

.box h4 {
  color: white;
  font-size: 20px;
  text-align: center;
}

.vl {
  height: 100%;
  position: absolute;
  right: 5%;
  top: 0;
  background-color: red;
  width: 2.5px;
  height: 90%;
  overflow: hidden;
}

.vh {
  position: absolute;
  top: 75%;
  background-color: red;
  height: 2.5px;
  width: 95%;
  overflow: hidden;
}
代码语言:javascript
复制
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<div class="box">
  <h4>box</h4>
</div>
<div class="vl"></div>
<div class="vh"></div>

​

票数 1
EN

Stack Overflow用户

发布于 2021-03-28 21:32:53

一种方法是将盒子包装在适合您想要的区域的容器中,并使用containment: "parent"

​

代码语言:javascript
复制
function getContainerDimensions() {
  const buffer = 6 // used in demo to prevent borders overlap
  return {
    width: $('.vl').offset().left - buffer,
    height: $('.vh').offset().top - buffer
  }
}

$(function() {
  const $cont = $('<div id="container">').css(getContainerDimensions())
  $(".box").wrap($cont)
    .resizable({containment: "parent"})
    .draggable({containment: "parent"});
});
代码语言:javascript
复制
body {
  padding: 0;
  margin: 0
}

#container {
  border: 2px solid green
}

.box {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 32%;
  height: 30%;
  background-color: #2b2d2f;
  color: black;
  position: absolute;
  top: 1%;
}

.box h4 {
  color: white;
  font-size: 20px;
  text-align: center;
}

.vl {
  height: 100%;
  position: absolute;
  right: 5%;
  top: 0;
  background-color: red;
  width: 2.5px;
  height: 100%;
  overflow: hidden;
}

.vh {
  width: 100%;
  position: absolute;
  top: 75%;
  background-color: red;
  height: 2.5px;
  width: 100%;
  overflow: hidden;
}
代码语言:javascript
复制
<html>

<head>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
  <link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />

</head>

<body>

  <div class="box">
    <h4>box</h4>
  </div>

  <div class="vl">
  </div>
  <div class="vh">
  </div>

</body>

</html>

​

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66841756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档