如何禁止 div 的“左”财产不超过指定数量
我正在使用 这个 小型拖动插件将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jQuery UI 的可拖动功能通过
containment
属性支持此功能,您可能需要检查一下以免您修改插件的源代码。请注意,如果您除了可拖动之外不需要任何其他内容,则可以自定义 jQuery UI 下载包。
编辑
经过一番思考,我想你可以用你的方法做一些修改。您必须:
.slider
元素的 mousedown 事件以捕获拖动的开始问题出在步骤3中。由于插件也绑定了mousemove事件并不断更改元素的CSS,因此您可能会遇到一些闪烁或奇怪的行为,因为两个 mousemove 函数试图用不同的值编辑 CSS。这就是我建议您编辑插件本身的原因。
无论如何,这是代码的修改版本,应该可以在某种程度上可接受:
请注意,如果不检查插件的源代码,我不能保证它可以工作。这就是为什么在插件内实现此行为是明智的。
通常,在 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:
.slider
element's mousedown event to capture the start of draggingThe 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:
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.