欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery實(shí)現(xiàn)div隨意拖動(dòng)的實(shí)例代碼(通用代碼)

 更新時(shí)間:2016年01月28日 09:24:01   作者:phpervip  
這篇文章主要介紹了jQuery實(shí)現(xiàn)div隨意拖動(dòng)的實(shí)例代碼,涉及到j(luò)query div隨意拖動(dòng)相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧

注意js放的位置,要放的靠近,若被其他覆蓋,則無(wú)法移動(dòng)。

比如:

<div id="move">可移動(dòng)的DIV</div>

引入jquery.js, jquery-ui.js,

<script scr="http://code.jquery.com/jquery-1.10.2.js"></script>
<script scr="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

一句:

$("#move").draggable();

如希望,點(diǎn)住時(shí)鼠標(biāo)變手形:

$("#move").mousedown(function(){
$(this).css("cursor","pointer");
}).mouseup(function(){
$(this).css("cursor","default");
});

下面給大家分享一段通用代碼jquery實(shí)現(xiàn)拖動(dòng)div的通用方法

<script type="text/javascript"><!-- 
$(document).ready(function() 
{ 
$(".show").mousedown(function(e)//e鼠標(biāo)事件 
{ 
$(this).css("cursor","move");//改變鼠標(biāo)指針的形狀 
var offset = $(this).offset();//DIV在頁(yè)面的位置 
var x = e.pageX - offset.left;//獲得鼠標(biāo)指針離DIV元素左邊界的距離 
var y = e.pageY - offset.top;//獲得鼠標(biāo)指針離DIV元素上邊界的距離 
$(document).bind("mousemove",function(ev)//綁定鼠標(biāo)的移動(dòng)事件,因?yàn)楣鈽?biāo)在DIV元素外面也要有效果,所以要用doucment的事件,而不用DIV元素的事件 
{ 
$(".show").stop();//加上這個(gè)之后 
var _x = ev.pageX - x;//獲得X軸方向移動(dòng)的值 
var _y = ev.pageY - y;//獲得Y軸方向移動(dòng)的值 
$(".show").animate({left:_x+"px",top:_y+"px"},10); 
}); 
}); 
$(document).mouseup(function() 
{ 
$(".show").css("cursor","default"); 
$(this).unbind("mousemove"); 
}) 
}) 
// --></script> 

相關(guān)文章

最新評(píng)論