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 text()要注意啦
Oct 30 Javascript
JQuyer $.post 与 $.ajax 访问WCF ajax service 时的问题需要注意的地方
Sep 20 Javascript
Jquery焦点与失去焦点示例应用
Jun 10 Javascript
picLazyLoad 实现图片延时加载(包含背景图片)
Jul 21 Javascript
微信小程序 弹幕功能简单实例
Feb 14 Javascript
webpack 代码分离优化快速指北
May 18 Javascript
websocket4.0+typescript 实现热更新的方法
Aug 14 Javascript
vue组件命名和props命名代码详解
Sep 01 Javascript
如何在微信小程序中存setStorage
Dec 13 Javascript
elementui更改el-dialog关闭按钮的图标d的示例代码
Aug 04 Javascript
浅谈JS的原型和原型链
Jun 04 Javascript
idea编译器vue缩进报错问题场景分析
Jul 04 Vue.js
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的curl实现get和post的代码
2008/08/23 PHP
PHP 函数语法介绍一
2009/06/14 PHP
又一个php 分页类实现代码
2009/12/03 PHP
CodeIgniter基本配置详细介绍
2013/11/12 PHP
php实现每天自动变换随机问候语的方法
2015/05/12 PHP
CentOS7系统搭建LAMP及更新PHP版本操作详解
2020/03/26 PHP
jQuery插件的写法分享
2013/06/12 Javascript
JavaScript获取和设置CheckBox状态的简单方法
2013/07/05 Javascript
JS清除IE浏览器缓存的方法
2013/07/26 Javascript
Javascript控制input输入时间格式的方法
2015/01/28 Javascript
JQuery的ON()方法支持的所有事件罗列
2015/02/28 Javascript
jQuery中数据缓存$.data的用法及源码完全解析
2016/04/29 Javascript
懒加载实现的分页&amp;&amp;网站footer自适应
2016/12/21 Javascript
js自定义弹框插件的封装
2020/08/24 Javascript
Vue.js 表单控件操作小结
2018/03/29 Javascript
关于Vue在ie10下空白页的debug小结
2018/05/02 Javascript
JS实现点击拉拽轮播图pc端移动端适配
2018/09/05 Javascript
vue微信分享出来的链接点开是首页问题的解决方法
2018/11/28 Javascript
vue+element模态框中新增模态框和删除功能
2019/06/11 Javascript
bootstrap+spring boot实现面包屑导航功能(前端代码)
2019/10/09 Javascript
JS highcharts实现动态曲线代码示例
2020/10/16 Javascript
[04:42]5分钟带你了解什么是DOTA2(第一期)
2017/02/07 DOTA
详解Django中的form库的使用
2015/07/18 Python
Python中的数学运算操作符使用进阶
2016/06/20 Python
python 文件操作api(文件操作函数)
2016/08/28 Python
python中实现将多个print输出合成一个数组
2018/04/19 Python
Python学习笔记之pandas索引列、过滤、分组、求和功能示例
2019/06/03 Python
numpy ndarray 按条件筛选数组,关联筛选的例子
2019/11/26 Python
HTML5中语义化 b 和 i 标签
2008/10/17 HTML / CSS
canvas使用注意点总结
2013/07/19 HTML / CSS
英国天然保健品网站:Simply Supplements
2017/03/22 全球购物
如何用PHP实现邮件发送
2012/12/26 面试题
大学生最新职业生涯规划书范文
2014/01/12 职场文书
艺术设计专业个人求职信
2014/04/10 职场文书
离职报告格式
2014/11/04 职场文书
酒店前台岗位职责
2015/04/16 职场文书