gpt4 book ai didi

jquery - 使用mousemove时如何阻止div向上移动?

转载 作者:太空宇宙 更新时间:2023-11-04 06:38:38 24 4
gpt4 key购买 nike

我试图让盒子不被拖上去,但可以被拖下,这是我想出的但我不知道如何保持原来的位置所以我有一个引用添加它到 if 检查。它只会在您稍微滚动主体后出现。

jsfiddle

window.addEventListener("scroll",function(){
if($(document).scrollTop() > 100){
$( ".box" ).slideDown({
start: function () {
$(this).css({
display: "flex"
})
}
});
}

var top = 0;
var divOverlay = document.getElementsByClassName('box')[0];
var is_clicked = false;

divOverlay.addEventListener('mousedown', function(e){
is_clicked = true;
top = divOverlay.offsetTop - e.clientY
}, true);

document.addEventListener('mouseup', function(){
is_clicked = false;
}, true);

document.addEventListener('mousemove', function(e){
event.preventDefault();

// add a check here so it doesn't allow dragging the container up?
if(is_clicked){
divOverlay.style.top = (e.clientY + top) + 'px';

// if the box reaches 50px down, it should automatically hide it if the user
//$('.box').slideUp();
}
}, true);

})

最佳答案

您可以尝试使用 Math.max 将框的最大顶部位置限制为视口(viewport)高度减去框高度:

document.addEventListener('mousemove', function(e){
event.preventDefault();

// add a check here so it doesn't allow dragging the container up?
var maxTop = window.innerHeight - divOverlay.offsetHeight;
if(is_clicked){
divOverlay.style.top = Math.max(maxTop, e.clientY + top) + 'px';

// if the box reaches 50px down, it should automatically hide it if the user
//$('.box').slideUp();
}
}, true);

关于jquery - 使用mousemove时如何阻止div向上移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53999520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com