JavaScript实现模仿桌面窗口的方法


Posted in Javascript onJuly 18, 2015

本文实例讲述了JavaScript实现模仿桌面窗口的方法。分享给大家供大家参考。具体如下:

这里使用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>
<title>JS山寨桌面窗口</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<style type="text/css" media="screen">
html, body, div {
 margin: 0;
 padding: 0;
} 
html, body {
 background: #FFFFFF;
 width: 100%;
 height: 100%;
 overflow: hidden;
}
#box {
 position: absolute;
 top: 30%;
 left: 40%;
 width: 250px;
 height: 150px;
 background: #EEE;
 border: 1px solid #666;
 border-radius: 8px;
 box-shadow: 2px 2px 5px #777;
}
/*标题栏*/
#boxHeader {
 width: 100%;
 height: 30px;
 background: none!important;
 background: #EEE;
 border-bottom: 2px solid #AAA;
 border-radius: 5px 5px 0 0;
}
#button {
 float: right;
 width: 79px;
 height: 15px;
 margin: 5px 5px 0 0!important;
 margin: 5px 2px 0 0;
 background: #CCC;
 border-radius: 5px;
}
#button div {
 float: left;
 width: 25px;
 height: 15px;
 border-right: 2px #AAA solid;
}
#button .close {
 border: none;
 border-radius: 0px 5px 5px 0;
}
#button .minimize {
 border-radius: 5px 0 0 5px;
}
/*八个方向*/
/*用于显示变栏颜色,作为测试
#boxN, #boxE, #boxS, #boxW {
 background: red;
}
#boxNE, #boxES, #boxSW, #boxWN {
 background: green;
}
*/
#boxN{
 position: absolute;
 top: 0;
 left: 0;
 width: 100%;
 height: 5px;
 overflow: hidden;
}
#boxE{
 position: absolute;
 top: 0;
 right: 0;
 width: 5px;
 height: 100%;
 overflow: hidden;
}
#boxS{
 position: absolute;
 bottom: 0;
 left: 0;
 width: 100%;
 height: 5px;
 overflow: hidden;
}
#boxW{
 position: absolute;
 top: 0;
 left: 0;
 width: 5px;
 height: 100%;
 overflow: hidden;
}
#boxNE {
 position: absolute;
 right: 0;
 top: 0;
 width: 5px;
 height: 5px;
 overflow: hidden;
}
#boxES {
 position: absolute;
 right: 0;
 bottom: 0;
 width: 5px;
 height: 5px;
 overflow: hidden;
}
#boxSW {
 position: absolute;
 left: 0;
 bottom: 0;
 width: 5px;
 height: 5px;
 overflow: hidden;
}
#boxWN {
 position: absolute;
 left: 0;
 top: 0;
 width: 5px;
 height: 5px;
 overflow: hidden;
}
/*显示按钮*/
#showButton {
 display: none;
 position: absolute;
 top: 50%;
 left: 50%;
 margin: -75px 0 0 -75px; 
 width: 150px;
 height: 150px;
}
#showButton span {
 font: 50px bolder;
}
/*改变大小时的预览DIV*/
#virtualBox {
 position: absolute;
 background: #8EC6FF;
 border: 1px solid #147AFF;
 border-radius: 8px;
 opacity: 0.4;
 filter: alpha(opacity = 40);
}
</style>
<script type="text/javascript">
//拖扯box函数
var dragDiv = function() {
 var box = document.getElementById("box");
 var boxHeader = document.getElementById("boxHeader");
 var bDraging = false;
 var disX = disY = 0;
 //记录鼠标按下时距离box左、上边框距离
 boxHeader.onmousedown = function(event) {
  bDraging = true;
  document.body.style.cursor = "move";
  var event = event || window.event;
  disX = event.clientX - box.offsetLeft;
  disY = event.clientY - box.offsetTop;
  //拖动box
  document.onmousemove = function(event) {
   if(!bDraging) return false;
   document.body.style.cursor = "move";
   var event = event || window.event;
   var boxX = event.clientX - disX;
   var boxY = event.clientY - disY;
   var maxX = document.body.scrollWidth - box.offsetWidth;
   var maxY = document.body.offsetHeight - box.offsetHeight;
   boxX = (boxX < 0) ? 0 : boxX;
   boxY = (boxY < 0) ? 0 : boxY;
   boxX = (boxX > maxX) ? maxX : boxX;
   boxY = (boxY > maxY) ? maxY : boxY;
   box.style.left = boxX + "px";
   box.style.top = boxY + "px";
  };
  document.onmouseup = function() {
   bDraging = false;
   document.body.style.cursor = "";
  };
  return false;
 };
};
var changeSize = function() {
 var box = document.getElementById("box");
 var virtualBox = document.getElementById("virtualBox");
 var boxSide = document.getElementById("boxSide").getElementsByTagName("div");
 var bSizeChanging = bMousedowning = false;
 //box是否正在改变 & 鼠标是否正在按下
 var originalWidth = box.offsetWidth;
 //box最原始宽度
 var originalHeight = box.offsetHeight;
 //box最原始高度
 for(var i = 0; i < boxSide.length; i++) {
 //遍历boxside,监听鼠标
  boxSide[i].index = i;
  boxSide[i].onmouseover = function() {
   if(bMousedowning) return false;
   changeCursor(true, this.index);
  };
  boxSide[i].onmouseout = function() {
   if(bMousedowning) return false;
   changeCursor(false, this.index);
  };
  boxSide[i].onmousedown = function(event) {
   var event = event || window.event;   
   var index = this.index;
   var aBoxPrevious = new Array();
   //记录box上一次的状态
   aBoxPrevious["clientX"] = event.clientX;
   aBoxPrevious["clientY"] = event.clientY;
   aBoxPrevious["left"] = box.offsetLeft;
   aBoxPrevious["top"]= box.offsetTop;
   aBoxPrevious["width"] = box.offsetWidth;
   aBoxPrevious["height"] = box.offsetHeight;
   bMousedowning = true;
   bSizeChanging = true;
   showVirtualBox(virtualBox, aBoxPrevious);
   document.onmousemove = function(event) {
    if(!bSizeChanging) return false;
    changeVirtualBoxSize(event, aBoxPrevious, index);
   };
   document.onmouseup = function() {
    changeBoxSize(virtualBox)
    hideVirtualBox(virtualBox);
    bSizeChanging = false;
    bMousedowning = false;
    changeCursor(false, index);
   };
   return false;
  };
 }
 //改变鼠标指针样式
 var changeCursor = function(bIsShowing, index) {
  if(bIsShowing) {
   var cursorStyle = ["n-resize","e-resize","s-resize","w-resize","ne-resize","se-resize","sw-resize","nw-resize"];
   document.body.style.cursor = cursorStyle[index];
  }
  else {
   document.body.style.cursor = "";
  }
 };
 //显示预览DIV
 var showVirtualBox = function(virtualBox, aBoxPrevious) {
  with(virtualBox.style) {
   display = "block";
   top = aBoxPrevious["top"] + "px";
   left = aBoxPrevious["left"] + "px";
   width = aBoxPrevious["width"] + "px";
   height = aBoxPrevious["height"] + "px";
  }
 }
 //隐藏预览DIV
 var hideVirtualBox = function(virtualBox) {
  virtualBox.style.display = "none";
 }
 //改变box大小
 var changeVirtualBoxSize = function(event, aBoxPrevious, index) {
  var event = event || window.event;
  var bTop = bRight = bBottom = bLeft = false;
  //八个方向,分别为N、E、S、W、NE、SW、SW、NW
  switch (index) {
   case 0:
    bTop = true;
    break;
   case 1:
    bRight = true;
    break;
   case 2:
    bBottom = true;
    break;
   case 3:
    bLeft = true;
    break;
   case 4:
    bTop = bRight = true;;
    break;
   case 5:
    bRight = bBottom = true;
    break;
   case 6:
    bBottom = bLeft = true;
    break;
   case 7:
    bLeft = bTop = true;
    break;
   default:
    break;
  }
  //向北改变高度
  if(bTop) {
   var newTopHeight = aBoxPrevious["height"] - (event.clientY - aBoxPrevious["clientY"]);
   (newTopHeight < originalHeight) && (newTopHeight = originalHeight);
   (newTopHeight > aBoxPrevious["top"] + aBoxPrevious["height"]) && (newTopHeight = aBoxPrevious["top"] + aBoxPrevious["height"]);
   var newTop = aBoxPrevious["top"] + (event.clientY - aBoxPrevious["clientY"]);
   (newTop > aBoxPrevious["top"] + aBoxPrevious["height"] - originalHeight) && (newTop = aBoxPrevious["top"] + aBoxPrevious["height"] - originalHeight);
   (newTop < 0) && (newTop = 0);
   virtualBox.style.top = newTop + "px";
   virtualBox.style.height = newTopHeight - box.clientTop * 2 + "px"; 
   //不能忽略border-width
   bTop = false;
  }
  //向东改变宽度
  if(bRight) {
   var newRightWidth = aBoxPrevious["width"] + (event.clientX - aBoxPrevious["clientX"]);
   (newRightWidth < originalWidth) && (newRightWidth = originalWidth);
   (newRightWidth > document.body.scrollWidth - aBoxPrevious["left"]) && (newRightWidth = document.body.scrollWidth - aBoxPrevious["left"]);
   virtualBox.style.width = newRightWidth - box.clientTop * 2 + "px";
   bRight = false;
  }
  //向南改变高度
  if(bBottom) {
   var newBottomHeight = aBoxPrevious["height"] + (event.clientY - aBoxPrevious["clientY"]);
   (newBottomHeight < originalHeight) && (newBottomHeight = originalHeight);
   (newBottomHeight > document.body.scrollHeight - aBoxPrevious["top"]) && (newBottomHeight = document.body.scrollHeight - aBoxPrevious["top"]);
   virtualBox.style.height = newBottomHeight - box.clientTop * 2 + "px";
   bBottom = false;
  }
  //向西改变宽度
  if(bLeft) {
   var newLeftWidth = aBoxPrevious["width"] - (event.clientX - aBoxPrevious["clientX"]);
   (newLeftWidth < originalWidth) && (newLeftWidth = originalWidth);
   (newLeftWidth > aBoxPrevious["left"] + aBoxPrevious["width"]) && (newLeftWidth = aBoxPrevious["left"] + aBoxPrevious["width"]);
   var newLeft = aBoxPrevious["left"] + (event.clientX - aBoxPrevious["clientX"]);
   (newLeft > aBoxPrevious["left"] + aBoxPrevious["width"] - originalWidth) && (newLeft = aBoxPrevious["left"] + aBoxPrevious["width"] - originalWidth);
   (newLeft < 0) && (newLeft = 0);
   virtualBox.style.left = newLeft + "px";
   virtualBox.style.width = newLeftWidth - box.clientLeft * 2 + "px";
   bLeft = false;
  }
 };
 var changeBoxSize = function(virtualBox) {
  with(box.style) {
   left = virtualBox.style.left;
   top = virtualBox.style.top;
   width = virtualBox.style.width;
   height = virtualBox.style.height;
  }
 }
};
//窗口按钮函数
boxButton = function() {
 var box = document.getElementById("box");
 var boxHeader = document.getElementById("boxHeader");
 var aButton = document.getElementById("button").getElementsByTagName("div");
 var showButton = document.getElementById("showButton");
 var span = showButton.getElementsByTagName("span")[0];
 var bIsMin = bIsMax = false;
 //目前状态是否最小 or 最大
 boxHeader.ondblclick = function() {
  maximize();
 }
 for(var i = 0; i < aButton.length; i++) {
  aButton[i].index = i;
  aButton[i].onmouseover = function() {
   aButton[this.index].style.background = "#AAA";
   document.body.style.cursor = "pointer";
  };
  aButton[i].onmouseout = function() {
   aButton[this.index].style.background = "";
   document.body.style.cursor = ""
  };
  aButton[i].onclick = function() {
   switch(this.index) {
    case 0:
     minimize();
     break;
    case 1:
     maximize();
     break;
    case 2:
     close();
     break;
    default:
     break;
   }
  };
 }
 var minimize = function() {
  if(bIsMin) {
   resumeBox();
   bIsMin = false;
  }
  else {
   box.style.width = "89px";
   box.style.height = "32px";
   box.style.left = "2%";
   box.style.top = document.body.offsetHeight - box.offsetHeight - 15 + "px";
   bIsMin = true;
   bIsMax = false;
  }
 };
 var maximize = function() {
  if(bIsMax) {
   resumeBox();
   bIsMax = false;
  }
  else {
   box.style.width = document.body.scrollWidth - 10 + "px";
   box.style.height = document.body.scrollHeight - 10 + "px";
   box.style.left = "5px";
   box.style.top = "5px";
   bIsMax = true;
   bIsMin = false;
  }
 };
 var close = function() {
  box.style.display = "none";
  showButton.style.display = "block";
 };
 var resumeBox = function() {
  box.style.top = "30%";
  box.style.left = "40%";
  box.style.width = "250px";
  box.style.height = "150px";
 };
 showButton.onmousedown = function() {
  span.innerHTML = "^o^";
 };
 showButton.onclick = function() {
  this.style.display = "none";
  span.innerHTML = ">_<";
  resumeBox();
  box.style.display = "block";
 };
};
window.onload = function() {
 changeSize();
 dragDiv();
 boxButton();
};
</script>
</head>
<body>
<div id="box">
 <div id="boxHeader">
  <div id="button">
   <div class="minimize"></div>
   <div class="maximize"></div>
   <div class="close"></div>
  </div>
 </div>
 <div id="boxSide">
  <div id="boxN"></div>
  <div id="boxE"></div>
  <div id="boxS"></div>
  <div id="boxW"></div>
  <div id="boxNE"></div>
  <div id="boxES"></div>
  <div id="boxSW"></div>
  <div id="boxWN"></div>
 </div>
</div>
<button id="showButton"><span>>_<</span><p>居然关掉人家,讨厌~</p><p>快打开</p></button>
<div id="virtualBox"></div>
</body>
</html>

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

Javascript 相关文章推荐
xmlHTTP实例
Oct 24 Javascript
用js实现的抽象CSS圆角效果!!
May 03 Javascript
ie8 不支持new Date(2012-11-10)问题的解决方法
Jul 31 Javascript
escape编码与unescape解码汉字出现乱码的解决方法
Jul 02 Javascript
jQuery圆形统计图开发实例
Jan 04 Javascript
js操作数据库实现注册和登陆的简单实例
May 26 Javascript
js获取页面引用的css样式表中的属性值方法(推荐)
Aug 19 Javascript
js利用appendChild对标签进行排序的实现方法
Oct 16 Javascript
webpack4 SCSS提取和懒加载的示例
Sep 03 Javascript
微信小程序页面间传值与页面取值操作实例分析
Apr 30 Javascript
javascript实现图片轮换动作方法
Aug 07 Javascript
微信小程序实现点击导航标签滚动定位到对应位置
Nov 19 Javascript
js实现固定显示区域内自动缩放图片的方法
Jul 18 #Javascript
javascript顺序加载图片的方法
Jul 18 #Javascript
javascript字符串循环匹配实例分析
Jul 17 #Javascript
动态加载jQuery的两种方法实例分析
Jul 17 #Javascript
javascript父子页面通讯实例详解
Jul 17 #Javascript
javascript实现的闭包简单实例
Jul 17 #Javascript
javascript实现的字符串与十六进制表示字符串相互转换方法
Jul 17 #Javascript
You might like
Zend Studio 无法启动的问题解决方法
2008/12/04 PHP
探讨:如何使用PhpDocumentor生成文档
2013/06/25 PHP
ThinkPHP V2.2说明文档没有说明的那些事实例小结
2015/07/01 PHP
PHP读取文件内容的五种方式
2015/12/28 PHP
php实现的http请求封装示例
2016/11/08 PHP
PHP字符串逆序排列实现方法小结【strrev函数,二分法,循环法,递归法】
2017/01/13 PHP
PHP正则+Snoopy抓取框架实现的抓取淘宝店信誉功能实例
2017/05/17 PHP
tp5 sum某个字段相加得到总数的例子
2019/10/18 PHP
js获取当月最后一天实例代码
2013/11/19 Javascript
Flash图片上传组件 swfupload使用指南
2015/03/14 Javascript
Jquery时间轴特效(三种不同类型)
2015/11/02 Javascript
jquery无限级联下拉菜单简单实例演示
2015/11/23 Javascript
Jquery左右滑动插件之实现超级炫酷动画效果附源码下载
2015/12/02 Javascript
js判断上传文件后缀名是否合法
2016/01/28 Javascript
JS功能代码集锦
2016/05/04 Javascript
浅析$(function) ready和onload 的区别
2016/09/03 Javascript
详解NodeJs支付宝移动支付签名及验签
2017/01/06 NodeJs
JavaScript实现随机数生成器(去重)
2017/10/13 Javascript
使用clipboard.js实现复制功能的示例代码
2017/10/16 Javascript
angularJS实现动态添加,删除div方法
2018/02/27 Javascript
详解Webpack-dev-server的proxy用法
2018/09/08 Javascript
微信小程序实现多图上传
2020/06/19 Javascript
jquery实现抽奖功能
2020/10/22 jQuery
Python常用模块之requests模块用法分析
2019/05/15 Python
HTML5到底会有什么发展?HTML5的前景展望
2015/07/07 HTML / CSS
英国蛋糕装饰用品一站式商店:Craft Company
2019/03/18 全球购物
机电一体化专业应届本科生求职信
2013/09/27 职场文书
接待员岗位责任制
2014/02/10 职场文书
金融学专科生自我鉴定
2014/02/21 职场文书
优秀实习生感言
2014/03/01 职场文书
个人四风问题对照检查材料
2014/10/01 职场文书
redis 查看所有的key方式
2021/05/07 Redis
Pytorch数据读取之Dataset和DataLoader知识总结
2021/05/23 Python
python生成可执行exe控制Microsip自动填写号码并拨打功能
2021/06/21 Python
Pytorch中使用ImageFolder读取数据集时忽略特定文件
2022/03/23 Python
postgresql中如何执行sql文件
2023/05/08 PostgreSQL