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

 更新时间:2015年04月30日 10:01:53   作者:休闲生活文化  
这篇文章主要介绍了javascript实现鼠标拖动改变层大小的方法,涉及javascript操作鼠标事件及样式的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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程序设计有所帮助。

相关文章

  • Webpack中Source Map配置深入解析

    Webpack中Source Map配置深入解析

    这篇文章主要为大家介绍了Webpack中Source Map配置深入解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • javascript正则表达式基础知识入门

    javascript正则表达式基础知识入门

    很长时间没看正则表达式了,碰巧今天用到,温故知新了一把,这里记录下来,分享给大家,都是些基础的知识,重点给大家讲解的是正则表达式中4种常用的方法,50% 的举一反三练习中的原创。
    2015-04-04
  • 轻量级JS Cookie插件js-cookie的使用方法

    轻量级JS Cookie插件js-cookie的使用方法

    js-cookie插件是一个JS操作cookie的插件,源文件只有3.34 KB,非常轻量级,js-cookie也支持npm和Bower安装和管理,下面看看js-cookie的具体用法
    2018-03-03
  • 用循环或if语句从json中取数据示例

    用循环或if语句从json中取数据示例

    倘若想将id和pid数据依次取出,就只能用循环,若想有选择性的输出时,需要添加if条件
    2014-08-08
  • javascript自动生成包含数字与字符的随机字符串

    javascript自动生成包含数字与字符的随机字符串

    这篇文章主要介绍了javascript自动生成包含数字与字符的随机字符串,涉及Math.random()和Math.floor()两个函数的使用技巧,需要的朋友可以参考下
    2015-02-02
  • 深入浅析Bootstrap列表组组件

    深入浅析Bootstrap列表组组件

    列表组是灵活又强大的组件,不仅能用于显示一组简单的元素,还能用于复杂的定制的内容。本文给大家介绍Bootstrap列表组组件,感兴趣的朋友一起学习吧
    2016-05-05
  • canvas实现手机端用来上传用户头像的代码

    canvas实现手机端用来上传用户头像的代码

    这篇文章主要介绍了canvas实现手机端用来上传用户头像的代码代码简单易懂非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-10-10
  • Taro小程序自定义顶部导航栏功能的实现

    Taro小程序自定义顶部导航栏功能的实现

    这篇文章主要介绍了Taro小程序自定义顶部导航栏功能的实现,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • js实现前端跨域postMessage的具体使用

    js实现前端跨域postMessage的具体使用

    这篇文章主要介绍了js实现前端跨域postMessage的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • JS实现可以用键盘方向键控制的动画

    JS实现可以用键盘方向键控制的动画

    这篇文章主要为大家详细介绍了JS实现可以用键盘方向键控制的动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-12-12

最新评论