简体   繁体   English

如何限制可拖动区域? 它在顶部和左侧起作用,但在右侧和底部不起作用

[英]How do I limit draggable area? it functions on top and left side, but not on the right side and bottom

So I tried to block the draggable contents from overflowing the body.所以我试图阻止draggable的内容overflowing身体。

It works on top and on the left side.适用于顶部和左侧。

I can't limit the right side and bottom.不能限制右侧和底部。

            e.pageX -= e.offsetX;
            e.pageY -= e.offsetY;

            // left/right constraint
            if (e.pageX - dragoffset.x < 0) {
                offsetX = 0;
            } else if (e.pageX + dragoffset.x > document.body.clientWidth) {
                offsetX = document.body.clientWidth - e.target.clientWidth;
            } else {
                offsetX = e.pageX - dragoffset.x;
            }

            // top/bottom constraint

            if (e.pageY - dragoffset.y < 0) {
                offsetY = 0;
            } else if (e.pageY + dragoffset.y > document.body.clientHeight) {
                offsetY = document.body.clientHeight - e.target.clientHeight;
            } else {
                offsetY = e.pageY - dragoffset.y;
            }   

            el.style.top = offsetY + "px";
            el.style.left = offsetX + "px";
        }
    });
};

Also, my divs are getting glitchy while I drag them around.此外,当我拖动它们时,我的 div 会出现故障 They only stop on the right side and bottom when the text inside them is selected. They only stop on the right side and bottom when the text inside them is selected.

There is drag&drop library with feature to restrict movements of draggables有一个拖放库,具有限制可拖动对象移动的功能

https://github.com/dragee/dragee https://github.com/dragee/dragee

By default it restrict movements of element inside container .默认情况下,它限制container内元素的移动。 It take into account size of element.它考虑了元素的大小。

import { Draggable } from 'dragee'
new Draggable(element, { container: document.body })

In same time it's possible to use custom bound function同时可以使用自定义绑定 function

import { Draggable, BoundToRectangle, Rectangle } from 'dragee'
new Draggable(element, { 
    bound: BoundToRectangle.bounding(Rectangle.fromElement(document.body, document.body))
})

where BoundToElement describe restrictions that you need其中BoundToElement描述您需要的限制

class BoundToRectangle {
  constructor(rectangle) {
    this.rectangle = rectangle
  }

  bound(point, size) {
    const calcPoint = point.clone()
    const rectP2 = this.rectangle.getP3()

    if (this.rectangle.position.x > calcPoint.x) {
      (calcPoint.x = this.rectangle.position.x)
    }
    if (this.rectangle.position.y > calcPoint.y) {
      calcPoint.y = this.rectangle.position.y
    }
    if (rectP2.x < calcPoint.x + size.x) {
      calcPoint.x = rectP2.x - size.x
    }
    if (rectP2.y < calcPoint.y + size.y) {
      calcPoint.y = rectP2.y - size.y
    }

    return calcPoint
  }
}

Size of element you can calculate next way:您可以通过以下方式计算元素的大小:

let width = parseInt(window.getComputedStyle(element)['width'])
let height = parseInt(window.getComputedStyle(element)['height'])

I can also suggest to use translate3d property for movements instead of positions left and top, because it is more efficient我还可以建议将translate3d属性用于移动而不是位置 left 和 top,因为它更有效

I assume that since the draggable item is a dom element it will push the width and height of the body when moved right or down, this changes the body size, so using the body size might not be the right way.我假设由于可拖动项目是一个 dom 元素,它会在向右或向下移动时推动主体的宽度和高度,这会改变主体大小,因此使用主体大小可能不是正确的方法。 Maybe you can use window.width and window.height or so to restrict the area with values the draggable item cannot change.也许您可以使用window.widthwindow.height左右来限制可拖动项目无法更改的值的区域。 Hope this helps.希望这可以帮助。

thank you, I'll try!谢谢,我试试! I hope it works我希望它有效

暂无
暂无

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

相关问题 如何正确地从各个侧面(顶部,左侧,底部,右侧)创建2d对象碰撞 - How do I create 2d object collision from every side(top,left,bottom,right) properly 如何在表格的左侧和右侧添加阴影 - how do i add shadow on left and right side of column of table 如何检测右上左下屏幕区域JavaScript - How to detect top right bottom left screen area JavaScript 如何限制可拖动的孩子的父项的权利和最低价值? - How can I limit a draggable child with right and bottom value of parent? 从左侧和右侧计算溢出的列表项。 (不是顶部/底部) - Count overflowed List items from both Left and Right side. (Not top/bottom) 如何使要滑动的图像从左侧0,0位置向右端移动到右侧? - How do i make the image to slide move to the right from left 0,0 position to the end right side? 如何在输入矩形的左下角正下方获得 x 和 y position? - how do i get the x and y position directly under the left bottom side of the input rectangle? 如何在地图上显示左上,右下的经度和纬度? - How do I get the top left, bottom right latitude and longitude being shown in the map? 如何获得绝对定位元素的左/右/上/下的实际值? - How do I get the actual values for left/right/top/bottom of an absolutely positioned element? 如何在JQuery中横向(从左到右,反之亦然)使图像褪色? - How do I make an image fade in sideways (left to right side & vice versa) in JQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM