JS实现拖动div改变大小
来源:互联网 发布:内江市淘宝贝幼儿园 编辑:程序博客网 时间:2024/05/09 08:32
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <title></title> <style type="text/css"> .upBtn,.downBtn,.upLeftBtn,.upRightBtn,.downLeftBtn,.downRightBtn,.centerLeftBtn,.centerRightBtn { position:absolute; background:#CC0000; width:9px; height:9px; z-index:5; } .upBtn { top:-4px; left:50%; cursor:n-resize; } .downBtn { bottom:-4px; left:50%; cursor:s-resize; } .upLeftBtn { top:-4px; left:-4px; cursor:nw-resize; } .upRightBtn { top:-4px; right:-4px; cursor:ne-resize; } .downLeftBtn { bottom:-4px; left:-4px; cursor:sw-resize; } .downRightBtn { bottom:-4px; right:-4px; cursor:se-resize; } .centerLeftBtn { top:50%; left:-4px; cursor:w-resize; } .centerRightBtn { top:50%; right:-4px; cursor:e-resize; } </style> <script type="text/javascript" > function $(id) { return document.getElementById(id); } function getEvent(e) { return e || window.event; } function getLocation(e) { return { x: e.x || e.clientX, y: e.y || e.clientY } } var obj = null; // 当前操作的对象 var preview = null; // 要处理的对象 var clickX = 0; // 保留上次的X轴位置 var clickY = 0; // 保留上次的Y轴位置 // 鼠标点击 var onDragDown = function (e, type) { e = getEvent(e); var location = getLocation(e); clickY = location.y; clickX = location.x; obj = this; obj.operateType = type; preview = $("preview"); return false; }; var onUpBtnDown = function (e) { onDragDown(e, "n"); }; var onDownBtnDown = function (e) { onDragDown(e, "s"); }; var onCenterLeftBtnDown = function (e) { onDragDown(e, "w"); }; var onCenterRightBtnDown = function (e) { onDragDown(e, "e"); }; var onUpLeftBtnDown = function (e) { onDragDown(e, "nw"); }; var onUpRightBtnDown = function (e) { onDragDown(e, "ne"); }; var onDownLeftBtnDown = function (e) { onDragDown(e, "sw"); }; var onDownRightBtnDown = function (e) { onDragDown(e, "se"); }; var onDragUp = function () { document.body.style.cursor = "auto"; obj = null; }; var move = function (operateType, location, preview) { document.body.style.cursor = location + "_resize"; switch (operateType) { case "e": var add_length = clickX - location.x; clickX = location.x; var length = parseInt(preview.style.width) - add_length; preview.style.width = length + "px"; break; case "s": var add_length = clickY - location.y; clickY = location.y; var length = parseInt(preview.style.height) - add_length; preview.style.height = length + "px"; break; case "w": var add_length = clickX - location.x; clickX = location.x; var length = parseInt(preview.style.width) + add_length; preview.style.width = length + "px"; preview.style.left = clickX + "px"; break; case "n": var add_length = clickY - location.y; clickY = location.y; var length = parseInt(preview.style.height) + add_length; preview.style.height = length + "px"; preview.style.top = clickY + "px"; break; } }; var onDragMove = function (e) { if (obj) { e = getEvent(e); var location = getLocation(e); switch (obj.operateType) { case "n": move("n", location, preview); break; case "s": move("s", location, preview); break; case "w": move("w", location, preview); break; case "e": move("e", location, preview); break; case "nw": move("n", location, preview); move("w", location, preview); break; case "ne": move("n", location, preview); move("e", location, preview); break; case "sw": move("s", location, preview); move("w", location, preview); break; case "se": move("s", location, preview); move("e", location, preview); break; } } return false; }; window.onload = function () { $("upBtn").onmousedown = onUpBtnDown; $("downBtn").onmousedown = onDownBtnDown; $("centerLeftBtn").onmousedown = onCenterLeftBtnDown; $("centerRightBtn").onmousedown = onCenterRightBtnDown; $("upLeftBtn").onmousedown = onUpLeftBtnDown; $("upRightBtn").onmousedown = onUpRightBtnDown; $("downLeftBtn").onmousedown = onDownLeftBtnDown; $("downRightBtn").onmousedown = onDownRightBtnDown; document.onmousemove = onDragMove; document.onmouseup = onDragUp; } </script></head><body><div id="preview" style="width:200px; height:200px; border:1px solid gray; position:absolute; left:200px; top:100px;" ><div class="upBtn" id="upBtn"></div><div class="downBtn" id="downBtn"></div><div class="upLeftBtn" id="upLeftBtn"></div><div class="upRightBtn" id="upRightBtn"></div><div class="downLeftBtn" id="downLeftBtn"></div><div class="downRightBtn" id="downRightBtn"></div><div class="centerLeftBtn" id="centerLeftBtn"></div><div class="centerRightBtn" id="centerRightBtn"></div></div></body></html>
0 0
- JS实现拖动div改变大小
- Js - 可拖动可改变大小div的实现代码
- 拖动改变Div大小
- js 拖动div 并改变div大小 的搜索网址
- js通过八个点 拖动改变div大小的实现方法
- js通过八个点 拖动改变div大小的实现方法
- 鼠标拖动改变DIV大小
- js实现div动态改变大小
- js实现拖动改变层的大小(宽度)
- 可拖动改变大小的div
- 拖动改变上下两个div大小
- 边框处拖动鼠标改变div大小
- jquery 鼠标拖动改变div大小
- 鼠标拖动改变div容器的大小
- JS实现DIV拖动
- js实现div拖动
- js拖动改变div的宽高
- JS+DIV 实现拖动效果
- cclablebmf/cclablettf游戏字体2
- 通过修改DB给cloudstack添加模板
- Https原理
- 意向锁
- Liunx下获取当前程序路径的方法
- JS实现拖动div改变大小
- Linux管理:将Redhat安装ISO设置为yum源
- 腾讯旗下支付公司财付通更名
- Myeclipse从SVN上加载项目时出现jar包混乱和项目里面的文件没错项目名上面有红叉解决方法
- declare-styleable中format详解
- 搜索引擎lucene之文本文件索引
- 用js判断页面刷新或关闭的方法(onbeforeunload与onunload事件) 详细出处参考:http://www.jb51.net/article/30640.htm
- 豆瓣客户端的实现02
- 【综述】(中科院)樊彬老师-“局部图像特征描述概述”