iframe父子頁面實現(xiàn)共用滾動條的常見方法
引言
在開發(fā)過程中,有時候需要用到iframe復(fù)用不同域名下的頁面內(nèi)容。為了提供連貫的用戶體驗,經(jīng)常需要在主頁面(父頁面)和iframe子頁面之間共享滾動位置。當(dāng)用戶在父頁面中滾動時,我們希望子頁面的滾動條也能相應(yīng)地移動到相同的位置。本文將介紹其中較為常見的一種方法,即使用postMessage來實現(xiàn)父子頁面間的通信,從而實現(xiàn)滾動同步。
基本原理
1.隱藏iframe的滾動條
我們可以通過iframe屬性frameborder="0"和scrolling="no"來隱藏iframe的滾動條,從而避免嵌入iframe的頁面雙滾動條的出現(xiàn)。
<iframe src="son.html" frameborder="0" scrolling="no"></iframe>
2.使用sticky粘性定位固定子頁面
iframe設(shè)置了以上屬性之后,子頁面無法滾動,而隨著父頁面滾動,子頁面也會跟著滾上去,不是我們想要的效果。所以需要用css的sticky粘性定位固定子頁面top上升到頂部就固定。
<style>
.container{
width: 100%;
min-height: 1500px;
position: relative;
}
iframe{
position: sticky;
top: 0;
width: 100%;
height: 900px;
overflow: hidden;
}
</style>
<div class="container">
<iframe src="son.html" frameborder="0" scrolling="no"></iframe>
</div>
3.通過postMessage通信實現(xiàn)父子頁面滾動聯(lián)動
我們可以通過監(jiān)聽父頁面的滾動事件,將滾動信息傳給子頁面,子頁面通過監(jiān)聽父頁面的postMessage事件,獲取滾動信息,手動觸發(fā)滾動事件,從而實現(xiàn)滾動聯(lián)動。
- 父頁面代碼:
window.onscroll = function(){
document.querySelector('iframe').contentWindow.postMessage({'scrollTop':window.scrollY},'*');
}
- 子頁面代碼:
window.addEventListener('message', function (event) {
window.scrollTo(0,event.data.scrollTop);
});
完整代碼
- 父頁面:parent.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>parent</title>
</head>
<style>
body{
padding: 0;
margin: 0;
}
.container{
width: 100%;
min-height: 1500px;
position: relative;
}
iframe{
position: sticky;
top: 0;
width: 100%;
height: 900px;
overflow: hidden;
}
</style>
<body>
<div class="container">
<iframe src="son.html" frameborder="0" scrolling="no"></iframe>
</div>
</body>
<script>
// 監(jiān)聽message事件
window.addEventListener('message', function(event) {
document.querySelector('.container').style.height = event.data.message + 'px';
document.querySelector('iframe').style.height = window.screen.availHeight + 'px';
},false);
window.onscroll = function(){
document.querySelector('iframe').contentWindow.postMessage({'scrollTop':window.scrollY},'*');
}
</script>
</html>
- 子頁面:son.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>son</title>
</head>
<style>
body{
margin: 0;
padding: 0;
}
div{
width: 100%;
height: 30vw;
border-bottom:#000 solid 1px;
background: #f5f5f5;
line-height: 30vw;
text-align: center;
}
</style>
<body>
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
<div>10</div>
<div>11</div>
<div>12</div>
</body>
<script>
function sendMessage(){
window.parent.postMessage({
sendHeight: true,
message: document.body.scrollHeight,
}, '*')
}
sendMessage();
window.addEventListener('message', function (event) {
window.scrollTo(0,event.data.scrollTop);
sendMessage();
});
</script>
</html>
到此這篇關(guān)于iframe父子頁面實現(xiàn)共用滾動條的常見方法的文章就介紹到這了,更多相關(guān)iframe父子頁面滾動條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
layui問題之渲染數(shù)據(jù)表格時,僅出現(xiàn)10條數(shù)據(jù)的解決方法
今天小編就為大家分享一篇layui問題之渲染數(shù)據(jù)表格時,僅出現(xiàn)10條數(shù)據(jù)的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
javascript中數(shù)組(Array)對象和字符串(String)對象的常用方法總結(jié)
這篇文章主要介紹了javascript中數(shù)組(Array)對象和字符串(String)對象的常用方法,結(jié)合實例形式總結(jié)分析了javascript中關(guān)于數(shù)組和字符串的常用函數(shù)與使用技巧,需要的朋友可以參考下2016-12-12
HTML+CSS+JavaScript創(chuàng)建一個簡單的井字游戲
使用javascript創(chuàng)建游戲是最有趣的學(xué)習(xí)方式。它會讓你保持動力,這對于學(xué)習(xí)?Web?開發(fā)等復(fù)雜技能至關(guān)重要。本文將使用HTML、CSS和?Javascript創(chuàng)建一個井字游戲。感興趣的童鞋可以關(guān)注一下2021-11-11
JavaScript數(shù)組去重常見四種方法(最新整理)
數(shù)組去重是JavaScript開發(fā)中常見的需求,本文將詳細(xì)解析四種常用的數(shù)組去重方法及其優(yōu)缺點,感興趣的朋友一起看看吧2025-07-07
JavaScript獲取function所有參數(shù)名的方法
本文使用javascript獲取function所有參數(shù)名的方法,對js獲取function所有參數(shù)名感興趣的朋友一起學(xué)習(xí)吧2015-10-10
layui form表單提交之后重新加載數(shù)據(jù)表格的方法
今天小編就為大家分享一篇layui form表單提交之后重新加載數(shù)據(jù)表格的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09

