JavaScript實(shí)現(xiàn)模仿桌面窗口的方法
本文實(shí)例講述了JavaScript實(shí)現(xiàn)模仿桌面窗口的方法。分享給大家供大家參考。具體如下:
這里使用JS模仿了桌面窗口的移動(dòng)、八個(gè)方向的縮放、最小化、最大化和關(guān)閉,以及 雙擊縮小放大窗口、改變窗口大小的預(yù)覽效果等功能。
<!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;
}
/*標(biāo)題欄*/
#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;
}
/*八個(gè)方向*/
/*用于顯示變欄顏色,作為測(cè)試
#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;
}
/*改變大小時(shí)的預(yù)覽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函數(shù)
var dragDiv = function() {
var box = document.getElementById("box");
var boxHeader = document.getElementById("boxHeader");
var bDraging = false;
var disX = disY = 0;
//記錄鼠標(biāo)按下時(shí)距離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;
//拖動(dòng)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是否正在改變 & 鼠標(biāo)是否正在按下
var originalWidth = box.offsetWidth;
//box最原始寬度
var originalHeight = box.offsetHeight;
//box最原始高度
for(var i = 0; i < boxSide.length; i++) {
//遍歷boxside,監(jiān)聽(tīng)鼠標(biāo)
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上一次的狀態(tài)
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;
};
}
//改變鼠標(biāo)指針樣式
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 = "";
}
};
//顯示預(yù)覽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";
}
}
//隱藏預(yù)覽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;
//八個(gè)方向,分別為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;
}
}
};
//窗口按鈕函數(shù)
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;
//目前狀態(tài)是否最小 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>居然關(guān)掉人家,討厭~</p><p>快打開(kāi)</p></button>
<div id="virtualBox"></div>
</body>
</html>
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
- JavaScript實(shí)現(xiàn)的浮動(dòng)層框架用法實(shí)例分析
- JavaScript實(shí)現(xiàn)的圓形浮動(dòng)標(biāo)簽云效果實(shí)例
- JavaScript點(diǎn)擊按鈕后彈出透明浮動(dòng)層的方法
- javascript實(shí)現(xiàn)div浮動(dòng)在網(wǎng)頁(yè)最頂上并帶關(guān)閉按鈕效果實(shí)例
- JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)上的浮動(dòng)廣告的簡(jiǎn)單方法
- 如何制作浮動(dòng)廣告 JavaScript制作浮動(dòng)廣告代碼
- JavaScript 浮動(dòng)定位提示效果實(shí)現(xiàn)代碼
- javascript 表格排序和表頭浮動(dòng)效果(擴(kuò)展SortTable)
- javascript彈出拖動(dòng)窗口
- JavaScript實(shí)現(xiàn)上下浮動(dòng)的窗口效果代碼
相關(guān)文章
layui 監(jiān)聽(tīng)彈窗關(guān)閉并刷新父級(jí)table的場(chǎng)景分析
這篇文章主要介紹了layui 監(jiān)聽(tīng)彈窗關(guān)閉并刷新父級(jí)table的場(chǎng)景分析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-07-07
Some tips of wmi scripting in jscript (1)
Some tips of wmi scripting in jscript (1)...2007-04-04
兼容IE和Firefox火狐的上下、左右循環(huán)無(wú)間斷滾動(dòng)JS代碼
html里的marqueen也能實(shí)現(xiàn)內(nèi)容的滾動(dòng),但滾動(dòng)是間斷的,運(yùn)用JavaScript可以使這一問(wèn)題得到改觀,實(shí)現(xiàn)無(wú)間斷的滾動(dòng),讓頁(yè)面看起來(lái)更美觀2013-04-04
微信小程序列表時(shí)間戳轉(zhuǎn)換實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了微信小程序列表時(shí)間戳轉(zhuǎn)換實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
bootstrap table實(shí)現(xiàn)橫向合并與縱向合并
這篇文章主要為大家詳細(xì)介紹了bootstrap table實(shí)現(xiàn)橫向合并與縱向合并,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
JS回調(diào)函數(shù) callback的理解與使用案例分析
這篇文章主要介紹了JS回調(diào)函數(shù) callback的理解與使用,結(jié)合具體案例形式分析了javascript回調(diào)函數(shù)的功能、原理、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-09-09
js 事件對(duì)象 鼠標(biāo)滾輪效果演示說(shuō)明
第三篇,繼續(xù)總結(jié)事件對(duì)象 先看看監(jiān)聽(tīng)鼠標(biāo)滾輪事件能幫我們做什么2010-05-05
JS仿淘寶實(shí)現(xiàn)的簡(jiǎn)單滑動(dòng)門效果代碼
這篇文章主要介紹了JS仿淘寶實(shí)現(xiàn)的簡(jiǎn)單滑動(dòng)門效果,可實(shí)現(xiàn)按字母分類滑動(dòng)切換的功能,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-10-10
JavaScript實(shí)現(xiàn)通過(guò)滑塊改變網(wǎng)頁(yè)顏色
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)通過(guò)滑塊改變網(wǎng)頁(yè)顏色,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08

