前端BOM操作常用命令詳解及代碼案例
前言
BOM(Browser Object Model)是瀏覽器對(duì)象模型,是瀏覽器提供的JavaScript操作瀏覽器的API。BOM提供了與網(wǎng)頁(yè)無(wú)關(guān)的瀏覽器的功能對(duì)象,雖然沒(méi)有正式的標(biāo)準(zhǔn),但現(xiàn)代瀏覽器已經(jīng)幾乎實(shí)現(xiàn)了JavaScript交互性方面的相同方法和屬性,因此常被認(rèn)為是BOM的方法和屬性。本文將詳細(xì)介紹BOM操作中的常用命令,并通過(guò)代碼案例進(jìn)行解釋。
1. 獲取瀏覽器窗口尺寸
- 獲取可視窗口寬度:
window.innerWidth - 獲取可視窗口高度:
window.innerHeight
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var m1 = window.innerWidth;
var m2 = window.innerHeight;
console.log(m1);
console.log(m2);
</script>
</body>
</html>
2. 瀏覽器的彈出層
- 提示框:
window.alert('提示信息') - 詢(xún)問(wèn)框:
window.confirm('提示信息') - 輸入框:
window.prompt('提示信息', '默認(rèn)值')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// window.alert('你好!')
// var res = window.confirm('你好嗎?')
// console.log(res)
var res2 = window.prompt('你是哪個(gè)省的?');
console.log(res2);
</script>
</body>
</html>
3. 開(kāi)啟和關(guān)閉標(biāo)簽頁(yè)
- 開(kāi)啟:
window.open('地址') - 關(guān)閉:
window.close()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button id="on">開(kāi)啟</button>
<button id="off">關(guān)閉</button>
<script>
var on = document.getElementById('on');
var off = document.getElementById('off');
on.onclick = function() {
window.open('https://www.baidu.com/');
}
off.onclick = function() {
window.close();
}
</script>
</body>
</html>
4. 瀏覽器常見(jiàn)事件
- 資源加載完畢:
window.onload = function() {} - 可視尺寸改變:
window.onresize = function() {} - 滾動(dòng)條位置改變:
window.onscroll = function() {}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<img src="圖片鏈接" alt="">
<script>
window.onload = function() {
console.log('資源已經(jīng)加載完畢');
}
window.onresize = function() {
console.log('可視尺寸改變');
}
window.onscroll = function() {
console.log('滾動(dòng)條位置改變');
}
</script>
</body>
</html>
5. 瀏覽器的歷史記錄操作
- 回退頁(yè)面:
window.history.back() - 前進(jìn)頁(yè)面:
window.history.forward() - 跳轉(zhuǎn)到指定頁(yè)面:
window.history.go(n),其中n可以是負(fù)數(shù)(表示后退)或正數(shù)(表示前進(jìn))
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button onclick="goBack()">回退</button>
<button onclick="goForward()">前進(jìn)</button>
<button onclick="goToPage(-2)">回退兩頁(yè)</button>
<script>
function goBack() {
window.history.back();
}
function goForward() {
window.history.forward();
}
function goToPage(n) {
window.history.go(n);
}
</script>
</body>
</html>
6. 瀏覽器卷去的尺寸和滾動(dòng)
- 卷去的高度:
document.documentElement.scrollTop或window.scrollY - 卷去的寬度:
document.documentElement.scrollLeft或window.scrollX - 滾動(dòng)到指定位置:
window.scrollTo(left, top)或window.scrollTo({left: xx, top: yy, behavior: 'smooth'})
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
width: auto;
height: 3000px;
}
button {
position: fixed;
bottom: 50px;
right: 50px;
}
</style>
</head>
<body>
<button id="go">傳送</button>
<script>
var go = document.getElementById('go');
go.onclick = function() {
window.scrollTo({left: 300, top: 400, behavior: "smooth"});
}
</script>
</body>
</html>
7. Navigator對(duì)象
Navigator對(duì)象包含有關(guān)瀏覽器的信息。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
console.log('瀏覽器品牌', navigator.appName);
console.log('瀏覽器版本', navigator.appVersion);
console.log('用戶(hù)代理', navigator.userAgent);
console.log('操作系統(tǒng)', navigator.platform);
</script>
</body>
</html>總結(jié)
到此這篇關(guān)于前端BOM操作常用命令詳解及代碼案例的文章就介紹到這了,更多相關(guān)前端BOM常用操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript實(shí)現(xiàn)兩個(gè)select下拉框選項(xiàng)左移右移
這篇文章主要介紹了JavaScript實(shí)現(xiàn)兩個(gè)select下拉框選項(xiàng)左移右移功能,js實(shí)現(xiàn)下拉框元素互相移動(dòng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
JS重寫(xiě)Date函數(shù)以及兼容IOS系統(tǒng)
這篇文章主要介紹了JS重寫(xiě)Date函數(shù)以及兼容IOS系統(tǒng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
JavaScript實(shí)現(xiàn)一鍵復(fù)制文本功能的示例代碼
這篇文章主要為大家介紹兩種javascript實(shí)現(xiàn)文本復(fù)制(將文本寫(xiě)入剪貼板)的方法,文中的示例代碼講解詳細(xì),大家可以根據(jù)需求特點(diǎn)選用2023-03-03
JS執(zhí)行控制之節(jié)流模式實(shí)例分析
這篇文章主要介紹了JS執(zhí)行控制之節(jié)流模式,結(jié)合實(shí)例形式分析了節(jié)流模式的功能、原理及相關(guān)使用方法,需要的朋友可以參考下2018-12-12
JS+CSS實(shí)現(xiàn)帶小三角指引的滑動(dòng)門(mén)效果
這篇文章主要介紹了JS+CSS實(shí)現(xiàn)帶小三角指引的滑動(dòng)門(mén)效果,可實(shí)現(xiàn)帶有箭頭提示效果的滑動(dòng)門(mén)功能,涉及JavaScript動(dòng)態(tài)操作頁(yè)面元素樣式的相關(guān)技巧,需要的朋友可以參考下2015-09-09
js限制checkbox選中個(gè)數(shù)以限制六個(gè)為例
需要展示多個(gè)checkbox復(fù)選框,而只能允許最多選6個(gè),下面為大家介紹下如何使用js限制checkbox選中個(gè)數(shù),需要的朋友可以參考下2014-07-07
javascript 包裹節(jié)點(diǎn) 提高效率
模仿jQuery,創(chuàng)建幾個(gè)包裹節(jié)點(diǎn)的方法,發(fā)現(xiàn)jQuery的方法很低效啊,下一次他又可以說(shuō)這幾個(gè)方法可以提升了多少多少了。2010-02-02

