jQuery拖拽通過八個(gè)點(diǎn)改變div大小
jQuery拖拽通過八個(gè)點(diǎn)改變div大小,供大家參考,具體內(nèi)容如下
js:
(function($) { /** * 默認(rèn)參數(shù) */ var defaultOpts = { stage: document, //舞臺(tái) item: 'resize-item', //可縮放的類名 }; /** * 定義類 */ var ZResize = function(options) { this.options = $.extend({}, defaultOpts, options); this.init(); } ZResize.prototype = { init: function() { this.initResizeBox(); }, /** * 初始化拖拽item */ initResizeBox: function() { var self = this; $(self.options.item).each(function () { //創(chuàng)建面板 var width = $(this).width(); var height = $(this).height(); var resizePanel = $('<div class"resize-panel"></div>'); resizePanel.css({ width: width, height: height, top: 0, left: 0, position: 'absolute', 'background-color': 'rgba(0,0,0,0.5)', cursor: 'move', display: 'none' }); self.appendHandler(resizePanel, $(this)); /** * 創(chuàng)建控制點(diǎn) */ var n = $('<div class="n"></div>');//北 var s = $('<div class="s"></div>');//南 var w = $('<div class="w"></div>');//西 var e = $('<div class="e"></div>');//東 var ne = $('<div class="ne"></div>');//東北 var nw = $('<div class="nw"></div>');//西北 var se = $('<div class="se"></div>');//東南 var sw = $('<div class="sw"></div>');//西南 //添加公共樣式 self.addHandlerCss([n, s, w, e, ne, nw, se, sw]); //添加各自樣式 n.css({ 'top': '-4px', 'margin-left': '-4px', 'left': '50%', 'cursor': 'n-resize' }); s.css({ 'bottom': '-4px', 'margin-left': '-4px', 'left': '50%', 'cursor': 's-resize' }); e.css({ 'top': '50%', 'margin-top': '-4px', 'right': '-4px', 'cursor': 'e-resize' }); w.css({ 'top': '50%', 'margin-top': '-4px', 'left': '-4px', 'cursor': 'w-resize' }); ne.css({ 'top': '-4px', 'right': '-4px', 'cursor': 'ne-resize' }); nw.css({ top: '-4px', 'left': '-4px', 'cursor': 'nw-resize' }); se.css({ 'bottom': '-4px', 'right': '-4px', 'cursor': 'se-resize' }); sw.css({ 'bottom': '-4px', 'left': '-4px', 'cursor': 'sw-resize' }); // 添加項(xiàng)目 self.appendHandler([n, s, w, e, ne, nw, se, sw], resizePanel); //綁定拖拽縮放事件 self.bindResizeEvent(resizePanel, $(this)); //綁定觸發(fā)事件 self.bindTrigger($(this)); }); self.bindHidePanel(); }, //控制點(diǎn)公共樣式 addHandlerCss: function(els) { for(var i = 0; i < els.length; i++) { el = els[i]; el.css({ position: 'absolute', width: '8px', height: '8px', background: '#ff6600', margin: '0', 'border-radius': '2px', border: '1px solid #dd5500', }); } }, /** * 插入容器 */ appendHandler: function(handlers, target) { for(var i = 0; i < handlers.length; i++) { el = handlers[i]; target.append(el); } }, /** * 顯示拖拽面板 */ triggerResize: function(el) { var self = this; el.siblings(self.options.item).children('div').css({ display: 'none' }); el.children('div').css({ display: 'block' }); }, /** * 拖拽事件控制 包含8個(gè)縮放點(diǎn) 和一個(gè)拖拽位置 */ bindResizeEvent: function(el) { var self = this; var ox = 0; //原始事件x位置 var oy = 0; //原始事件y位置 var ow = 0; //原始寬度 var oh = 0; //原始高度 var oleft = 0; //原始元素位置 var otop = 0; var org = el.parent('div'); //東 var emove = false; el.on('mousedown','.e', function(e) { ox = e.pageX;//原始x位置 ow = el.width(); emove = true; }); //南 var smove = false; el.on('mousedown','.s', function(e) { oy = e.pageY;//原始x位置 oh = el.height(); smove = true; }); //西 var wmove = false; el.on('mousedown','.w', function(e) { ox = e.pageX;//原始x位置 ow = el.width(); wmove = true; oleft = parseInt(org.css('left').replace('px', '')); }); //北 var nmove = false; el.on('mousedown','.n', function(e) { oy = e.pageY;//原始x位置 oh = el.height(); nmove = true; otop = parseInt(org.css('top').replace('px', '')); }); //東北 var nemove = false; el.on('mousedown','.ne', function(e) { ox = e.pageX;//原始x位置 oy = e.pageY; ow = el.width(); oh = el.height(); nemove = true; otop = parseInt(org.css('top').replace('px', '')); }); //西北 var nwmove = false; el.on('mousedown','.nw', function(e) { ox = e.pageX;//原始x位置 oy = e.pageY; ow = el.width(); oh = el.height(); otop = parseInt(org.css('top').replace('px', '')); oleft = parseInt(org.css('left').replace('px', '')); nwmove = true; }); //東南 var semove = false; el.on('mousedown','.se', function(e) { ox = e.pageX;//原始x位置 oy = e.pageY; ow = el.width(); oh = el.height(); semove = true; }); //西南 var swmove = false; el.on('mousedown','.sw', function(e) { ox = e.pageX;//原始x位置 oy = e.pageY; ow = el.width(); oh = el.height(); swmove = true; oleft = parseInt(org.css('left').replace('px', '')); }); //拖拽 var drag = false; el.on('mousedown', function(e) { ox = e.pageX;//原始x位置 oy = e.pageY; otop = parseInt(org.css('top').replace('px', '')); oleft = parseInt(org.css('left').replace('px', '')); drag = true; }); $(self.options.stage).on('mousemove', function(e) { if(emove) { var x = (e.pageX - ox); el.css({ width: ow + x }); org.css({ width: ow + x }); } else if(smove) { var y = (e.pageY - oy); el.css({ height: oh + y }); org.css({ height: oh + y }); } else if(wmove) { var x = (e.pageX - ox); el.css({ width: ow - x, // left: oleft + x }); org.css({ width: ow - x, left: oleft + x }); } else if(nmove) { var y = (e.pageY - oy); el.css({ height: oh - y, // top: otop + y }); org.css({ height: oh - y, top: otop + y }); } else if(nemove) { var x = e.pageX - ox; var y = e.pageY - oy; el.css({ height: oh - y, // top: otop + y, width: ow + x }); org.css({ height: oh - y, top: otop + y, width: ow + x }); } else if(nwmove) { var x = e.pageX - ox; var y = e.pageY - oy; el.css({ height: oh - y, // top: otop + y, width: ow - x, // left: oleft + x }); org.css({ height: oh - y, top: otop + y, width: ow - x, left: oleft + x }); } else if(semove) { var x = e.pageX - ox; var y = e.pageY - oy; el.css({ width: ow + x, height: oh + y }); org.css({ width: ow + x, height: oh + y }); } else if(swmove) { var x = e.pageX - ox; var y = e.pageY - oy; el.css({ width: ow - x, // left: oleft + x, height: oh + y }); org.css({ width: ow - x, left: oleft + x, height: oh + y }); } else if(drag) { var x = e.pageX - ox; var y = e.pageY - oy; org.css({ left: oleft + x, top: otop + y }); } }).on('mouseup', function(e) { emove = false; smove = false; wmove = false; nmove = false; nemove = false; nwmove = false; swmove = false; semove = false; drag = false; }); }, /** * 點(diǎn)擊item顯示拖拽面板 */ bindTrigger: function(el) { var self = this; el.on('click', function(e) { e.stopPropagation(); self.triggerResize(el); }); }, /** * 點(diǎn)擊舞臺(tái)空閑區(qū)域 隱藏縮放面板 */ bindHidePanel: function(el) { var stage = this.options.stage; var item = this.options.item; $(stage).bind('click', function() { $(item).children('div').css({ display: 'none' }); }) } } window.ZResize = ZResize; })(jQuery);
html:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>jQuery拖拽放大縮小插件idrag</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style type="text/css"> .item1 { width: 405px; height: 291px; cursor: move; position: absolute; top: 30px; left: 30px; background-color: #FFF; border: 1px solid #CCCCCC; -webkit-box-shadow: 10px 10px 25px #ccc; -moz-box-shadow: 10px 10px 25px #ccc; box-shadow: 10px 10px 25px #ccc; } .item2 { width: 200px; height: 100px; cursor: move; position: absolute; top: 400px; left: 100px; background-color: #FFF; border: 1px solid #CCCCCC; -webkit-box-shadow: 10px 10px 25px #ccc; -moz-box-shadow: 10px 10px 25px #ccc; box-shadow: 10px 10px 25px #ccc; line-height: 100px; text-align: center; } body { background-color: #F3F3F3; } </style> </head> <body> <div id="mydiv" style="width:800px; height:800px; border-style:solid"> <div id="div1" class="resize-item item1"> <img src="images/dog.png" width="100%" height="100%"> </div> <div class="resize-item item2"> 你是我的小小狗 </div> </div> <script src="jquery.min.js"></script> <script type="text/javascript" src='jquery.ZResize.js'></script> <script type="text/javascript"> new ZResize({ stage: "#mydiv", //舞臺(tái) item: '#div1', //可縮放的類名 }); </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery 移動(dòng)端拖拽(模塊化開發(fā),觸摸事件,webpack)
- jQuery實(shí)現(xiàn)div拖拽效果實(shí)例分析
- jQuery div拖拽用法實(shí)例
- jQuery使用drag效果實(shí)現(xiàn)自由拖拽div
- jQuery控制Div拖拽效果完整實(shí)例分析
- jquery實(shí)現(xiàn)拖拽調(diào)整Div大小
- jQuery拖拽div實(shí)現(xiàn)思路
- jquery實(shí)現(xiàn)div拖拽寬度示例代碼
- JQuery Dialog(JS 模態(tài)窗口,可拖拽的DIV)
- Jquery實(shí)現(xiàn)移動(dòng)端控制DIV拖拽
相關(guān)文章
jQuery使用prepend()方法在元素前添加內(nèi)容用法實(shí)例
這篇文章主要介紹了jQuery使用prepend()方法在元素前添加內(nèi)容的方法,實(shí)例分析了prepend方法追加內(nèi)容的使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03基于jquery的一行代碼輕松實(shí)現(xiàn)拖動(dòng)效果
寫JS實(shí)現(xiàn)拖動(dòng)需要一大堆不便維護(hù)的代碼,實(shí)屬麻煩,Google了大半天,發(fā)現(xiàn)了一個(gè)優(yōu)秀的Jquery插件EasyDrag,只需要一行代碼便可輕松在主流瀏覽器上。2010-12-12同域jQuery(跨)iframe操作DOM(實(shí)例講解)
本篇文章主要是對(duì)同域jQuery(跨)iframe操作DOM進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-12-12jquery無縫向上滾動(dòng)實(shí)現(xiàn)代碼
向上滾動(dòng)的效果想必大家都見過吧!無縫間歇向上滾動(dòng)應(yīng)該也不會(huì)陌生吧,接下來為大家介紹下jquery實(shí)現(xiàn)無縫間歇滾動(dòng),感興趣的朋友可以參考下哈,希望可以幫助到你們2013-03-03jQuery動(dòng)畫出現(xiàn)連續(xù)觸發(fā)、滯后反復(fù)執(zhí)行的解決方法
這篇文章主要介紹了jQuery動(dòng)畫出現(xiàn)連續(xù)觸發(fā)、滯后反復(fù)執(zhí)行的解決方法,實(shí)例分析了針對(duì)jQuery中slideUp、slideDown、animate等動(dòng)畫運(yùn)用時(shí)出現(xiàn)的滯后反復(fù)執(zhí)行等問題的解決方法,需要的朋友可以參考下2015-01-01jQuery+.net實(shí)現(xiàn)瀏覽更多內(nèi)容(改編php版本)
改編自php版本這里記錄.net 下的實(shí)現(xiàn);首先創(chuàng)建數(shù)據(jù)庫表test,并插入一些測(cè)試數(shù)據(jù)接下來建立一個(gè)html文件,感興趣的朋友可以參考下哈,希望您可以幫助到你2013-03-03jQuery實(shí)現(xiàn)table隔行換色和鼠標(biāo)經(jīng)過變色的兩種方法
這篇文章主要介紹jQuery實(shí)現(xiàn)table隔行換色和鼠標(biāo)經(jīng)過變色的兩種方法,需要的朋友可以參考下2014-06-06JQuery動(dòng)態(tài)給table添加、刪除行 改進(jìn)版
最近需要使用JQuery動(dòng)態(tài)操作table,自己整理了一下,可以添加新行,刪除選中的一行或多行,簡(jiǎn)單代碼如下2011-01-01淺談jQuery中 wrap() wrapAll() 與 wrapInner()的差異
本文結(jié)合W3School的文檔,分析了jQuery中 wrap() wrapAll() 與 wrapInner()的差異,并給出了圖文對(duì)比教程,非常的簡(jiǎn)單實(shí)用,有需要的朋友可以參考下2014-11-11