jQuery實現(xiàn)鼠標(biāo)拖動div改變位置、大小的實踐
實現(xiàn)類似windows窗體的效果,在中間拖動改變div位置,在邊緣拖動改變div大小,鼠標(biāo)在相應(yīng)的位置改變成相應(yīng)的形狀
效果如圖: (截圖沒顯示鼠標(biāo))
代碼如下:
$(".test1").mousemove(function(e){ $(".test1").unbind("mousedown"); $(".test1").css("cursor","default"); //$("span > b").text(parseInt($("div").width())); var left = $(".test1").offset().left; var top = $(".test1").offset().top; // 如果鼠標(biāo)在中間 if(e.clientX - left > 10 && e.clientX-left < parseInt($(".test1").width()) - 10 && e.clientY - top > 10 && e.clientY-top < parseInt($(".test1").height()) - 10) { $(".test1").css("cursor","move"); $(".test1").mousedown(function(e) { var ismove = true; var x = e.pageX - $(".test1").offset().left; var y = e.pageY - $(".test1").offset().top; $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"left":e.pageX - x, "top":e.pageY - y}); } }).mouseup(function() { ismove = false; }); }); } //如果鼠標(biāo)在左上角 if(e.clientX - left < 10 && e.clientY - top < 10) { $(".test1").css("cursor","nw-resize"); $(".test1").mousedown(function(e) { var ismove = true; var y = e.pageY - $(".test1").offset().top; var h = e.pageY + parseInt($(".test1").css("height")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"height":h - e.pageY, "top":e.pageY - y}); } }).mouseup(function() { ismove = false; }); }); $(".test1").mousedown(function(e) { var ismove = true; var x = e.pageX - $(".test1").offset().left; var w = e.pageX + parseInt($(".test1").css("width")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"width":w - e.pageX, "left":e.pageX - x}); } }).mouseup(function() { ismove = false; }); }); } //如果鼠標(biāo)在上 if(e.clientY - top < 10 && e.clientX - left > 10 && e.clientX-left < parseInt($(".test1").width()) - 10) { $(".test1").css("cursor","n-resize"); $(".test1").mousedown(function(e) { var ismove = true; var y = e.pageY - $(".test1").offset().top; var h = e.pageY + parseInt($(".test1").css("height")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"height":h - e.pageY, "top":e.pageY - y}); } }).mouseup(function() { ismove = false; }); }); } //如果鼠標(biāo)在右上角 if(e.clientY - top < 10 && e.clientX-left > parseInt($(".test1").width()) - 10) { $(".test1").css("cursor","ne-resize"); $(".test1").mousedown(function(e) { var ismove = true; var y = e.pageY - $(".test1").offset().top; var h = e.pageY + parseInt($(".test1").css("height")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"height":h - e.pageY, "top":e.pageY - y}); } }).mouseup(function() { ismove = false; }); }); $(".test1").mousedown(function(e) { var ismove = true; var x = e.pageX - $(".test1").offset().left; var w = e.pageX - parseInt($(".test1").css("width")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"width":e.pageX - w}); } }).mouseup(function() { ismove = false; }); }); } //如果鼠標(biāo)在右 if(e.clientX-left > parseInt($(".test1").width()) - 10 && e.clientY - top > 10 && e.clientY-top < parseInt($(".test1").height()) - 10) { $(".test1").css("cursor","e-resize"); $(".test1").mousedown(function(e) { var ismove = true; var x = e.pageX - $(".test1").offset().left; var w = e.pageX - parseInt($(".test1").css("width")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"width":e.pageX - w}); } }).mouseup(function() { ismove = false; }); }); } //如果鼠標(biāo)在右下 if(e.clientX-left > parseInt($(".test1").width()) - 10 && e.clientY-top > parseInt($(".test1").height()) - 10) { $(".test1").css("cursor","se-resize"); $(".test1").mousedown(function(e) { var ismove = true; var x = e.pageX - $(".test1").offset().left; var w = e.pageX - parseInt($(".test1").css("width")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"width":e.pageX - w}); } }).mouseup(function() { ismove = false; }); }); $(".test1").mousedown(function(e) { var ismove = true; var y = e.pageY - $(".test1").offset().top; var h = e.pageY - parseInt($(".test1").css("height")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"height":e.pageY - h}); } }).mouseup(function() { ismove = false; }); }); } //如果鼠標(biāo)在下 if(e.clientY-top > parseInt($(".test1").height()) - 10 && e.clientX - left > 10 && e.clientX-left < parseInt($(".test1").width()) - 10) { $(".test1").css("cursor","s-resize"); $(".test1").mousedown(function(e) { var ismove = true; var y = e.pageY - $(".test1").offset().top; var h = e.pageY - parseInt($(".test1").css("height")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"height":e.pageY - h}); } }).mouseup(function() { ismove = false; }); }); } //如果鼠標(biāo)在左下 if(e.clientY-top > parseInt($(".test1").height()) - 10 && e.clientX - left < 10) { $(".test1").css("cursor","sw-resize"); $(".test1").mousedown(function(e) { var ismove = true; var x = e.pageX - $(".test1").offset().left; var w = e.pageX + parseInt($(".test1").css("width")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"width":w - e.pageX, "left":e.pageX - x}); } }).mouseup(function() { ismove = false; }); }); $(".test1").mousedown(function(e) { var ismove = true; var y = e.pageY - $(".test1").offset().top; var h = e.pageY - parseInt($(".test1").css("height")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"height":e.pageY - h}); } }).mouseup(function() { ismove = false; }); }); } //如果鼠標(biāo)在左 if(e.clientX - left < 10 && e.clientY - top > 10 && e.clientY-top < parseInt($(".test1").height()) - 10) { $(".test1").css("cursor","w-resize"); $(".test1").mousedown(function(e) { var ismove = true; var x = e.pageX - $(".test1").offset().left; var w = e.pageX + parseInt($(".test1").css("width")); $(document).mousemove(function(e) { if(ismove) { $(".test1").css({"width":w - e.pageX, "left":e.pageX - x}); } }).mouseup(function() { ismove = false; }); }); } });
到此這篇關(guān)于jQuery實現(xiàn)鼠標(biāo)拖動div改變位置、大小的實踐的文章就介紹到這了,更多相關(guān)jQuery 鼠標(biāo)拖動div內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何解決jQuery EasyUI 已打開Tab重新加載問題
最近在項目中遇到這樣的需求,要求實現(xiàn)點擊左側(cè)已經(jīng)打開的tab可以刷新重新加載datagrid。下面給大家分享實現(xiàn)代碼,一起看看吧2016-12-12jQuery 實現(xiàn)ajax傳入?yún)?shù)含有特殊字符的方法總結(jié)
在做ajax登錄時候遇到的一個問題,當(dāng)傳入?yún)?shù)含有特殊字符,比如:“$'#@”等。參數(shù)傳遞會有問題,無法正確獲取。本文章向碼農(nóng)介紹jQuery ajax特殊字符參數(shù)解決方法,需要的朋友可以參考一下。2016-10-10jquery mobile界面數(shù)據(jù)刷新的實現(xiàn)方法
下面小編就為大家?guī)硪黄猨query mobile界面數(shù)據(jù)刷新的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-05-05jQuery 頂部導(dǎo)航跟隨滾動條滾動固定浮動在頂部
這篇文章主要介紹了通過jQuery實現(xiàn)的頂部導(dǎo)航跟隨滾動條滾動固定浮動在頂部,需要的朋友可以參考下2014-06-06