JS拖拽,移动与拉伸

上次做的简单的拖拽:javascript简单拖拽练习(鼠标事件 mousedown mousemove mouseup)

这次增加了一些相关的功能,增加四个角的拉伸改变宽度,主要还是用到一些简单的坐标位置计算,没有什么技术难度,熟练了一下自己对拖拽的运用

不晓得为什么粘贴到博客园上就不支持IE6了,直接在本地是支持IE6的,有个问题就是,鼠标点击的时候光标会变为选择文字的光标,不知道应该怎么

处理这个问题呢?

DEMO如下:

按此处拖动

中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间 内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容 中间内容中间内容中间内容中间内容中间内容中间内容

在此记录一下代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>简单拖拽扩展</title><style type="text/css">*{margin:0;padding:0;}#outwarp{ margin:20px auto; width:600px; height:600px; background:#fff; border:1px solid #333; position: relative; }.controlBox{ width:200px; height:200px; position: absolute; left:25px; top:50px; background:#ccc; font-size:12px; color:#fff; border: 1px solid #333;}.controlBar{cursor: move;}.controlBar h2{ font-size:12px; text-align: center; line-height: 25px; background: blue;}.innerCon{text-align: left; line-height: 20px;}.innerCon p{ padding: 10px; color: #000;}.resize{ position: absolute; height: 10px; width:10px; color: white; z-index: 10; background: red;}.lt{left:0;right:0; cursor:nw-resize;}.tr{right:0;top: 0;cursor:ne-resize;}.rb{right:0;bottom: 0; cursor:nw-resize;}.bl{left:0;bottom:0;cursor:ne-resize;}</style>
</head>
<body>
<div id="outwarp"><div class="controlBox"><div class="resize lt"></div><div class="resize tr"></div><div class="resize rb"></div><div class="resize bl"></div><div class="controlBar"><h2>按此处拖动</h2></div><div class="innerCon"><p>中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容中间内容</p></div></div>
</div>
<script type="text/javascript">
(function () {//定义便捷函数getElementById,getElementsByTagName,getElementsByClassName,bindFunction,bindEventfunction $() {var elements = new Array();for (var i = 0; i < arguments.length; i++) {var element = arguments[i];if (typeof element == 'string') {element = document.getElementById(element);}if (!element) {continue;}if (arguments.length == 1) {return element;}elements.push(element);}return elements;}function $$(tag, parent) {parent = parent || document;return $(parent).getElementsByTagName(tag);}function $$$(str, parent, tag) {if (parent) {parent = $(parent);} else {parent = document;}tag = tag || '*';var els = parent.getElementsByTagName(tag),arr = [];for (var i = 0, n = els.length; i < n; i++) {for (var j = 0, k = els[i].className.split(' '), l = k.length; j < l; j++) {if (k[j] == str) {arr.push(els[i]);break;}}}return arr;}function bindFunction(obj, func) {return function () {return func.apply(obj, arguments);};}function bindEvent(element, type, func) {if (element.addEventListener) {element.addEventListener(type, func, false); //false 表示冒泡
        } else if (element.attachEvent) {element.attachEvent('on' + type, func);} else {element['on' + type] = func;}}/*定义拖拽类*/var DragBox = function (options) {var outWarpId = this.outWarpId = options.outWarpId;//获取最外围的包裹层IDvar outWarp = $(outWarpId);//获取外围包裹层对象var controlBox = this.controlBox = $$$('controlBox', outWarp, 'div')[0]; //被拖动的层this.controlBar = $$$('controlBar', controlBox, 'div')[0];this.resizeLt = $$$('lt', controlBox, 'div')[0];this.resizeTr = $$$('tr', controlBox, 'div')[0];this.resizeRb = $$$('rb', controlBox, 'div')[0];this.resizeBl = $$$('bl', controlBox, 'div')[0];/*获取outWarpId信息*/this.warpWidth = outWarp.offsetWidth - 2;   // 对象宽度this.warpHeight = outWarp.offsetHeight - 2;  // 对象高度this.warpLeft = outWarp.offsetLeft; //对象靠LEFT距离this.warpTop = outWarp.offsetTop;  //对象靠TOP距离/*定义拖动的对象*/this.draging = null;this.bind();};DragBox.prototype = {moveBox:function (event) {event = event || window.event;var target = event.target || event.srcElement;// 记录滚动条位置this.scrollTop = document.documentElement.scrollTop || document.body.scrollTop;this.scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;//记录光标的位置var mousex = event.clientX; // 光标LEFTvar mousey = event.clientY; //光标TOP
            console.log('mousex:' + mousex);console.log('mousey:' + mousey);// 光标相对outwarp层的坐标var posx = mousex + this.scrollLeft - this.warpLeft;var posy = mousey + this.scrollTop - this.warpTop;// 多次用到this.controlBox 赋值一个短变量名var my = this.controlBox;// 多次用到this.controlBox.style,赋值一个短变量名var myStyle = my.style;// 最小尺寸var minWidth = 200,minHeight = 200;switch(event.type){case 'mousedown':/*记录相关初始信息*/if(target.parentNode.className.indexOf('controlBar')!=-1){this.draging = this.controlBox; //赋值拖动对象
                    }if(target.className.indexOf('rb')!= -1){this.draging = this.resizeRb; // 赋值为右下角拖动
                    }if(target.className.indexOf('tr')!= -1){this.draging = this.resizeTr; // 赋值为右上角改变大小
                    }if(target.className.indexOf('lt')!= -1){this.draging = this.resizeLt; // 赋值为左上角改变大小
                    }if(target.className.indexOf('bl') != -1){this.draging = this.resizeBl;}//alert('this.scrollTop:'+this.scrollTop);//点击时记录拖动层的初始信息this.controlBoxWidth = my.offsetWidth; //拖动层的宽度this.controlBoxHeight = my.offsetHeight; //拖动层的宽度this.controlBoxLeft = my.offsetLeft; //拖动层的LEFT坐标this.controlBoxTop = my.offsetTop; //拖动层的TOP坐标// 记录鼠标按下时鼠标相对与拖动层的距离this.mx = posx - this.controlBoxLeft;this.my = posy - this.controlBoxTop;// 记录左下角的坐标,因为按住右上拖动的时候左下角不动this.lb_x = my.offsetWidth + my.offsetLeft;this.lb_y = my.offsetHeight + my.offsetTop;console.log('my.offsetHeight:'+my.offsetHeight+'my.offsetTop:'+my.offsetTop);// 记录右下角坐标,按住左上角拖动的时候右下角不动this.rb_x = my.offsetLeft + my.offsetWidth;this.rb_y = my.offsetTop + my.offsetHeight;// 记录右上角坐标,当按住左下角的时候右上角不动this.lt_x = my.offsetLeft + my.offsetWidth;this.lt_y = my.offsetTop;break;case 'mousemove':if (this.draging == this.controlBox){window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty(); //取消页面上由于鼠标按下拖动造成的选中文字和图片的选择状态//拖动层的位置var left = posx - this.mx;var top = posy - this.my;left < 0 && (left = 0);top < 0 && (top = 0);left > (this.warpWidth - this.controlBoxWidth) && (left = this.warpWidth - this.controlBoxWidth);top > (this.warpHeight - this.controlBoxHeight) && (top = this.warpHeight - this.controlBoxHeight);//改变位置
                        myStyle.left = left + 'px';myStyle.top = top + 'px';//右下角拖动
                    } else if (this.draging == this.resizeRb) {// 需要改变的宽度var changeWidth = posx - this.controlBoxWidth - this.controlBoxLeft,changeHeight = posy - this.controlBoxHeight - this.controlBoxTop,// 计算总宽度
                                allWidth = this.controlBoxWidth + changeWidth + this.controlBoxLeft ,allHeight = this.controlBoxHeight + changeHeight + this.controlBoxTop;//改变宽度
                        myStyle.width = (this.controlBoxWidth + changeWidth) + 'px';myStyle.height = (this.controlBoxHeight + changeHeight) + 'px';// 限制最大宽度if (allWidth > this.warpWidth) {myStyle.width = ( this.warpWidth - this.controlBoxLeft) + 'px';}if (allHeight > this.warpHeight) {myStyle.height = (this.warpHeight - this.controlBoxTop) + 'px';}// 限制最小宽度if ( parseInt(myStyle.width) < minWidth)myStyle.width = minWidth + 'px';if (parseInt(myStyle.height) < minHeight) myStyle.height = minHeight + 'px';// 右上角
                    } else if (this.draging == this.resizeTr) {// 需要改变的宽度,该变高度即改变TOP的坐标
                        changeWidth = posx - this.controlBoxWidth - this.controlBoxLeft;// 计算总宽度
                        allWidth = this.controlBoxWidth + changeWidth + this.controlBoxLeft;// 改变宽度
                        myStyle.width = (this.controlBoxWidth + changeWidth) + 'px';if (allWidth > this.warpWidth) myStyle.width = ( this.warpWidth - this.controlBoxLeft) + 'px';if ( parseInt(myStyle.width) < minWidth) myStyle.width = minWidth + 'px';// 改变高度
                        my.style.top = posy + 'px';my.style.height = (this.controlBoxHeight + (this.controlBoxTop - posy)) +'px';if(parseInt(my.style.height)< minHeight){my.style.height = minHeight +'px';my.style.top = (this.lb_y - minHeight) + 'px';}if(parseInt(my.style.height)>this.lb_y){my.style.height = this.lb_y + 'px';my.style.top = 0;}// 左上角
                    }else if (this.draging == this.resizeLt) {changeWidth = this.controlBoxLeft - posx;changeHeight = this.controlBoxTop - posy;my.style.left = posx + 'px';my.style.width = (this.controlBoxWidth + changeWidth) + 'px';my.style.top = posy + 'px';my.style.height = (this.controlBoxHeight + changeHeight) + 'px';// 限制宽高度最大值if(parseInt(my.style.width) > this.controlBoxLeft + this.controlBoxWidth){my.style.width = (this.controlBoxLeft + this.controlBoxWidth) + 'px';my.style.left = 0;}else if(parseInt(my.style.width)< minWidth){my.style.left = (this.rb_x - minWidth) + 'px';my.style.width = minWidth + 'px';}if(parseInt(my.style.height) > this.controlBoxHeight + this.controlBoxTop){my.style.height = (this.controlBoxHeight + this.controlBoxTop) + 'px';if(parseInt(my.style.top)<0) my.style.top = 0;}else if(parseInt(my.style.height)< minHeight){my.style.top = (this.rb_y - minHeight) + 'px';my.style.height = minHeight + 'px';}// 左下角
                    }else if(this.draging == this.resizeBl){changeWidth = this.controlBoxLeft - posx;changeHeight = posy - this.controlBoxHeight - this.controlBoxTop;my.style.left = posx + 'px';my.style.width = this.controlBoxWidth + changeWidth + 'px';my.style.height = this.controlBoxHeight + changeHeight + 'px';if(parseInt(my.style.width)< minWidth){my.style.width = minWidth + 'px';my.style.left = this.lt_x - minWidth + 'px';}else if(parseInt(my.style.width)> this.lt_x){my.style.width = this.lt_x + 'px';my.style.left = 0;}if(parseInt(my.style.height)< minHeight){my.style.height = minHeight + 'px';}else if(parseInt(my.style.height)> this.warpHeight- this.lt_y){my.style.height = this.warpHeight- this.lt_y +'px';}}break;case 'mouseup':this.draging = null;break;}},bind:function () {var that = this;bindEvent(document, 'mousedown', bindFunction(that,that.moveBox));bindEvent(document, 'mousemove', bindFunction(that,that.moveBox));bindEvent(document, 'mouseup', bindFunction(that,that.moveBox));}};var demo = new DragBox({outWarpId:'outwarp'});
})()
</script>
</body>
</html>

转载于:https://www.cnblogs.com/NNUF/archive/2012/05/17/2506123.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/260808.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

关于release后retainCount还是1的问题

转自&#xff1a;http://www.cocoachina.com/bbs/read.php?tid175523 realse之后再调用还能调用的的问题&#xff0c;我做了这么多年也是经常遇到&#xff0c;也曾经试图寻找原因&#xff0c; 就像6楼说的&#xff0c;很多时候都会出现realse过后还能调用的现象。而且对象不是…

Maven for Eclipse 第二章 ——安装 m2eclipse插件

m2eclipse 是一个提供了 Maven 与 Eclipse 整合的插件。它的意图是桥接上 Maven 和 Eclipse 之间的缺口。通过 Maven 原型提供的简单直白的接口创建项目&#xff0c;它使 Maven 在 IDE 中非常容易使用。下面是m2eclipse 提供的一些特性。 创建和导入 Maven 项目在 Eclipse 运行…

第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波9 - 直方图处理 - 直方图匹配(规定化)灰度图像,彩色图像都适用

直方图匹配&#xff08;规定化&#xff09; 连续灰度 sT(r)(L−1)∫0rpr(w)dw(3.17)s T(r) (L-1) \int_{0}^{r} p_r(w) \text{d} w \tag{3.17} sT(r)(L−1)∫0r​pr​(w)dw(3.17) 定义关于变量zzz的一个函数GGG&#xff0c;它具有如下性质&#xff1a; G(z)(L−1)∫0zpz(v)d…

C#委托之就是跟委托过不去…

在上一篇博文当中,我们例举了一个机房自动化系统的逻辑控制程序,其中用到了Lambda表达式,因此方便了我们程序功能的实现.然而,我们不能仅仅为实现功能,完成任务而奋斗,应该知其然,知其所以然,也就是说,知道了Lambda表达式能够带来这样的方便,也应该知道为什么能够带来这样的方便…

closewait一直不释放_机床为什么要释放应力?怎么释放应力才好?

在机床行业内一直有种说法&#xff0c;就是机床需要释放应力&#xff0c;而且越是高精密的机床就越要注意应力的释放&#xff0c;最近就有机床粉向小编询问应力是什么&#xff1f;为什么要释放应力&#xff1f;如果释放要释放多久&#xff1f;怎么释放应力才好等一系列关于机床…

HDU 1025 Constructing Roads In JGShining's Kingdom(DP+二分)

点我看题目 题意 &#xff1a;两条平行线上分别有两种城市的生存&#xff0c;一条线上是贫穷城市&#xff0c;他们每一座城市都刚好只缺乏一种物资&#xff0c;而另一条线上是富有城市&#xff0c;他们每一座城市刚好只富有一种物资&#xff0c;所以要从富有城市出口到贫穷城市…

表单元素选择器

无论是提交还是传递数据&#xff0c;表单元素在动态交互页面的作用是非常重要的。jQuery中专门加入了表单选择器&#xff0c;从而能够极其方便地获取到某个类型的表单元素 表单选择器的具体方法描述&#xff1a; 注意事项&#xff1a; 除了input筛选选择器&#xff0c;几乎每个…

怎样在excel表格中画斜线并打字_一日一技丨Excel斜线表头如何制作?标题、表头的4个技巧...

来源 | 迅捷PDF转换器 (ID:xjpdf6)作者丨小小迅「一日一技」是每天的知识分享专栏&#xff0c;一是分享一些PDF、Office、办公小技巧&#xff1b;二是抽取小可爱们在留言中的疑问并解决。希望对大家有所帮助&#xff01;表头的标题是Excel中的第一道大门&#xff0c;精致好看的…

Retina时代的前端视觉优化

随着New iPad的发布&#xff0c;平板也将逐渐进入Retina时代&#xff0c;在高分辨率设备里图片的显示效果通常不尽人意&#xff0c;为了达到最佳的显示效果就需要对图片进行优化&#xff0c;这里介绍一些优化方法&#xff1a; 一、用CSS替代图片 这一点在任何时候都适用&#x…

第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波10 - 直方图处理 - 局部直方图处理

这里写目录标题局部直方图处理局部直方图处理 因为像素是由基于整个图像的灰度的变换函数修改的。这种全局性方法适合于整体增强&#xff0c;但当目的是增强图像中几个小区域的细节时&#xff0c;通常就会失败。这是因为在这些小区域中&#xff0c;像素的数量对计算全局变换的…

CodeForces369C On Changing Tree

昨天的CF自己太挫了。一上来看到A题&#xff0c;就有思路&#xff0c;然后马上敲&#xff0c;但是苦于自己很久没有敲计数的题了&#xff0c;许多函数都稍微回忆了一阵子。A题的主要做法就是将每个数质因数分解&#xff0c;统计每个质因子的个数&#xff0c;对于每个质因子pi的…

ES6之const命令

一直以来以ecma为核心的js始终没有常量的概念&#xff0c;es6则弥补了这一个缺陷&#xff1b; const foofoo;foobar;//TypeError: Assignment to constant variable.上例声明了一个基本类型的常量&#xff0c;如过试图修改初始值则会报错&#xff1b;如果是引用类型的值同样适用…

C++和Rust_后端程序员一定要看的语言大比拼:Java vs. Go vs. Rust

这是Java&#xff0c;Go和Rust之间的比较。这不是基准测试&#xff0c;更多是对可执行文件大小、内存使用率、CPU使用率、运行时要求等的比较&#xff0c;当然还有一个小的基准测试&#xff0c;可以看到每秒处理的请求数量&#xff0c;我将尝试对这些数字进行有意义的解读。为了…

Hdu 2015 偶数求和

题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid2040。水题。CODE&#xff1a;1 #include <stdio.h>2 #include <stdlib.h>3 #include <string.h>4 #include <math.h>5 using namespace std;6 7 const int maxn 102;8 9 int save[ma…

第3章 Python 数字图像处理(DIP) - 灰度变换与空间滤波11 - 直方图处理 - 使用直方图统计量增强图像

使用直方图统计量增强图像 全局均值和方差 μn∑i0L−1(ri−m)np(ri)(3.24)\mu_{n} \sum_{i0}^{L-1} (r_{i} - m)^{n} p(r_{i}) \tag{3.24}μn​i0∑L−1​(ri​−m)np(ri​)(3.24) m∑i0L−1rip(ri)(3.25)m \sum_{i0}^{L-1} r_{i} p(r_{i}) \tag{3.25}mi0∑L−1​ri​p(ri​…

数据结构 --- 堆

to be continued转载于:https://www.cnblogs.com/zhongzhiqiang/p/5808564.html

HDU - 1723 - Distribute Message

先上题目&#xff1a; Distribute Message Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1186 Accepted Submission(s): 547 Problem DescriptionThe contest’s message distribution is a big thing in pre…

nodejs 图片处理模块 rotate_学会Pillow再也不用PS啦——Python图像处理库Pillow入门!...

你在用什么软件进行图像处理呢&#xff1f;厌倦了鼠标和手指的拖拖点点&#xff0c;想不想用程序和代码进行图像的高效处理&#xff0c;Python作为简单高效又很强大的一门编程语言&#xff0c;对于图像的处理自然也是轻松拿下&#xff0c;听起来是不是很酷很极客&#xff0c;那…

创建一个追踪摄像机(2)

为了生成曲线&#xff0c;函数需要通过4个在沿着重量值在0和1之间的路径上连贯的位置。由于重量在这些2个值之间增加&#xff0c;曲线返回在更远的路径上的坐标。 当所提供的重量值为0&#xff0c;曲线将返回正确的坐标在第二个输入坐标。当所提供的重量值为1&#xff0c;曲线将…

Xcodebuild自动打包

#! /bin/bash #firtoken 29b441056e1e17c984cb32fadadsdddd shell_dirdirname $0 TARGET_NAME"SmartLock" DIR_PATH/Users/用户名/Desktop/SmartLock SIGN"iPhone Distribution:******" PROFILE"66d127d6-7963-4c20-ac8b-47e4f0fe8742" TEMP_DIR…