css禁止滑动页面_弹出层完美禁止页面滚动

Html

Css

Js

弹出层去除背景滚动--原生版

使用js构建的弹出层类,使用new的方式实例化,传入的参数为:

{

el: "触发元素,click触发弹出层",

content: "弹出层内容",

time: 0 // 显示持续时间

}

使用 passive 事件和overflow:hidden;禁止滚动。完美兼容PC和移动端。

main content

Popup

Popup 500

body {

margin:0;

}

h2,p {

padding:0 10px;

}

.main {

height:1000px;

background:#f63;

background:-webkit-linear-gradient(top,#f8f8f8,#f63);

padding:10px;

}

.btn-popup {

padding:0 10px;

height:40px;

line-height:40px;

background:#f63;

font-weight:600;

border:none;

outline:none;

color:#333;

border-radius:4px;

cursor:pointer;

font-size:16px;

-webkit-tap-highlight-color:transparent;

}

code {

background:#ddd;

padding:2px 5px;

border-radius:2px;

color:#333;

white-space:pre-wrap;

display:inline-block;

margin:0 10px;

}

/****************** css for layer /*******************/

.layer {

position:fixed;

overflow:auto;

top:0;

left:0;

right:0;

bottom:0;

background:#666;

background:rgba(0,0,0,0.8);

z-index:100001;

display:none;

-webkit-will-change:background;

will-change:background;

}

.layer-content {

text-align:center;

color:#333;

font-size:0.9em;

padding:0 5px;

display:inline-block;

width:40%;

max-width:200px;

height:80px;

line-height:80px;

background:#fff;

position:absolute;

top:50%;

left:50%;

-webkit-transform:translate(-50%,-50%);

transform:translate(-50%,-50%);

border-radius:5px;

}

.layer-content>div {

overflow:hidden;

white-space:nowrap;

text-overflow:ellipsis;

}

.lock {

overflow:hidden;

touch-action:none;

}

.lock body {

overflow:hidden;

touch-action:none;

margin-right:17px;

}

.lock.mobile body {

margin-right:0;

}

function PopupLayer(options) {

this.eles = [];

this.times = [];

this.contents = [];

this.time = 0;

this.animation = false;

this.isMobile = navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i);

if(this.isMobile){

document.querySelector("html").classList.add("mobile");

}

// init layer

var html = '

';

this.layer = document.createElement('div');

this.layer.className = "layer J_layer";

this.layer.innerHTML = html;

document.body.appendChild(this.layer);

this.addElement = function(ele_options){

if(!ele_options.ele){

console.log("need ele option.");

return;

}

// init trigger element

document.querySelector(ele_options.ele).classList.add("J_popup");

this.times.push(ele_options.time);

this.eles.push(ele_options.ele);

this.contents.push(ele_options.content);

};

this.forbidScroll = function(e) {

e.preventDefault && e.preventDefault();

e.returnValue = false;

e.stopPropagation && e.stopPropagation();

return false;

}

var me = this;

this.toggleShow = function(){

var now = +new Date();

if(now-1){

document.querySelector(".layer-content .content").innerText = me.contents[index];

me.time = me.times[index];

break;

}

}

me.showLayer();

if(me.time){

setTimeout(function(){

me.hideLayer();

},me.time+me.trans_time);

}

} else if (e.target.classList.contains("J_layer")) {

me.hideLayer();

}

});

}

var layer = new PopupLayer();

layer.addElement({

ele: '.popup',

content: 'content of layer'

});

layer.addElement({

ele: '.popup300',

content: 'content of layer 300',

time: 300

})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以通过以下代码来实现在层中嵌入HTML页面: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>层-layui</title> <link rel="stylesheet" href="layui/css/layui.css"> </head> <body> <!-- 引入layui.js --> <script src="layui/layui.js"></script> <script> // 使用layui层 layui.use('layer', function(){ var layer = layui.layer; // 点击按钮,层显示HTML页面 layer.open({ type: 2, title: 'HTML页面', shadeClose: true, shade: 0.8, area: ['800px', '600px'], content: 'test.html' // 层中嵌入的HTML页面 }); }); </script> </body> </html> ``` 在这个代码中,我们使用了 layui 层组件,通过 `layer.open()` 方法打开一个层,并指定了层的标题、大小、遮罩等参数。最重要的是 `content` 参数,这个参数指定了要在层中嵌入的 HTML 页面,这里我指定了一个名为 `test.html` 的文件。 接下来,我们来看一下 `test.html` 的内容: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>测试页面</title> </head> <body> <h1>Hello, world!</h1> <p>这是一个测试页面,用于演示在层中嵌入HTML页面。</p> </body> </html> ``` 这个页面很简单,只有一个标题和一段文字。你可以根据自己的实际需求来编写这个页面。 最后,将上述两段代码保存为两个文件,分别为 `index.html` 和 `test.html`,并且确保 `layui` 目录下的文件已经正确引入。然后在浏览器中打开 `index.html`,你就可以看到一个层,其中嵌入了 `test.html` 页面
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值