一、BOM打开,关闭窗口
window.open(); window.close();
<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <script> window.onload = function() { var oBtn = document.getElementById('btn1'); var oBtn2 = document.getElementById('btn2'); oBtn.onclick = function(){ var oNewWin = window.open('about:blank'); oNewWin.document.write(123); }; oBtn2.onclick = function(){ window.close(); }; }; </script> </head> <body style="height:2000px;"> <button id="btn1">open</button><button id="btn2">close</button> </body> </html>
二、BOM常用属性
window.navigator.userAgent; window.loaction;
三、BOM侧边栏随窗口滚动(广告浮窗)
可视区高/宽度: document.documentElement.clientHeight/clientWidth;
滚动条滚动距离: document.documentElement.scrollTop || document.body.scrollTop;
<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <style> #div1{ width:100px; height:100px; background:red; position:absolute; right:0; top:0;} </style> <script> window.onload = window.onresize = window.onscroll = function(){ var oDiv = document.getElementById('div1'); var iScrollTop = document.body.scrollTop || document.documentElement.scrollTop; //获取滚动条距离顶部高度 var iH = document.documentElement.clientHeight; //获取可视区高度 oDiv.style.top = iScrollTop + (iH - oDiv.offsetHeight) / 2 + 'px'; }; </script> </head> <body style="height:2000px;"> <div id="div1"></div> </body> </html>
四、BOM回到顶部
<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> <style> button{ position:fixed; right:0; bottom:0;} </style> <script> window.onload = function(){ var oBtn = document.getElementById('btn1'); var timer = null; var bSys = true; window.onscroll = function(){ if(!bSys){ clearInterval(timer); } bSys = false; }; oBtn.onclick = function(){ timer = setInterval(function(){ var scrollTop = document.body.scrollTop || document.documentElement.scrollTop; var iSpeed = Math.floor((-scrollTop / 8)); if(scrollTop == 0){ clearInterval(timer); } bSys = true; document.documentElement.scrollTop = document.body.scrollTop = scrollTop + iSpeed; }, 30); }; }; </script> </head> <body style="height:2000px;"> <button id="btn1">回到顶部</button> </body> </html>