html div溢出隐藏,div溢出但是不隐藏让他停靠。

你的意思是拖拽的过程中不让子元素拖出父元素边界吗?如果是的话,可以参考下面jq代码实现,主要是设置做大可移动的宽高//拖拽

$.fn.wmDrag = function(){

return this.each(function(index, oDiv){

oDiv.draggable = false

oDiv.style.cursor = 'move'

oDiv.onmousedown = function (e) {

if(e.button == 2)return; //过滤右键按下事件

var omousemove = document.onmousemove,

omouseup = document.onmouseup;

//鼠标按下,计算当前元素距离可视区的距离

var disX = e.clientX,

disY = e.clientY,

sty = window.getComputedStyle(oDiv),

transition = sty.transition,

left = parseInt(sty.left),

top = parseInt(sty.top),

$parent = $(oDiv).parent(),

//maxL = document.body.clientWidth - parseInt(sty.width),

maxL = $parent.width() - parseInt(sty.width),

//maxT = document.body.clientHeight - parseInt(sty.height);

maxT = $parent.height() - parseInt(sty.height);

oDiv.style.transition = 'none'

oDiv.style.left = left + 'px'

oDiv.style.top = top + 'px'

oDiv.style.bottom = oDiv.style.right = 'auto'

document.onmousemove = function (e) {

//通过事件委托,计算移动的距离

var l = e.clientX - disX;

var t = e.clientY - disY;

l = left + l

l = l > maxL ? maxL : l < 0 ? 0 : l

t = top + t

t = t > maxT ? maxT : t < 0 ? 0 : t

//移动当前元素

oDiv.style.left = l + 'px';

oDiv.style.top = t + 'px';

};

document.onmouseup = function (e) {

document.onmousemove = omousemove;

document.onmouseup = omouseup;

oDiv.style.transition = transition

sty = window.getComputedStyle(oDiv)

left = parseInt(sty.left)

top = parseInt(sty.top)

let right = parseInt(sty.right),

bottom = parseInt(sty.bottom)

if(left > right){

oDiv.style.right = right + 'px'

oDiv.style.left = 'auto'

}

if(top > bottom){

oDiv.style.bottom = bottom + 'px'

oDiv.style.top = 'auto'

}

};

};

})

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值