什么是滚动穿透:弹出层滚动时,弹出层下的body跟着滚动
解决方法:
方法一 : position:fixed
1.打开模态框时,给body加上类名preventScroll; -
2.关闭模态框时,移除body上的preventScroll类名。
问题 ! 当关闭弹出框 , body 的滚动位置会丢失还原为0,也就是body 的ScrollTop 属性值会变为 0
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>滚动穿透</title>
<style>
* {
padding: 0;
margin: 0;
}
html,
body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
box-sizing: border-box;
}
.box {
width: 100%;
height: 100%;
}
p:nth-child(even) {
width: 100%;
height: 90px;
background: pink;
}
p:nth-child(odd) {
width: 100%;
height: 90px;
background: skyblue;
}
.module {
position: fixed;
top: 0;
width: 100%;
height: 100%;
z-index: 1;
background: rgba(0, 0, 0, .3);
display: none;
overflow-y: auto;
}
.module p:nth-child(even) {
width: 80%;
height: 90px;
background: red;
opacity: 0.3;
}
.module p:nth-child(odd) {
width: 80%;
height: 90px;
background: yellow;
opacity: 0.3;
}
.close {
width: 5%;
height: 100%;
background: rgba(51, 255, 0, 0.3);
display: flex;
align-items: center;
position: absolute;
top: 0;
right: 0;
}
/* 给 overflow:scroll 的元素加上一个 class (就是body) */
.preventScroll {
position: fixed;
height: 100%;
}
</style>
</head>
<body>
<div class="box">
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
<p>9</p>
<p>0</p>
</div>
<div class="module">
<div class="close">关闭</div>
</div>
<script src="./jquery-1.8.3.min.js"></script>
<script>
$('p').click(function () {
$('.module').show()
$('body').addClass('preventScroll')
})
// 移动端不支持双击事件
$('.close').on('click', function () {
$('.module').hide()
$('body').removeClass('preventScroll')
})
</script>
</body>
</html>
出现问题,弹出框出现,虽然解决滚动穿透,但是出现新的问题,body页面滚动位置为0
方法二: position : fixed + scrollTop
添加 .preventScroll 类之后会使body的滚动位置丢失 , 那么在显示弹框之前,把滚动位置保存下来,退出弹框之后设置回去
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>滚动穿透</title>
<style>
* {
padding: 0;
margin: 0;
}
html,
body {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
box-sizing: border-box;
}
.box {
width: 100%;
height: 100%;
}
p:nth-child(even) {
width: 100%;
height: 90px;
background: pink;
}
p:nth-child(odd) {
width: 100%;
height: 90px;
background: skyblue;
}
.module {
position: fixed;
top: 0;
width: 100%;
height: 100%;
z-index: 1;
background: rgba(0, 0, 0, .3);
display: none;
overflow-y: auto;
}
.module p:nth-child(even) {
width: 80%;
height: 90px;
background: red;
opacity: 0.3;
}
.module p:nth-child(odd) {
width: 80%;
height: 90px;
background: yellow;
opacity: 0.3;
}
.close {
width: 5%;
height: 100%;
background: rgba(51, 255, 0, 0.3);
display: flex;
align-items: center;
position: absolute;
top: 0;
right: 0;
}
/* 给 overflow:scroll 的元素加上一个 class (就是body) */
.preventScroll {
position: fixed;
height: 100%;
}
</style>
</head>
<body>
<div class="box">
<p>1</p>
<p>2</p>
<p>3</p>
<p>4</p>
<p>5</p>
<p>6</p>
<p>7</p>
<p>8</p>
<p>9</p>
<p>0</p>
</div>
<div class="module">
<div class="close">关闭</div>
</div>
<script src="./jquery-1.8.3.min.js"></script>
<script>
$('p').click(function () {
fixedBody()
$('.module').show()
$('body').addClass('preventScroll')
})
// 移动端不支持双击事件
$('.close').on('click', function () {
$('.module').hide()
$('body').removeClass('preventScroll')
looseBody()
})
//打开模态框前调用
function fixedBody() {
var scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
document.body.style.cssText += 'position:fixed;top:-' + scrollTop + 'px;';
}
//关闭模态框后调用
function looseBody() {
var body = document.body;
body.style.position = 'static';
var top = body.style.top;
document.body.scrollTop = document.documentElement.scrollTop = -parseInt(top);
body.style.top = '';
}
</script>
</body>
</html>