Js可拖拽放大的层拖动特效实现方法

 更新时间:2015年02月25日 10:27:15   作者:代码家园  
这篇文章主要介绍了Js可拖拽放大的层拖动特效实现方法,涉及javascript操作DOM元素及css样式的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Js可拖拽放大的层拖动特效实现方法。分享给大家供大家参考。具体实现方法如下:

复制代码 代码如下:
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Js实现层拖动效果,还可以拖拽放大</title>
<style>
*{margin:0;padding:0;}
#zhezhao{
 width:100%;
 height:100%;
 background:#f00;
 filter:alpha(opacity:0);
 opacity:0;
 z-index:9999;
 position:absolute;
 top:0;
 left:0;
 display:none;
}
#div2{
 width:200px;
 height:200px;
 position:relative;
 background:#EEEEEE;
 border:1px solid #f00;
}
#div1{
 width:15px;
 height:15px;
 background:#99CC00;
 position:absolute;
 right:0px;
 bottom:0px;
 cursor:nw-resize;
 overflow:hidden;
 font-size:12px;
 text-align:center;
 line-height:15px;
 color:#FFFFFF;
 float:right;
 z-index:3;
}
#right{
 width:15px;
 height:100%;
 background:#f00;
 float:right;
 position:absolute;
 right:0;
 top:0;
 cursor:e-resize;
 overflow:hidden;
 filter:alpha(opacity:0);
 opacity:0;
 z-index:1;
}
#bottom{
 width:100%;
 height:15px;
 background:#f00;
 position:absolute;
 left:0;
 bottom:0;
 cursor:n-resize;
 overflow:hidden;
 filter:alpha(opacity:0);
 opacity:0;
 z-index:1;
}
#div2 p{
 padding:10px;
 line-height:24px;
 font-size:13px;
 text-indent:24px;
 color:#996600;
}
#div2 h2{
 width:100%;
 height:25px;
 line-height:25px;
 font-size:14px;
 background:#CC9900;
 color:#FFFFFF;
 text-indent:15px;
 cursor:move;
 overflow:hidden;
}
</style>
<script type="text/javascript">
window.onload=function()
{
 var oDiv=document.getElementById("div1");
 var oDiv2=document.getElementById("div2");
 var zhezhao=document.getElementById("zhezhao");
 var h2=oDiv2.getElementsByTagName("h2")[0];
 var right=document.getElementById("right");
 var bottom=document.getElementById("bottom");
 var mouseStart={};
 var divStart={};
 var rightStart={};
 var bottomStart={};
 //往右拽
 right.onmousedown=function(ev)
 {
  var oEvent=ev||event;
  mouseStart.x=oEvent.clientX;
  mouseStart.y=oEvent.clientY;
  rightStart.x=right.offsetLeft;
  if(right.setCapture)
  {
   right.onmousemove=doDrag1;
   right.onmouseup=stopDrag1;
   right.setCapture();
  }
  else
  {
   document.addEventListener("mousemove",doDrag1,true);
   document.addEventListener("mouseup",stopDrag1,true);
  }
 };
 function doDrag1(ev)
 {
  var oEvent=ev||event;
  var l=oEvent.clientX-mouseStart.x+rightStart.x;
  var w=l+oDiv.offsetWidth;
 
  if(w<oDiv.offsetWidth)
  {
   w=oDiv.offsetWidth;
  }
  else if(w>document.documentElement.clientWidth-oDiv2.offsetLeft)
  {
   w=document.documentElement.clientWidth-oDiv2.offsetLeft-2;
  }
  oDiv2.style.width=w+"px";
 };
 function stopDrag1()
 {
  if(right.releaseCapture)
  {
   right.onmousemove=null;
   right.onmouseup=null;
   right.releaseCapture();
  }
  else
  {
   document.removeEventListener("mousemove",doDrag1,true);
   document.removeEventListener("mouseup",stopDrag1,true);
  }
 };
 //往下拽
 bottom.onmousedown=function(ev)
 {
  var oEvent=ev||event;
  mouseStart.x=oEvent.clientX;
  mouseStart.y=oEvent.clientY;
  bottomStart.y=bottom.offsetTop;
  if(bottom.setCapture)
  {
   bottom.onmousemove=doDrag2;
   bottom.onmouseup=stopDrag2;
   bottom.setCapture();
  }
  else
  {
   document.addEventListener("mousemove",doDrag2,true);
   document.addEventListener("mouseup",stopDrag2,true);
  }
 };
 function doDrag2(ev)
 {
  var oEvent=ev||event;
  var t=oEvent.clientY-mouseStart.y+bottomStart.y;
  var h=t+oDiv.offsetHeight;
 
  if(h<oDiv.offsetHeight)
  {
   h=oDiv.offsetHeight;
  }
  else if(h>document.documentElement.clientHeight-oDiv2.offsetTop)
  {
   h=document.documentElement.clientHeight-oDiv2.offsetTop-2;
  }
 
  oDiv2.style.height=h+"px";
 };
 function stopDrag2()
 {
  if(bottom.releaseCapture)
  {
   bottom.onmousemove=null;
   bottom.onmouseup=null;
   bottom.releaseCapture();
  }
  else
  {
   document.removeEventListener("mousemove",doDrag2,true);
   document.removeEventListener("mouseup",stopDrag2,true);
  }
 };
 //往左右同时拽
 oDiv.onmousedown=function(ev)
 {
  var oEvent=ev||event;
  mouseStart.x=oEvent.clientX;
  mouseStart.y=oEvent.clientY;
  divStart.x=oDiv.offsetLeft;
  divStart.y=oDiv.offsetTop;
  if(oDiv.setCapture)
  {
   oDiv.onmousemove=doDrag;
   oDiv.onmouseup=stopDrag;
   oDiv.setCapture();
  }
  else
  {
   document.addEventListener("mousemove",doDrag,true);
   document.addEventListener("mouseup",stopDrag,true);
  }
  zhezhao.style.display='block';
 };
 function doDrag(ev)
 {
  var oEvent=ev||event;
  var l=oEvent.clientX-mouseStart.x+divStart.x;
  var t=oEvent.clientY-mouseStart.y+divStart.y;
 
 
  var w=l+oDiv.offsetWidth;
  var h=t+oDiv.offsetHeight;
 
  if(w<oDiv.offsetWidth)
  {
   w=oDiv.offsetWidth;
  }
  else if(w>document.documentElement.clientWidth-oDiv2.offsetLeft)
  {
   w=document.documentElement.clientWidth-oDiv2.offsetLeft-2;
  }
  if(h<oDiv.offsetHeight)
  {
   h=oDiv.offsetHeight;
  }
  else if(h>document.documentElement.clientHeight-oDiv2.offsetTop)
  {
   h=document.documentElement.clientHeight-oDiv2.offsetTop-2;
  }
 
  oDiv2.style.width=w+"px";
  oDiv2.style.height=h+"px";
 };
 function stopDrag()
 {
  if(oDiv.releaseCapture)
  {
   oDiv.onmousemove=null;
   oDiv.onmouseup=null;
   oDiv.releaseCapture();
  }
  else
  {
   document.removeEventListener("mousemove",doDrag,true);
   document.removeEventListener("mouseup",stopDrag,true);
  }
  zhezhao.style.display='none';
 };
 
 //h2完美拖拽
 h2.onmousedown=function(ev)
 {
  var oEvent=ev||event;
  mouseStart.x=oEvent.clientX;
  mouseStart.y=oEvent.clientY;
  divStart.x=oDiv2.offsetLeft;
  divStart.y=oDiv2.offsetTop;
 
  if(h2.setCapture)
  {
   h2.onmousemove=doDrag3;
   h2.onmouseup=stopDrag3;
   h2.setCapture();
  }
  else
  {
   document.addEventListener("mousemove",doDrag3,true);
   document.addEventListener("mouseup",stopDrag3,true);
  }
 
  zhezhao.style.display='block';
 };
 function doDrag3(ev)
 {
  var oEvent=ev||event;
  var l=oEvent.clientX-mouseStart.x+divStart.x;
  var t=oEvent.clientY-mouseStart.y+divStart.y;
  if(l<0)
  {
   l=0;
  }
  else if(l>document.documentElement.clientWidth-oDiv2.offsetWidth)
  {
   l=document.documentElement.clientWidth-oDiv2.offsetWidth;
  }
  if(t<0)
  {
   t=0;
  }
  else if(t>document.documentElement.clientHeight-oDiv2.offsetHeight)
  {
   t=document.documentElement.clientHeight-oDiv2.offsetHeight;
  }
  oDiv2.style.left=l+"px";
  oDiv2.style.top=t+"px";
 };
 function stopDrag3()
 {
  if(h2.releaseCapture)
  {
   h2.onmousemove=null;
   h2.onmouseup=null;
   h2.releaseCapture();
  }
  else
  {
   document.removeEventListener("mousemove",doDrag3,true);
   document.removeEventListener("mouseup",stopDrag3,true);
  }
 
  zhezhao.style.display='none';
 }
};
</script>
</head>
<body>
<div id="div2">
 <div style="width:100%; height:100%; overflow:hidden;">
 <h2>完美的拖拽</h2>
 <p>体验不错的JavaScript网页拖动,除了拖动,还可拖动放大,像Windows窗口一样被放大或缩小,只要按住层的右下角,就可以收放自如的放大或缩小。想使用的朋友,可将代码里的Js封装成类,从外部引入想必更合理些。'</p>
 <div id="right"></div>
 <div id="div1">拖</div>
 <div id="bottom"></div>
 </div>
</div>
<div id="zhezhao"></div>
</body>
</html>

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

相关文章

  • 自己的js工具 Cookie 封装

    自己的js工具 Cookie 封装

    有些时候我们的web程序需要利用cookie来实现一些功能,很多时候我们并不需要非得用服务端来操作cookie,因为js可以也操作cookie.
    2009-08-08
  • JavaScript实现矩形块大小任意缩放

    JavaScript实现矩形块大小任意缩放

    这篇文章主要为大家详细介绍了JavaScript实现矩形块大小任意缩放,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-08-08
  • 详解ES6 Symbol 的用途

    详解ES6 Symbol 的用途

    Symbol 唯一的用途就是标识对象属性,表明对象支持的功能。 这篇文章给大家介绍ES6 Symbol 的用途,感兴趣的朋友跟随小编一起看看吧
    2018-10-10
  • js中匿名函数的创建与调用方法分析

    js中匿名函数的创建与调用方法分析

    这篇文章主要介绍了js中匿名函数的创建与调用方法,详细分析了匿名函数的原理与用法,以及闭包的原理分析,非常具有实用价值,需要的朋友可以参考下
    2014-12-12
  • js实现图片上传预览原理分析

    js实现图片上传预览原理分析

    这篇文章主要为大家详细介绍了js实现图片上传预览的原理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • html5+CSS 实现禁止IOS长按复制粘贴功能

    html5+CSS 实现禁止IOS长按复制粘贴功能

    因为在移动端APP需要实现长按执行别的事件,但是在iOS系统有默认的长按选择复制粘贴。禁止在网上找了很多资料,下面小编给大家分享解决方案,一起看看吧
    2016-12-12
  • 微信小程序如何实现数据共享与方法共享详解

    微信小程序如何实现数据共享与方法共享详解

    这篇文章主要给大家介绍了关于微信小程序如何实现数据共享与方法共享的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2022-01-01
  • Web技术实现移动监测的介绍

    Web技术实现移动监测的介绍

    移动侦测,一般也叫运动检测,常用于无人值守监控录像和自动报警。通过摄像头按照不同帧率采集得到的图像会被 CPU 按照一定算法进行计算和比较,当画面有变化时,如有人走过,镜头被移动,计算比较结果得出的数字会超过阈值并指示系统能自动作出相应的处理
    2017-09-09
  • 图解JavaScript作用域链底层原理

    图解JavaScript作用域链底层原理

    当代码在一个环境中执行时,会创建变量对象的一个作用域链,作用域链的用途是保证对执行环境有权访问的所有变量和函数的有序访问,下面这篇文章主要给大家介绍了关于JavaScript作用域链底层原理的相关资料,需要的朋友可以参考下
    2021-11-11
  • cocos2dx+lua实现橡皮擦功能

    cocos2dx+lua实现橡皮擦功能

    这篇文章主要为大家详细介绍了cocos2dx+lua实现橡皮擦功能,类似刮刮乐效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12

最新评论