JSFiddle - React, Tailwind, and code Playground
HTML
<button class="show-big-img" data-h="300">查看大图 h300</button>
<button class="show-big-img" data-h="3000">查看大图 h3000</button>
<div class="layer-shade"></div>
<div class="layer-wrap">
<a href="javascript:;" class="layer-close">×</a>
<div class="layer-content">
<p class="show-origin-img">
<a href="javascript:;">查看原图</a>
</p>
<div class="big-img">
<div class="big-img__item">我是图片</div>
</div>
</div>
</div>
SCSS
body {
&.layer-scroll-in-body {
overflow: hidden;
}
}
.layer-shade {
display: none;
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
background-color: #000;
opacity: .15;
&.visible {
display: block;
}
}
@keyframes bounceIn {
to {
opacity: 1;
transform: scale(1);
}
}
.layer-wrap {
display: none;
z-index: 1;
position: fixed;
width: 70%;
height: 50%;
top: 100px;
left: 100px;
background-color: #000;
border-radius: 3px;
opacity: 0;
transform: scale(.3);
&.visible {
display: block;
animation: bounceIn .5s;
animation-fill-mode: both;
}
&.layer-scroll-in-body {
position: absolute;
height: auto;
}
&__wrapper {
overflow: auto;
position: fixed;
top: 0;
right: 0;
width: 100%;
height: 100%;
}
}
.layer-close {
position: absolute;
top: -16px;
right: -12px;
width: 24px;
height: 24px;
text-decoration: none;
color: #fff;
background: #999;
border-radius: 50%;
border: 3px solid #fff;
text-align: center;
line-height: 22px;
font-size: 22px;
&:hover {
background-color: #358eea;
}
}
.layer-content {
height: 100%;
overflow: auto;
}
.show-origin-img {
position: absolute;
top: 5px;
right: 20px;
font-size: 12px;
a {
color: #fff;
text-decoration: none;
&:hover {
text-decoration: underline;
}
}
}
.big-img {
display: flex;
justify-content: center;
box-sizing: border-box;
padding: 20px;
&__item {
display: flex;
justify-content: center;
align-items: center;
max-width: 100%;
width: 300px;
height: 3000px;
background-color: #c7bdb3;
}
}
JavaScript
// 显示弹窗
function showLayer(onShow, onClose) {
var $body = $('body'),
$layerShade = $('.layer-shade'),
$layerWrap = $('.layer-wrap');
// 查看大图
$('.show-big-img').click(function() {
$layerWrap.find('.big-img__item').css('height', $(this).attr('data-h'));
// 显示前处理
if (onShow && typeof onShow === 'function') {
onShow($body, $layerWrap);
}
$layerShade.addClass('visible');
$layerWrap.addClass('visible');
});
$('.layer-close').click(function() {
// 关闭前处理
if (onClose && typeof onClose === 'function') {
onClose($body, $layerWrap);
}
$layerShade.removeClass('visible');
$layerWrap.removeClass('visible');
});
}
// 显示弹窗,并设置弹窗内容滚动区为body区
function showLayerScrollInBody(setPageScroll) {
// 模拟:确保显示弹窗前页面由垂直方向滚动条
setPageScroll && $('.show-big-img').css('margin-bottom', 2000);
showLayer(function($body, $layer) {
// body设置 overflow: hidden
$body.addClass('layer-scroll-in-body');
$layer
.addClass('layer-scroll-in-body')
// 弹出层增加一层父级
.wrap('<div class="layer-wrap__wrapper">');
}, function($body, $layer) {
// 关闭弹窗,则恢复
$body.removeClass('layer-scroll-in-body');
$layer
.removeClass('layer-scroll-in-body')
.unwrap();
});
}
showLayer();
/* showLayerScrollInBody(); */
/* showLayerScrollInBody(true); */