如何禁止 div 的“左”财产不超过指定数量

发布于 2024-08-15 01:42:05 字数 1028 浏览 2 评论 0原文

我正在使用 这个 小型拖动插件将 div.slider 水平拖动到其 div.container 上。 我不希望 div.slider 能够超出 div.container 的边界(不希望它退出)。 因此,当 div.slider 到达其容器 div.container 的最右侧时,我希望它不允许用户将其进一步向右拖动,以便它保留在容器内。

这是我的jquery:

    $(".slider").mouseup(function() {
        var leftStyleValue = $('div.slider').css('left');
        $('.display_value').html(leftStyleValue);

        if (parseInt($('.slider').css('left')) > 100) {
            $('#completion').text("YOU DID IT!");
        }
        else {
            $(".slider").animate({left: 0
            }, 500);
        }

    });

如果我将“mouseup”更改为mousemove,那么它甚至不允许您拖动它,因为它会检查每个像素的移动。现在,我可以将 div.slider 拖到 div.container 之外,当我放开鼠标按钮时,因为我没有在 div.container 上执行 mouseup,所以它什么也不做。

如果您想查看插件代码,请转到上面的站点,复制压缩的插件代码,然后转到 < a href="http://jsbeautifier.org/" rel="nofollow noreferrer">http://jsbeautifier.org/ 并查看未压缩版本。

I'm using this tiny drag plugin to drag div.slider horizontally across it's div.container.
I do not want div.slider to be able to go past the bounds(don't want it to exit) div.container.
so when div.slider reaches the very right side of it's container div.container, i want it to not allow the user to drag it any further right, so that it stays inside of it's container.

here's my jquery:

    $(".slider").mouseup(function() {
        var leftStyleValue = $('div.slider').css('left');
        $('.display_value').html(leftStyleValue);

        if (parseInt($('.slider').css('left')) > 100) {
            $('#completion').text("YOU DID IT!");
        }
        else {
            $(".slider").animate({left: 0
            }, 500);
        }

    });

If i change "mouseup" to mousemove, then it wont allow you to even drag it since it's checking on every pixel move. right now, i can drag div.slider outside of div.container and when i let go of the mousebutton, since i'm not doing a mouseup on div.container, it does nothing.

If you want to see the plugin code, go to the site above, copy the compressed plugin code, then go to http://jsbeautifier.org/ and see the uncompressed version.

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

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

发布评论

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

评论(1

他夏了夏天 2024-08-22 01:42:05

jQuery UI 的可拖动功能通过 containment 属性支持此功能,您可能需要检查一下以免您修改插件的源代码。

请注意,如果您除了可拖动之外不需要任何其他内容,则可以自定义 jQuery UI 下载包。

编辑

经过一番思考,我想你可以用你的方法做一些修改。您必须:

  1. 将一个函数绑定到 .slider 元素的 mousedown 事件以捕获拖动的开始
  2. 计算最大左侧位置(以及其他必要的位置)
  3. 绑定到 mousemove 事件,检测元素现在所在的位置,检查是否超出限制,如果超出,则将其CSS调整到最大左侧位置

问题出在步骤3中。由于插件也绑定了mousemove事件并不断更改元素的CSS,因此您可能会遇到一些闪烁或奇怪的行为,因为两个 mousemove 函数试图用不同的值编辑 CSS。这就是我建议您编辑插件本身的原因。

无论如何,这是代码的修改版本,应该可以在某种程度上可接受:

$(".slider").mousedown(function() {    
    var $slider = $(this);

    // Get maximum x-position. 
    // The max_x position is equal to the sliders width from the container's right side.
    var max_x = $slider.parent().offset().left + $slider.parent().width() - $slider.width();

    // Track mouse position
    $().mousemove(function(e) {
        if(e.pageX > max_x) {
            $slider.css({
                left: max_x
            });
        }
    });

    // Unbind events to $() to prevent multiple bindings
    $().one('mouseup', function() {
        $().unbind();
    });
});

请注意,如果不检查插件的源代码,我不能保证它可以工作。这就是为什么在插件内实现此行为是明智的。

通常,在 mousemove 和 mousedown 事件之后返回 false; 是明智的,以防止浏览器行为,但我不确定它是否会与插件本身冲突。

而且这个函数只考虑右边界,它会允许元素从容器的其他侧面穿过。但我想如果需要的话你将能够实现这些。

jQuery UI's draggable supports this via containment property, you might want to check that out to save you from modifying the plugins's source code.

Note that you can do a custom download package of jQuery UI if you don't need anything else than the draggable.

EDIT

After some thinking, I guess you can make it with your method with some modifications. You have to:

  1. Bind a function to the .slider element's mousedown event to capture the start of dragging
  2. Calculate maximum left position (and others if necessary)
  3. Bind to mousemove event, detect the position where the element is now, check if it's over the limit, if it is, adjust it's css to maximum left position

The problem lies within step 3. As the plugin has bound to the mousemove event also and changes the element's css constantly, you might run into some flickering or weird behaviour as the two mousemove functions are trying to edit the CSS with different values. This is the reason why I suggested you to edit the plugin itself.

Anyways, here's a modified version of your code that should work somewhat acceptably:

$(".slider").mousedown(function() {    
    var $slider = $(this);

    // Get maximum x-position. 
    // The max_x position is equal to the sliders width from the container's right side.
    var max_x = $slider.parent().offset().left + $slider.parent().width() - $slider.width();

    // Track mouse position
    $().mousemove(function(e) {
        if(e.pageX > max_x) {
            $slider.css({
                left: max_x
            });
        }
    });

    // Unbind events to $() to prevent multiple bindings
    $().one('mouseup', function() {
        $().unbind();
    });
});

Note that without examining the plugin's source code, I cannot guarantee that this works. That's why it would be wise to implement this behaviour inside the plugin.

Usually it would be wise to return false; after mousemove and mousedown events to prevent browser behaviour, but I'm not sure whether it'll confilct with the plugin itself.

And this function only takes account the right boundary, it will allow the element to pass through from other sides of the container. But I guess you'll be able to implement those if needed.

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