"/>
">1.初次實作
1.1 html程式碼
<html xmlns="www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>div change width by drag</title><script src="../jQuery/jquery-1.8.3.min.js?1.1.11" type="text/javascript"></script></head><body><h1>div change width by drag</h1><div id="pos" style="color:red"></div><div id="myDiv" style="border:2px solid red;width:300px;height:50px;margin-left: 100px;margin-top: 20px"></div></body></html>
1.2 js程式碼
var eleLeft = $('#myDiv').offset().left;var isMouseDown = false;var borderLen = 4; //左右边框 $('#myDiv').bind({ mousedown:function(e){var ele = $(this);var rightPos = eleLeft + ele.width() + borderLen;if(rightPos-5 <= e.pageX && e.pageX <= rightPos){ isMouseDown = true; } }, mousemove:function(e){var ele = $(this);var rightPos = eleLeft + ele.width() + borderLen; $('#pos').text("x:"+e.pageX + " eleLeft:"+eleLeft+" rightPos:"+rightPos);if(rightPos-5 <= e.pageX && e.pageX <= rightPos){ ele.css('cursor','e-resize'); }else{if(!isMouseDown){ ele.css('cursor','auto'); } }if(isMouseDown){ ele.width((e.pageX-eleLeft-borderLen)+'px'); //新鼠标位置-div距左-borderLen } }, mouseup:function(e){ isMouseDown = false; } });
1.3 結果
只能往左拖曳使div寬度變小,往右拖曳也沒用!原因往右拖曳滑鼠mousemove事件無法被div捕獲了。拖曳時也很難停下來!所以得改進。
2.再改進
$('#myDiv' ele = $( rightPos = eleLeft + ele.width() +(rightPos-5 <= e.pageX && e.pageX <== 'body' ele = $('#myDiv' rightPos = eleLeft + ele.width() +'#pos').text("x:"+e.pageX + " eleLeft:"+eleLeft+" rightPos:"+(rightPos-5 <= e.pageX && e.pageX <='cursor','e-resize'(!'cursor','auto'-eleLeft-borderLen)+'px'); =
這次解決了上述問題,可以往右拖,並且隨時可以停下來了。到這裡就完成了嗎? NO!
當我引入一個其他div,並且阻止mouseup事件冒泡情況怎麼樣?答案是,拖曳到這個其它div上放開滑鼠後無法停止下來!
<div id="otherDiv" style="border: 2px solid blue;width: 200px;height: 200px;margin-left: 400px"></div>
$('#otherDiv').mouseup(function(e){//e.preventDefault(); //阻止默认行为e.stopPropagation(); //阻止事件冒泡(导致body捕获不到mouseup事件)});
3.完美解決
拖曳停止可能受到其它元素的幹擾,怎麼解決?想到一些彈出層點擊其它其它地方隱藏的功能,讓我想到了,加一個遮罩層,讓mouseup事件總是可以響應,不就搞定了嘛!
$('#myDiv').bind({ mousedown:function(e){var ele = $(this);var rightPos = eleLeft + ele.width() + borderLen;if(rightPos-5 <= e.pageX && e.pageX <= rightPos){ isMouseDown = true;//创建遮罩层,防止mouseup事件被其它元素阻止冒泡,导致mouseup事件无法被body捕获,导致拖动不能停止var bodyWidth = $('body').width();var bodyHeight = $('body').height(); $('body').append('<div id="mask" style="opacity:0.2;top:0px;left:0px;background-color:green;position:absolute;z-index:9999;width:'+bodyWidth+'px;height:'+bodyHeight+'px;"></div>'); } } }); $('body').bind({ mousemove:function(e){var ele = $('#myDiv');var rightPos = eleLeft + ele.width() + borderLen; $('#pos').text("x:"+e.pageX + " eleLeft:"+eleLeft+" rightPos:"+rightPos);if(rightPos-5 <= e.pageX && e.pageX <= rightPos){ ele.css('cursor','e-resize'); }else{if(!isMouseDown){ ele.css('cursor','auto'); } }if(isMouseDown){ ele.width((e.pageX-eleLeft-borderLen)+'px'); } }, mouseup:function(e){ isMouseDown = false; $('#mask').remove(); } }); $('#otherDiv').mouseup(function(e){//e.preventDefault(); //阻止默认行为e.stopPropagation(); //阻止事件冒泡(导致body捕获不到mouseup事件)});
4.完整程式碼與最終效果
div change width by drag div change width by drag
<div id="otherDiv" style="border: 2px solid blue;width: 200px;height: 200px;margin-left: 400px"></div>
以上是滑鼠拖曳改變DIV的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!