javascript实现鼠标拖动改变层大小的方法


Posted in Javascript onApril 30, 2015

本文实例讲述了javascript实现鼠标拖动改变层大小的方法。分享给大家供大家参考。具体实现方法如下:

<html>
<head>
<title>拖动改变层的大小</title>
<meta content="text/html; charset=gb2312" http-equiv="Content-Type">
<style> {
box-sizing: border-box; moz-box-sizing: border-box
}
#testDiv { background-color: buttonface; background-repeat: repeat; 
background-attachment: scroll; color: #3969A5; height: 300px; 
left: 30px; overflow: hidden; width: 500; z-index: 2; 
border: 2px outset white; margin: 0px; padding: 2px; 
background-position: 0% 50% }
body { font-family: Verdana; font-size: 9pt }
#innerNice { background-color: white; background-repeat: repeat;
background-attachment: 
scroll; color: #3969A5; height: 100%; overflow: auto; 
width: 100%; border: 2px inset white; padding: 8px; 
background-position: 0% 50% }
</style>
<SCRIPT language=javascript>
var theobject = null; 
function resizeObject() {
this.el = null; //pointer to the object
this.dir = ""; //type of current resize (n,s,e,w,ne,nw,se,sw)
this.grabx = null; //Some useful values
this.graby = null;
this.width = null;
this.height = null;
this.left = null;
this.top = null;
}
function getDirection(el) {
var xPos, yPos, offset, dir;
dir = "";
xPos = window.event.offsetX;
yPos = window.event.offsetY;
offset = 8; //The distance from the edge in pixels
if (yPos<offset) dir += "n";
else if (yPos > el.offsetHeight-offset) dir += "s";
if (xPos<offset) dir += "w";
else if (xPos > el.offsetWidth-offset) dir += "e";
return dir;
}
function doDown() {
var el = getReal(event.srcElement, "className", "resizeMe");
if (el == null) {
theobject = null;
return;
} 
dir = getDirection(el);
if (dir == "") return;
theobject = new resizeObject();
theobject.el = el;
theobject.dir = dir;
theobject.grabx = window.event.clientX;
theobject.graby = window.event.clientY;
theobject.width = el.offsetWidth;
theobject.height = el.offsetHeight;
theobject.left = el.offsetLeft;
theobject.top = el.offsetTop;
window.event.returnValue = false;
window.event.cancelBubble = true;
}
function doUp() {
if (theobject != null) {
theobject = null;
}
}
function doMove() {
var el, xPos, yPos, str, xMin, yMin;
xMin = 8; //The smallest width possible
yMin = 8; // height
el = getReal(event.srcElement, "className", "resizeMe");
if (el.className == "resizeMe") {
str = getDirection(el);
//Fix the cursor
if (str == "") str = "default";
else str += "-resize";
el.style.cursor = str;
}
//Dragging starts here
if(theobject != null) {
if (dir.indexOf("e") != -1)
theobject.el.style.width = Math.max(xMin, theobject.width + window.event.clientX - theobject.grabx) + "px";
if (dir.indexOf("s") != -1)
theobject.el.style.height = Math.max(yMin, theobject.height + window.event.clientY - theobject.graby) + "px";
if (dir.indexOf("w") != -1) {
theobject.el.style.left = Math.min(theobject.left + window.event.clientX - theobject.grabx, theobject.left + theobject.width - xMin) + "px";
theobject.el.style.width = Math.max(xMin, theobject.width - window.event.clientX + theobject.grabx) + "px";
}
if (dir.indexOf("n") != -1) {
theobject.el.style.top = Math.min(theobject.top + window.event.clientY - theobject.graby, theobject.top + theobject.height - yMin) + "px";
theobject.el.style.height = Math.max(yMin, theobject.height - window.event.clientY + theobject.graby) + "px";
}
window.event.returnValue = false;
window.event.cancelBubble = true;
} 
}
function getReal(el, type, value) {
temp = el;
while ((temp != null) && (temp.tagName != "BODY")) {
if (eval("temp." + type) == value) {
el = temp;
return el;
}
temp = temp.parentElement;
}
return el;
}
document.onmousedown = doDown;
document.onmouseup = doUp;
document.onmousemove = doMove;
</SCRIPT>
</head>
<body>
<div class="resizeMe" id="testDiv">
<div id="innerNice">
<p align="center"> </p>
<p align="center">
请在边框处拖动鼠标</p>
<p> </p>
<p> </p>
<p> </p>
</div>
</div>
</body>
</html>

希望本文所述对大家的javascript程序设计有所帮助。

Javascript 相关文章推荐
Jquery UI震动效果实现原理及步骤
Feb 04 Javascript
如何在一个页面显示多个百度地图
Apr 07 Javascript
iframe子父页面调用js函数示例
Nov 07 Javascript
浅析Node.js查找字符串功能
Sep 03 Javascript
JS实现兼容性较好的随屏滚动效果
Nov 09 Javascript
jquery使用on绑定a标签无效 只能用live解决
Jun 02 Javascript
相册展示PhotoSwipe.js插件实现
Aug 25 Javascript
JQuery遍历元素的父辈和祖先的方法
Sep 18 Javascript
详解Angular2响应式表单
Jun 14 Javascript
Vue CLI3 如何支持less的方法示例
Aug 29 Javascript
小白教程|一小时上手最流行的前端框架vue(推荐)
Apr 10 Javascript
React学习之受控组件与数据共享实例分析
Jan 06 Javascript
jQuery实现伸展与合拢panel的方法
Apr 30 #Javascript
jQuery实现鼠标悬停显示提示信息窗口的方法
Apr 30 #Javascript
jQuery层动画定位滑动效果的方法
Apr 30 #Javascript
jquery中$each()方法的使用指南
Apr 30 #Javascript
jQuery实现手机号码输入提示功能实例
Apr 30 #Javascript
javascript实现checkbox全选的代码
Apr 30 #Javascript
javascript实现的固定位置悬浮窗口实例
Apr 30 #Javascript
You might like
PHP 和 MySQL 基础教程(一)
2006/10/09 PHP
PHP开发不能违背的安全规则 过滤用户输入
2011/05/01 PHP
解析PHP中的正则表达式以及模式匹配
2013/06/19 PHP
微信公众平台开发实现2048游戏的方法
2015/04/15 PHP
删除PHP数组中头部、尾部、任意元素的实现代码
2017/04/10 PHP
JavaScript 快捷键设置实现代码
2009/03/13 Javascript
图片连续滚动代码[兼容IE/firefox]
2009/06/11 Javascript
nodejs获取本机内网和外网ip地址的实现代码
2014/06/01 NodeJs
Node.js开源应用框架HapiJS介绍
2015/01/14 Javascript
JS运动基础框架实例分析
2015/03/03 Javascript
js判断空对象的实例(超简单)
2016/07/26 Javascript
微信小程序 传值取值的几种方法总结
2017/01/16 Javascript
详解Angular 4.x 动态创建组件
2017/04/25 Javascript
AngularJS全局警告框实现方法示例
2017/05/18 Javascript
js 将canvas生成图片保存,或直接保存一张图片的实现方法
2018/01/02 Javascript
JS实现选项卡效果的代码实例
2019/05/20 Javascript
nodejs实现用户登录路由功能
2019/05/22 NodeJs
Vue实现商品详情页的评价列表功能
2019/09/04 Javascript
如何配置vue.config.js 处理static文件夹下的静态文件
2020/06/19 Javascript
如何构建一个Vue插件并生成npm包
2020/10/26 Javascript
[51:14]LGD vs VP 2018国际邀请赛淘汰赛BO3 第一场 8.21
2018/08/22 DOTA
python类参数self使用示例
2014/02/17 Python
python实现12306火车票查询器
2017/04/20 Python
基于Django URL传参 FORM表单传数据 get post的用法实例
2018/05/28 Python
使用Python项目生成所有依赖包的清单方式
2020/07/13 Python
Python Tricks 使用 pywinrm 远程控制 Windows 主机的方法
2020/07/21 Python
python归并排序算法过程实例讲解
2020/11/04 Python
windows+vscode安装paddleOCR运行环境的步骤
2020/11/11 Python
关于h5中的fetch方法解读(小结)
2017/11/15 HTML / CSS
创建索引时需要注意的事项
2013/05/13 面试题
通用自荐信范文
2014/03/14 职场文书
《翻越远方的大山》教学反思
2014/04/13 职场文书
保密工作承诺书
2014/08/29 职场文书
医药公司采购员岗位职责
2015/04/03 职场文书
幼儿园六一儿童节开幕词
2016/03/04 职场文书
使用nginx配置访问wgcloud的方法
2021/06/26 Servers