首页 \ 问答 \ jQuery如何在鼠标仍处于运行状态时触发拖动事件(jQuery How to fire the drag event while mouse is still down)

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
更新时间:2023-06-19 09:06

最满意答案

在编码(写入)对象时,可以尝试使用UIImageJPEGRepresentationUIImagePNGRepresentation 。 并且在解码(读取)时只使用[UIImage imageWithData:data]

注意:如果您经常阅读和写回对象,则应使用PNG版本。 Jpeg会使图像变得越来越糟

注意2:哦,是的,如果图像很大,你不应该将它们存储在数据库中,而是存储在磁盘上,只需将数据存储在数据库中


You can try to use UIImageJPEGRepresentation or UIImagePNGRepresentation 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

相关问答

更多
  • 我遇到了这个问题并且永远无法修复它我认为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 ...
  • 从技术上讲,您可以在运行时创建实体框架模型,至少通过动态创建程序集并使用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) { ...
  • 我认为唯一正确的方法是使用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 ...
  • 在编码(写入)对象时,可以尝试使用UIImageJPEGRepresentation或UIImagePNGRepresentation 。 并且在解码(读取)时只使用[UIImage imageWithData:data] 。 注意:如果您经常阅读和写回对象,则应使用PNG版本。 Jpeg会使图像变得越来越糟 注意2:哦,是的,如果图像很大,你不应该将它们存储在数据库中,而是存储在磁盘上,只需将数据存储在数据库中 You can try to use UIImageJPEGRepresentation or ...
  • 您的基础模型类不应从数据库访问类继承。 相反,它应该使用数据库类(或映射器,谁说它必须始终是一个数据库?)然后提供ORM方法(获取,插入,更新等...)。 正如其他人发布的那样,您应该使用众多优秀的预构建解决方案之一。 至于寻找轻量级ORM,其他人之前已经问过这个问题。 以下是其中一个问题的链接: https://stackoverflow.com/questions/1995834/looking-a-lightweight-php-orm Your base model class shouldn't ...
  • 将模型与其图像文件分离并使用模型图像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从模型中生成这些字段,但原则上您可以为此添加字段。 将字段放入类中的另 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。