🍹 浏览器对象模型是用于与浏览器进行交互的接口,包括操作浏览器窗口、导航等功能,提供了对浏览器环境的访问。
1 BOM
- BOM
- 浏览器对象模型,即Browser Object Model(BOM),尚无正式标准。
- BOM可通过JavaScript来操作浏览器,提供了一组对象用来完成操作。
- Screen:用于获取用户显示器的相关信息。
- History:操作历史记录,由于隐私只能前后翻页且仅当次访问有效。
- Window:代表的是整个浏览器的窗口,同时也是网页中的全局对象。
- Location:获取当前浏览器的地址栏信息,或者操作浏览器跳转页面。
- Navigator:代表当前浏览器信息,通过该对象可以识别不同的浏览器。
- 这些BOM对象在浏览器中作为Window对象的属性保存,可直接使用或通过Window对象使用。
2 Screen
- Screen
- Screen包含客户端显示屏幕的信息,所有浏览器都支持。
- 每个Window对象的screen属性都引用了一个Screen对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>Screen</title> </head>
<body style="text-align: center"><br> <script> console.log(screen.colorDepth); console.log(screen.pixelDepth);
console.log(screen.height); console.log(screen.width); console.log(screen.availWidth); console.log(screen.availHeight);
console.log(screen.bufferDepth);
console.log(screen.deviceXDPI); console.log(screen.deviceYDPI);
console.log(screen.updateInterval);
console.log(screen.logicalXDPI); console.log(screen.logicalYDPI);
console.log(screen.fontSmoothingEnabled); </script> </body>
</html>
|
3 History
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>History</title> </head>
<body style="text-align: center"><br> <script> console.log(history.length); console.log(history);
history.forward();
history.go(-2);
history.back(); </script> </body>
</html>
|
4 Window
- Window
- JavaScript有三种类型弹出框:警告框、确认框、提示框。
- 警告框:确保信息传递给用户,通常会使用警告框。
- 确认框:希望用户验证或接受某事务,使用确认框。
- 提示框:希望用户进入页面前输入值,使用提示框。
- 定时事件
- setTimeout():指延迟后执行一次操作。
- setInterval():用来定期重复执行操作。
4-1 警告框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>警告框</title> </head>
<body style="text-align: center"> <script> window.alert("我是一个警告框!"); </script> </body>
</html>
|
4-2 确认框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>确认框</title> </head>
<body style="text-align: center"> <script> window.confirm("我是一个确认框!"); var x = confirm("请点击一下按钮!"); if (x == true) { y = "点击了确认按钮!"; } else { y = "点击了取消按钮!"; } console.log(y); </script> </body>
</html>
|
4-3 提示框
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>提示框</title> </head>
<body style="text-align: center"> <script> window.prompt("我是一个提示框!"); var person = confirm("请输入您姓名!", "史迪仔"); if (person != null) { console.log(person); } </script> </body>
</html>
|
4-4 延时器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>延时器</title> </head>
<body style="text-align: center"> <button id="btn">按钮</button> <script> var btn = document.getElementById("btn");
btn.onclick = function () { var timer = setTimeout(function () { console.log("Hi~"); }, 3000); }
</script> </body>
</html>
|
4-5 定时器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>定时器</title> </head>
<body style="text-align: center"> <button id="btn">按钮</button> <script> var btn = document.getElementById("btn");
btn.onclick = function () { var timer = setInterval(function () { console.log("Hi~"); }, 3000); }
</script> </body>
</html>
|
4-6 窗口属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>窗口属性</title> </head>
<body style="text-align: center"> <script> var w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
var h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
console.log(w); console.log(h); </script> </body>
</html>
|
4-7 窗口方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>窗口方法</title> </head>
<body style="text-align: center"><br> <button onclick="openWin()">打开窗口</button><br><br> <button onclick="moveWin()">移动窗口</button><br><br> <button onclick="resizeWin()">调整窗口</button><br><br> <button onclick="closeWin()">关闭窗口</button> <script> function openWin() { myWindow = window.open("", "", "width=200, height=100"); myWindow.document.write("<p>新建窗口</p>"); } function moveWin() { myWindow.moveTo(300, 300); myWindow.focus(); } function resizeWin() { myWindow.resizeTo(600, 600); myWindow.focus(); } function closeWin() { myWindow.close(); } </script> </body>
</html>
|
5 Location
- Location
- Location对象中封装了浏览器的地址栏信息。
- 直接打印Location,则可获取到地址栏信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>Location</title> </head>
<body style="text-align: center"><br> <script> console.log(location.origin); console.log(location.protocol); console.log(location.hostname); console.log(location.host); console.log(location.port); console.log(location.search); console.log(location.pathname); console.log(location.href); console.log(location);
</script> </body>
</html>
|
6 Navigator
- Navigator
- Navigator代表当前浏览器的信息,通过该对象可用来识别不同的浏览器。
- 目前一般只使用userAgent判断浏览器信息,不同浏览器的userAgent不同。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8" /> <title>Navigator</title> </head>
<body style="text-align: center"><br> <script> var ua = navigator.userAgent; if (/firefox/i.test(ua)) { alert("火狐浏览器!"); } else if (/chrome/i.test(ua)) { alert("谷歌浏览器!"); } else if (/msie/i.test(ua)) { alert("IE5-IE10浏览器!"); } else if ("ActiveXObject" in window) { alert("IE11浏览器!"); } </script> </body>
</html>
|
7 小汽车动画
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| <!DOCTYPE html> <html>
<head> <meta charset="UTF-8"> <title>小汽车动画</title> <style> .controller { width: 600px; height: 50px; line-height: 50px; margin: 0 auto; text-align: center; }
.controller button { outline: none; border: none; margin: 0px; padding: 0px; width: 200px; height: 50px; font-size: 16px; line-height: 50px; text-align: center; background-color: #E9E9E9; cursor: pointer; float: left; -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; border: 2px solid #F0F0F0; }
.controller button:hover { background-color: #F9F9F9; }
.road { width: 100%; height: 100px; position: relative; margin-top: 50px; background: lemonchiffon; opacity: .90; }
.road800 { width: 800px; height: 100px; background: greenyellow; position: absolute; top: 0px; left: 0px; z-index: 1000; opacity: .75; }
.road1200 { width: 1200px; height: 100px; background: lavender; position: absolute; top: 0px; left: 0px; z-index: 500; }
div#car { width: 135px; height: 100px; display: block; position: absolute; top: 0px; left: 0px; z-index: 1500; background-image: url("./file/car.png"); background-repeat: no-repeat; background-size: contain; background-position: center; }
div#car.reverse { transform: scaleX(-1); } </style> </head>
<body style="text-align: center"><br><br> <div class="controller"> <button id="btn1">移动到800PX</button> <button id="btn2">移动到1200PX</button> <button id="btn3">回家啦!</button> </div>
<div class="road"> <div class="road800"></div> <div class="road1200"></div> <div id="car"></div> </div>
<script> document.getElementById("btn1").onclick = function () { move(document.getElementById("car"), 800); };
document.getElementById("btn2").onclick = function () { move(document.getElementById("car"), 1200); };
document.getElementById("btn3").onclick = function () { move(document.getElementById("car"), 0); };
function move(element, target) { clearInterval(element.timeId); element.timeId = setInterval(function () { var current = element.offsetLeft; var step = 10; step = current < target ? step : -step; current += step; if (Math.abs(target - current) > Math.abs(step)) { element.style.left = current + "px"; } else { clearInterval(element.timeId); element.style.left = target + "px"; } if (target < element.offsetLeft) { element.classList.add("reverse"); } else { element.classList.remove("reverse"); } }, 20); } </script> </body>
</html>
|
8 网页图片飞
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| R = 0; x1 = 0.1; y1 = 0.05; x2 = 0.25; y2 = 0.24; x3 = 1.6; y3 = 0.24; x4 = 300; y4 = 200; x5 = 300; y5 = 200;
var DI = document.getElementsByTagName("img"); var DIL = DI.length;
function A() { for (i = 0; i < DIL; i++) { var DIS = DI[i].style; DIS.position = "absolute"; DIS.left = Math.sin(R * x1 + i * x2 + x3) * x4 + x5 + "px"; DIS.top = Math.cos(R * y1 + i * y2 + y3) * y4 + y5 + "px"; } R++; }
var tag = setInterval("A()", 5);
document.onmousedown = function () { clearInterval(tag); for (i = 0; i < DIL; i++) { DI[i].style.position = "static"; } };
void (0);
|