This article mainly introduces the method of imitating desktop windows in JavaScript, which can imitate the opening, closing, moving, zooming, maximizing, minimizing and other functions of desktop windows. Friends who need it can refer to it, the details are as follows:
JS is used here to simulate the movement of the desktop window, zooming, minimizing, maximizing and closing in eight directions, as well as the preview effect of double-clicking to shrink and enlarge the window, and changing the window size.
<!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, p { 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 p { 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; } /*改变大小时的预览p*/ #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 dragp = 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("p"); 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 = ""; } }; //显示预览p 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"; } } //隐藏预览p 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("p"); 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(); dragp(); boxButton(); }; </script> </head> <body> <p id="box"> <p id="boxHeader"> <p id="button"> <p class="minimize"></p> <p class="maximize"></p> <p class="close"></p> </p> </p> <p id="boxSide"> <p id="boxN"></p> <p id="boxE"></p> <p id="boxS"></p> <p id="boxW"></p> <p id="boxNE"></p> <p id="boxES"></p> <p id="boxSW"></p> <p id="boxWN"></p> </p> </p> <button id="showButton"><span>>_<</span><p>居然关掉人家,讨厌~</p><p>快打开</p></button> <p id="virtualBox"></p> </body> </html>
The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!
