jQuery實(shí)現(xiàn)拖拽功能 模塊拖拽
來源:易賢網(wǎng) 閱讀:2684 次 日期:2015-04-17 13:41:49
溫馨提示:易賢網(wǎng)小編為您整理了“jQuery實(shí)現(xiàn)拖拽功能 模塊拖拽”,方便廣大網(wǎng)友查閱!

jQuery實(shí)現(xiàn)拖拽功能 模塊拖拽,基于jQuery的拖拽插件,能夠方便的在Web上實(shí)現(xiàn)拖拽功能,同時(shí)支持拖拽動(dòng)作結(jié)束之后的的回調(diào)方法自己定義。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script type="text/javascript" src="js/jquery-1.8.1.min.js"></script>

<script type="text/javascript" src="js/jquery.jsmartdrag.js"></script>

<link href="css/jsmartdrag.css" rel="stylesheet" type="text/css" />

<title>jsmartdrag實(shí)例頁面</title>

<script type="text/javascript">

$(document).ready(function(){

js = $(".source").jsmartdrag({

target:'.target',

afterDrag:afterDrag

});

});

function afterDrag(selected,currentObj,targetSelected){

if(selected){

alert("將元素ID為"+currentObj.attr("id")+"移動(dòng)到了元素ID為"+targetSelected.attr("id")+"上");

}else{

alert("沒有拖動(dòng)到指定的區(qū)域里哦!");

}

}

</script>

</head>

<body>

<div id="target1" class="target">target1</div>

<div id="target2" class="target">target2</div>

<div id="target3" class="target">target3</div>

<div id="source1" class="source" >source1</div>

<div id="source2" class="source" >source2</div>

<div id="source3" class="source" >source3</div>

</body>

</html>

jquery.jsmartdrag.js

(function($){

var move = false;

var cloneDiv = null;

var targetObj = null;

var currentObj = null;

var targetSelected = null;

var selected = false;

var scrollTop = 0;

$.fn.jsmartdrag = function(options){

var jQueryObj = this;

var defaults = {

sourceClass:"jsmartdrag-source",

sourceHoverClass:"jsmartdrag-source-hover",

cursorHoverClass:"jsmartdrag-cursor-hover",

targetHoverClass:"jsmartdrag-target-hover",

canSelect:false,

target:".target",

onDrag: function(){},

afterDrag: function(selected,currentObj,targetObj){}

};

options = $.extend(defaults, options);

this.each(function(){

$("body").css("-moz-user-select","none");

if(!defaults.canSeletct){

document.ondragstart = function () { return false; }; //禁止瀏覽器的拖拽行為

document.onselectstart = function () {return false; };//禁止瀏覽器的選中行為

}

if(options.target!=null){

targetObj = $(options.target);

}

$(this).addClass(options.sourceClass);

$(this).mousedown(function(){

currentObj = $(this);

move = true;

$(this).addClass(options.sourceHoverClass);

cloneDiv = $(this).clone();

cloneDiv.attr("id","cloneDiv");

cloneDiv.addClass(options.sourceHoverClass);

scrollTop = $("html,body").scrollTop();//當(dāng)鼠標(biāo)點(diǎn)擊的時(shí)候才計(jì)算滾動(dòng)的高度,是為了防止頁面瀏覽的時(shí)候用戶改變了瀏覽器高度

});

$(document).mousemove(function(event){

if(move){

if($("#cloneDiv").length<=0){

$("body").append(cloneDiv);

}

var dragPos = {x1:0,x2:0,y1:0,y2:0};

var pageX = 0;

var pageY = 0;

if($.browser.msie){

pageX = event.clientX;

pageY = event.clientY+scrollTop;

}else{

pageX = event.pageX;

pageY = event.pageY;

}

dragPos.x1 =pageX-cloneDiv.innerWidth()/2;

dragPos.y1 = pageY-cloneDiv.innerHeight()/2;

cloneDiv.css({left:dragPos.x1+"px",top:dragPos.y1+"px",position:'absolute'});

if(targetObj.length>0){

targetObj.each(function(){

if(checkAreaOverride(cloneDiv,$(this))){

$(this).addClass(options.targetHoverClass);

}else{

$(this).removeClass(options.targetHoverClass);

}

});

}

options.onDrag();

}

});

$(document).mouseup(function(){

if(move){

move = false;

if(cloneDiv!=null && targetObj!=null){

if($($("[class$='jsmartdrag-target-hover']")[0]).length>0){

targetSelected = $($("[class$='jsmartdrag-target-hover']")[0]);

selected = true;

}

options.afterDrag(selected,currentObj,targetSelected);

//恢復(fù)初始狀態(tài)

cloneDiv.remove();

cloneDiv.removeClass(options.cursorHoverClass);

$("[class$='jsmartdrag-target-hover']").each(function(){

$(this).removeClass(options.targetHoverClass);

});

currentObj.removeClass(options.sourceHoverClass);

currentObj = null;

if(selected == true){

targetSelected.removeClass(options.targetHoverClass);

targetSelected = null;

selected = false;

}

}

}

});

});

function checkAreaOverride (_cloneDiv,_targetObj){

//這里來判斷是否在里面哦!

var source_left = _cloneDiv.offset().left;

var source_top = _cloneDiv.offset().top;

var source_right = _cloneDiv.offset().left+cloneDiv.innerWidth();

var source_bottom = _cloneDiv.offset().top+cloneDiv.innerHeight();

var target_left = _targetObj.offset().left;

var target_top = _targetObj.offset().top;

var target_right = _targetObj.offset().left+targetObj.innerWidth();

var target_bottom = _targetObj.offset().top+targetObj.innerHeight();

var new_area_left = max(source_left,target_left);

var new_area_top = max(source_top,target_top);

var new_area_right = min(source_right,source_right);

var new_area_bottom = min(source_bottom,target_bottom);

if(new_area_left<target_right&&new_area_top<target_bottom&&new_area_right>target_left&&new_area_bottom>target_top){

$("[class$='jsmartdrag-target-hover']").each(function(){

$(this).removeClass("jsmartdrag-target-hover");

});

return true;

}else{

return false;

}

}

function max(_p1,_p2){

if(_p1>_p2){

return _p1;

}else{

return _p2;

}

}

function min(_p1,_p2){

if(_p1>_p2){

return _p2;

}else{

return _p1;

}

}

return jQueryObj;

};

})(jQuery);

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看腳本欄目
易賢網(wǎng)手機(jī)網(wǎng)站地址:jQuery實(shí)現(xiàn)拖拽功能 模塊拖拽
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)