javascript 向下拖动元素。第一次拖动后跳转

发布于 2024-12-25 20:01:10 字数 1096 浏览 0 评论 0原文

我编写了这段代码,当您单击该元素时,它上面的元素将调整大小更大或更小

当我单击它时,它似乎很好地向下拖动一次,然后尝试再次向下拖动会导致它从屏幕上下来。

function pulldown(element){
var puller = document.getElementById(element);
puller.addEventListener("mousedown", function(e){
    var boxStyle = document.getElementById("resizeBox").getAttribute("style");
    var currentSize = (boxStyle.match(/\d+/));
    var ypos = e.clientY;
    var resize = document.getElementById("resizeBox");
    resize.style.height = currentSize;

    function watchPull(e){
        number2 = currentSize + (e.clientY - ypos);
        resize.style.height = number2+"px";
    }
    document.addEventListener("mousemove", watchPull,false);

    document.addEventListener("mouseup", function(e){
        document.removeEventListener("mousemove", watchPull, false);
        number = currentSize + (e.clientY - ypos);
        resize.style.height = number+"px";
    },false)
},false);
}
pulldown("pullDown");

这就是正在发生的事情。

http://jsfiddle.net/jamcoupe/4hKg8/ (点击“无项目”,然后向下或向上拖动黑线)

I wrote this code that when you click on the element the element above it will resize to be bigger or smaller

When I click on it it seems to drag down nicely once and then trying to drag down again causes it to go down off the screen.

function pulldown(element){
var puller = document.getElementById(element);
puller.addEventListener("mousedown", function(e){
    var boxStyle = document.getElementById("resizeBox").getAttribute("style");
    var currentSize = (boxStyle.match(/\d+/));
    var ypos = e.clientY;
    var resize = document.getElementById("resizeBox");
    resize.style.height = currentSize;

    function watchPull(e){
        number2 = currentSize + (e.clientY - ypos);
        resize.style.height = number2+"px";
    }
    document.addEventListener("mousemove", watchPull,false);

    document.addEventListener("mouseup", function(e){
        document.removeEventListener("mousemove", watchPull, false);
        number = currentSize + (e.clientY - ypos);
        resize.style.height = number+"px";
    },false)
},false);
}
pulldown("pullDown");

here is what is happening.

http://jsfiddle.net/jamcoupe/4hKg8/
(click on "no items" and then drag the black line down or up)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

注定孤独终老 2025-01-01 20:01:10

解决了!

把我的头发拔出来后就是这样!

function pullbox() {
var pull;
pull = document.getElementById("pullDown");
pull.addEventListener("mousedown", function (e) {
    e.preventDefault();
    var currentSize = new Number((document.getElementById("resizeBox").getAttribute("style").match(/\d+/)));
    var yClick = e.clientY;
    function mouseMove(e) {
        if(currentSize < 0){
            currentSize = 0;
        }
        e.stopPropagation();
        var newTotalMove = ((e.clientY - yClick) + currentSize);
        document.getElementById("resizeBox").setAttribute("style", "height:" + newTotalMove + "px");
    }
    function mouseUp() {
        document.removeEventListener("mousemove", mouseMove, false);
        document.removeEventListener("mouseup", moseUp, false);
    }
    document.addEventListener("mousemove", mouseMove, false);
    document.addEventListener("mouseup", mouseUp, false);
}, false);
}
pullbox();

http://jsfiddle.net/jamcoupe/4hKg8/1/

Solved it!

After pulling my hairs out here is it!

function pullbox() {
var pull;
pull = document.getElementById("pullDown");
pull.addEventListener("mousedown", function (e) {
    e.preventDefault();
    var currentSize = new Number((document.getElementById("resizeBox").getAttribute("style").match(/\d+/)));
    var yClick = e.clientY;
    function mouseMove(e) {
        if(currentSize < 0){
            currentSize = 0;
        }
        e.stopPropagation();
        var newTotalMove = ((e.clientY - yClick) + currentSize);
        document.getElementById("resizeBox").setAttribute("style", "height:" + newTotalMove + "px");
    }
    function mouseUp() {
        document.removeEventListener("mousemove", mouseMove, false);
        document.removeEventListener("mouseup", moseUp, false);
    }
    document.addEventListener("mousemove", mouseMove, false);
    document.addEventListener("mouseup", mouseUp, false);
}, false);
}
pullbox();

http://jsfiddle.net/jamcoupe/4hKg8/1/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文