javascript 可拖动/可移动 <div> 水平

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15801886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:09:21  来源:igfitidea点击:

draggable / moveable <div> horizontally

javascriptjquery

提问by IMUXIxD

How can I make a div that is draggable / moveable horizontally, but not vertically? I would prefer if this could be done without JQuery UI. JQuery is fine. Or vanilla JS.

如何制作可水平拖动/可移动但不能垂直移动的 div?如果没有 JQuery UI 就可以做到这一点,我更愿意。jQuery 很好。或香草JS。

回答by basarat

Creating such a behavior yourself would be quite a bit of work. I would recommend using jquery ui :

自己创建这样的行为将是相当多的工作。我建议使用 jquery ui :

http://jsfiddle.net/basarat/f7nNf/1/

http://jsfiddle.net/basarat/f7nNf/1/

Its a single line of code:

它的一行代码:

$( "#draggable" ).draggable({ axis: "x" });

And you don't event need to download the complete jquery ui. You can do a custom build with only draggable here : http://jqueryui.com/download/

而且您不需要下载完整的 jquery ui。您可以在此处进行仅可拖动的自定义构建:http: //jqueryui.com/download/

Read more about draggable here: http://jqueryui.com/draggable/#constrain-movement

在此处阅读有关可拖动的更多信息:http: //jqueryui.com/draggable/#constrain-movement