jQuery如何在鼠标仍处于运行状态时触发拖动事件(jQuery How to fire the drag event while mouse is still down)
好吧,我已经玩了好几个小时了,现在仍然没有骰子。 我正在创建一个界面,允许您拖放图标(带有图像的div)。 我正在使用jQuery UI脚本,因为他们尝试并测试了div拖动功能。
我的问题是你不能在它的父div之外拖动一个div。 为了解决这个问题,我正在创建一个可拖动的div,当用户触发mousedown事件时,可以根据需要附加到文档正文。 但我无法弄清楚如何触发所需的拖动事件,以便用户可以立即开始拖动这个新创建的div。
总之我需要; 鼠标按下 - >调用函数创建可拖动的div - >在鼠标仍然按下时拖动div。
我确信这很简单。 任何帮助将非常感激。
这是我的问题的一个例子,它需要运行的是jQuery UI脚本,可以在上面的链接中找到。
<html> <head> <script src="../../jquery-1.6.2.js"></script> <script src="../../ui/jquery.ui.core.js"></script> <script src="../../ui/jquery.ui.widget.js"></script> <script src="../../ui/jquery.ui.mouse.js"></script> <script src="../../ui/jquery.ui.draggable.js"></script> <style> #inactive_dragable { width: 50px; height: 50px; padding: 0.5em; background-color: green; } .test_drag { width: 50px; height: 50px; padding: 0.5em; background-color: red; position: absolute; } </style> <script> function createDraggableDiv(e){ if (!e) var e = window.event; jQuery('<div/>', { id: 'tester', }).appendTo('#page_wrap'); var isIE = document.all ? true : false; var _x,_y; if (!isIE) { _x = e.pageX; _y = e.pageY; } if (isIE) { _x = e.clientX + document.body.scrollLeft; _y = e.clientY + document.body.scrollTop; } var boundry = $('#page_wrap').offset(); $('#tester').addClass('test_drag'); $('#tester').html("New div to drag"); $('#tester').css("top", _y + "px"); $('#tester').css("left", _x + "px"); $('#tester').draggable(); // Now how do I make the new div immediately start dragging? // ... } </script> </head> <body> <div id="page_wrap"> <div class="demo" style="width: 300px; height: 200px; background-color: blue; overflow-y: auto; overflow-x: hidden;"> <div onmousedown="createDraggableDiv(event);" id="inactive_dragable" class="ui-widget-content"> <p>Drag me around</p> </div> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</div> </div> </body> </html>
当你点击div我希望能够创建一个新的div并立即拖动它。
Ok I've been playing around with this for hours now and still no dice. I'm creating an interface allowing you to drag and drop an icon(a div with an image inside). I'm using the jQuery UI scripts for they're tried and tested div dragging functionality.
My problem is you can not drag a div outside it's parent div. To get around this I'm creating a dragable div, appended to the document body, on demand when a user fires a mousedown event. But I can't figure out how to fire off the drag event needed so the user can immediately start dragging this newly created div around.
In short I need to; mouse down -> call function to create draggable div -> drag the div around while the mouse is still down.
I'm sure it's something simple. Any help would be much appreciated.
Here is an example of my problem, all it needs to run are the jQuery UI scripts which can be found at the link above.
<html> <head> <script src="../../jquery-1.6.2.js"></script> <script src="../../ui/jquery.ui.core.js"></script> <script src="../../ui/jquery.ui.widget.js"></script> <script src="../../ui/jquery.ui.mouse.js"></script> <script src="../../ui/jquery.ui.draggable.js"></script> <style> #inactive_dragable { width: 50px; height: 50px; padding: 0.5em; background-color: green; } .test_drag { width: 50px; height: 50px; padding: 0.5em; background-color: red; position: absolute; } </style> <script> function createDraggableDiv(e){ if (!e) var e = window.event; jQuery('<div/>', { id: 'tester', }).appendTo('#page_wrap'); var isIE = document.all ? true : false; var _x,_y; if (!isIE) { _x = e.pageX; _y = e.pageY; } if (isIE) { _x = e.clientX + document.body.scrollLeft; _y = e.clientY + document.body.scrollTop; } var boundry = $('#page_wrap').offset(); $('#tester').addClass('test_drag'); $('#tester').html("New div to drag"); $('#tester').css("top", _y + "px"); $('#tester').css("left", _x + "px"); $('#tester').draggable(); // Now how do I make the new div immediately start dragging? // ... } </script> </head> <body> <div id="page_wrap"> <div class="demo" style="width: 300px; height: 200px; background-color: blue; overflow-y: auto; overflow-x: hidden;"> <div onmousedown="createDraggableDiv(event);" id="inactive_dragable" class="ui-widget-content"> <p>Drag me around</p> </div> Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</div> </div> </body> </html>
When you click on the div I want to be able to create a new div and drag that around instantly.
原文:https://stackoverflow.com/questions/8759884
最满意答案
在编码(写入)对象时,可以尝试使用
UIImageJPEGRepresentation
或UIImagePNGRepresentation
。 并且在解码(读取)时只使用[UIImage imageWithData:data]
。注意:如果您经常阅读和写回对象,则应使用PNG版本。 Jpeg会使图像变得越来越糟
注意2:哦,是的,如果图像很大,你不应该将它们存储在数据库中,而是存储在磁盘上,只需将数据存储在数据库中
You can try to use
UIImageJPEGRepresentation
orUIImagePNGRepresentation
when to encode (writing) your objects. And just use[UIImage imageWithData:data]
when decoding (reading).Note: If you will be reading and writing back objects often you should use the PNG version. Jpeg will make the image worse and worse
Note 2: Oh yes, if the images are large you shouldn't store them in your db but rather on disk and just store a reference in the db
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
我遇到了这个问题并且永远无法修复它我认为VS无法管理其内部的Mysql数据库服务器资源管理器的数据集/绑定。 即使在WPF中,我也不得不手动为WPF绑定构建数据集对象,这对于屁股来说是一种痛苦。 如果可以的话,坚持使用MSSQL或学习如何用C#编程MySQL,这并不好 I have had this problem and was never able to fix it, I think VS just can't manage the Mysql Database inside its server e ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
即时创建模型(Creating model on the fly)[2021-08-12]
从技术上讲,您可以在运行时创建实体框架模型,至少通过动态创建程序集并使用apis和代码首次运行的属性或通过为模型创建所需的xml来实现。 但是你不需要在运行时创建模型类。 事实上,在运行时创建模型类是无用的,因为您可以创建模型以在编译时和运行时键入。 您可以创建模型,因为您希望将Type1传递给方法,或者编写像.Where(x=>x.SomeFiled == SomeValue)这样的类型化查询。 如果您针对不同的客户有不同的应用程序实例 如果您针对不同的客户端有不同的应用程序实例,则不需要执行任何特定的操 ... -
要获取用户数据中的子节点数据这是Student(年份,课程)中的值,您需要创建如下的模型类: public class User { private String name; private String thumbnail; private Student student; public String getName() { return name; } public void setName(String name) { ...
-
Emberjs如何在不创建模型的情况下访问类/构造函数属性(Emberjs How to access a class/constructor attributes without creating a model)[2021-12-28]
我认为唯一正确的方法是使用modelFor上的modelFor函数 。 你是否也意识到._keys.list; 是完全私人的API? 这只适用于这个特定的Map polypill。 有一个简单的公共API。 使用.keys()与扩展运算符结合使用: const attrs = [...store.modelFor('feedback').get('attributes').keys()]` Finally the good way to do it is to import the file into m ... -
使用图像创建模型类(creating a model class with an image)[2022-08-19]
在编码(写入)对象时,可以尝试使用UIImageJPEGRepresentation或UIImagePNGRepresentation 。 并且在解码(读取)时只使用[UIImage imageWithData:data] 。 注意:如果您经常阅读和写回对象,则应使用PNG版本。 Jpeg会使图像变得越来越糟 注意2:哦,是的,如果图像很大,你不应该将它们存储在数据库中,而是存储在磁盘上,只需将数据存储在数据库中 You can try to use UIImageJPEGRepresentation or ... -
使用PDO创建模型层(Creating a Model Layer with PDO)[2023-03-13]
您的基础模型类不应从数据库访问类继承。 相反,它应该使用数据库类(或映射器,谁说它必须始终是一个数据库?)然后提供ORM方法(获取,插入,更新等...)。 正如其他人发布的那样,您应该使用众多优秀的预构建解决方案之一。 至于寻找轻量级ORM,其他人之前已经问过这个问题。 以下是其中一个问题的链接: https://stackoverflow.com/questions/1995834/looking-a-lightweight-php-orm Your base model class shouldn't ... -
Objective C模型 - 如何在模型类中下载图像?(Objective C model - How to download image inside a model class?)[2019-10-18]
将模型与其图像文件分离并使用模型图像URL的密钥将图像保存在缓存中是更好的方法。 此外,将初始化方法放在MyCustomCollectionViewCell中是配置单元格的更好方法。 我建议你使用https://github.com/rs/SDWebImage (或类似的库)来下载和缓存图像。 MyCustomCollectionViewCell.h: @interface MyCustomCollectionViewCell : UITableViewCell -(void) initializeWit ... -
您正在创建一个ModelForm子类。 模型表单必须有一个模型Meta , Meta对象配置它。 像这样的配置被分组到Meta类中以避免名称冲突; 这样,您可以在表单中放置model 字段 ,而不会干扰配置。 换句话说,通过使用class Meta:您将获得一个嵌套的命名空间,用于配置与模型相关的ModelForm 。 ModelForm类主体本身的名称空间(在Meta之外)保留给表单字段本身以及表单方法。 您通常只是让ModelForm从模型中生成这些字段,但原则上您可以为此添加字段。 将字段放入类中的另 ...