jQuery拖动div选定的文本 [英] jQuery dragging divs selected text
问题描述
我使用jquery在网站上制作几个div。我的代码与此类似:
$(#thediv)。bind('mousedown',function(){
$(document).bind('mousemove',function(ms){
// drag
})
});
问题是,如果鼠标对于div太快,鼠标会移出div 。这会在屏幕上选择随机内容(将其突出显示为蓝色)。它没有给出平稳的拖动效果。有没有一个很好的方法来解决这个问题? jQuery的ui拖动插件没有这个问题。我不想使用它,因为它是一个大文件。
谢谢
为了防止在页面中选择元素,只需从函数中返回false(onmousedown,onmousemove)。
var mouseMove = function(){
//此函数不需要jquery,只需使用此模式:移动div
返回false;
}
var mouseUp = function(){
document.onmousemove = null;
document.onmouseup = null;
}
var mouseDown = function(){
document.onmousemove = mouseMove;
document.onmouseup = mouseUp;
返回false;
}
div.onmousedown = mouseDown;
不要忘记从事件函数mouseMove返回false以防止默认事件;
I'm using jquery to make a few divs draggable on a website. My code is similar to this:
$("#thediv").bind('mousedown',function(){
$(document).bind('mousemove',function(ms){
//drag
})
});
The problem is that if the mouse is too fast for the div, the mouse goes outside of the div. This selects random content on the screen (highlights it blue). It does not give a smooth drag effect. Is there a nice way of fixing this? jQuery's ui drag plugin does not have this problem. I don't want to use it though because it is a large file.
Thanks
To prevent selection of elements in page just return false from your functions (onmousedown, onmousemove). Also for this functionality you do not need jquery, simply use this pattern:
var mouseMove = function () { // move div return false; } var mouseUp = function () { document.onmousemove = null; document.onmouseup = null; } var mouseDown = function () { document.onmousemove = mouseMove; document.onmouseup = mouseUp; return false; } div.onmousedown = mouseDown;
Don't forget return false from event function mouseMove to prevent default events;
这篇关于jQuery拖动div选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!