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

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

(0)

相关推荐

  • javascript弹出拖动窗口

    可以拖动的弹出窗口,在很多网页上都可以见到,非常的人性化,下面通过一段javascript代码就可以实现弹出拖动窗口,废话不多说了,直接贴代码了. <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www.jb51.net/" /> <title>

  • JavaScript实现的圆形浮动标签云效果实例

    本文实例讲述了JavaScript实现的圆形浮动标签云效果.分享给大家供大家参考.具体如下: 这里介绍的JS标签云效果,在鼠标的作用下会自动转动,整体上围绕成一个圆形,各个标签之间无需Div代码,直接文字+链接的形式,有多少就显示多少,JavaScript会自动调整显示数量,让视觉效果最佳. 运行效果如下图所示: 具体代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://

  • JavaScript实现上下浮动的窗口效果代码

    本文实例讲述了JavaScript实现上下浮动的窗口效果代码.分享给大家供大家参考.具体如下: 这里介绍使用JavaScript实现上下浮动的窗口,在垂直方向上漂浮,代码内的JS函数有超丰富的浮动层定义功能,像浮动层位置高度.初始化事件触发器.设定浮动层为可见,用style.left设定浮动层左边距.浮动层的运动速度等,还有更多的设置选项都能实现. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/js-up-down-float-move-win

  • JavaScript实现的浮动层框架用法实例分析

    本文实例讲述了JavaScript实现的浮动层框架用法.分享给大家供大家参考.具体如下: 这是一个JavaScript编写的浮动层框架,作为框架来说,骨干已经完成了.可以实现"类"."构造函数"."继承"(木有实现多继承)."静态方法"."重载"(木有实现多态),对我来说主要的意义就是,我喜欢这种代码风格,可以非常简单的重用和二次开发. 运行效果截图如下: 在线演示地址如下: http://demo.jb5

  • javascript实现div浮动在网页最顶上并带关闭按钮效果实例

    复制代码 代码如下: <html> <head> <title>javascript实现div浮动在网页最顶上并带关闭按钮效果实例</title> <style type="text/css"> <!-- body { margin: 0px;padding: 0px;text-align: center;} TD {FONT-SIZE: 12px; COLOR: #333;} #toubiao {BORDER-BOTT

  • javascript 表格排序和表头浮动效果(扩展SortTable)

    一.SortTable说明 SortTable version 2 7th April 2007 Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/ Instructions: Download this file Add <script src="sorttable.js"></script> to your HTML Add class="sortable"

  • JavaScript实现网页上的浮动广告的简单方法

    漫游于网络之间,你会发觉,因特网不但是信息的海洋,也是广告的海洋.除了普通的Gif Banner.Flash外,浮动广告也是时下网上较为流行的广告形式之一.当你拖动浏览器的滚动条时,这种在页面上浮动的广告,可以跟随屏幕一起移动.尽管这种效果对于广告展示有相当的实用价值,但对浏览你网页的人来讲,这则是个既妨碍阅读,又影响阅读兴趣的东西,因此一定不能滥用.不过,如果你能善用的话,它就能发挥出极大的作用. 要做出浮动式广告的效果并不困难,如果你有JS基础的可以自己写一个,如果连写都懒得写的话,到网上下

  • 如何制作浮动广告 JavaScript制作浮动广告代码

    如果有一定的JavaScript基础,制作浮动广告还是比较容易的.直接上代码了: 复制代码 代码如下: <!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"

  • JavaScript 浮动定位提示效果实现代码第1/2页

    这个效果本身难度不大,主要在程序结构和扩展中下了些功夫,务求用起来更方便,能用在更多的地方.程序特点 1,同一个提示框用在多个触发元素时,只需一个实例: 2,显示和隐藏分别有点击方式和触发方式选择: 3,能设置延时显示和隐藏: 4,有25种预设定位位置: 5,可在预设定位基础上,再自定义定位: 6,可设置自适应窗口定位: 程序说明 [Tip对象] Tip对象就是用来显示提示信息的容器,程序用Tip属性表示.这个没什么要求,程序初始化时会对它进行一些设置. 首先进行下面设置: 复制代码 代码如下:

  • JavaScript点击按钮后弹出透明浮动层的方法

    本文实例讲述了JavaScript点击按钮后弹出透明浮动层的方法.分享给大家供大家参考.具体分析如下: 这里实现点击后页面变灰色,并用JS弹出一个居中的浮动层提示窗口,这个窗口是透明的,可以设置透明度,网上已经有很多类似的JavaScript代码,你可以借鉴一下. <HTML> <HEAD> <TITLE>浮动层居中的对话框效果演示</TITLE> <META http-equiv=Content-Type content="text/htm

随机推荐