鼠标拖动改变DIV等网页元素的大小的最佳实践
生活随笔
收集整理的這篇文章主要介紹了
鼠标拖动改变DIV等网页元素的大小的最佳实践
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.初次實(shí)現(xiàn)
1.1 html代碼
<html xmlns="http://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" 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'); //新鼠標(biāo)位置-div距左-borderLen }},mouseup:function(e){isMouseDown = false;}});1.3 結(jié)果
只能往左拖動(dòng)使div寬度變小,往右拖動(dòng)沒有用!原因往右拖動(dòng)鼠標(biāo)mousemove事件無法被div捕獲了。拖動(dòng)時(shí)也很難停下來!所以得改進(jìn)。
?
2.再次改進(jìn)
$('#myDiv').bind({mousedown:function(e){var ele = $(this);var rightPos = eleLeft ele.width() borderLen;if(rightPos-5 <= e.pageX && e.pageX <= rightPos){isMouseDown = true;}}});$('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;}});?這次解決了上述問題,可以往右拖,并且隨時(shí)可以停下來了。到這里就完成了嗎?NO!
當(dāng)我引入一個(gè)其他div,并且阻止mouseup事件冒泡情況怎么樣呢?答案是,拖動(dòng)到這個(gè)其它div上放開鼠標(biāo)后無法停止下來!
<div id="otherDiv" style="border: 2px solid blue;width: 200px;height: 200px;margin-left: 400px"></div>?
$('#otherDiv').mouseup(function(e){//e.preventDefault(); //阻止默認(rèn)行為e.stopPropagation(); //阻止事件冒泡(導(dǎo)致body捕獲不到mouseup事件)});?
3.完美解決
拖動(dòng)停止可能受到其它元素的干擾,怎么解決?想到一些彈出層點(diǎn)擊其它其它地方隱藏的功能,讓我想到了,加一個(gè)遮罩層,讓mouseup事件總是可以響應(yīng),不就搞定了嘛!
$('#myDiv').bind({mousedown:function(e){var ele = $(this);var rightPos = eleLeft ele.width() borderLen;if(rightPos-5 <= e.pageX && e.pageX <= rightPos){isMouseDown = true;//創(chuàng)建遮罩層,防止mouseup事件被其它元素阻止冒泡,導(dǎo)致mouseup事件無法被body捕獲,導(dǎo)致拖動(dòng)不能停止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(); //阻止默認(rèn)行為e.stopPropagation(); //阻止事件冒泡(導(dǎo)致body捕獲不到mouseup事件)});?
4.完整代碼和最終效果
<html xmlns="http://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" 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><div id="otherDiv" style="border: 2px solid blue;width: 200px;height: 200px;margin-left: 400px"></div></body><script type="text/javascript">$(document).ready(function(){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;//創(chuàng)建遮罩層,防止mouseup事件被其它元素阻止冒泡,導(dǎo)致mouseup事件無法被body捕獲,導(dǎo)致拖動(dòng)不能停止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(); //阻止默認(rèn)行為 e.stopPropagation(); //阻止事件冒泡(導(dǎo)致body捕獲不到mouseup事件) });});</script> </html>?
更多專業(yè)前端知識(shí),請上 【猿2048】www.mk2048.com
總結(jié)
以上是生活随笔為你收集整理的鼠标拖动改变DIV等网页元素的大小的最佳实践的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring data jpa 分页查询
- 下一篇: 四、spring boot 1.5.4