js怎样控制遮罩层不随背景移动而移动

当我鼠标滑动的时候,遮罩层也会跟着背景的滚动而滚动,但是我用fixed把页面定住了后,背景也也会跟着定住,怎样单独控制遮罩层页面?

阅读 3.6k
3 个回答

用fix定位遮罩层

.遮罩{
    position: fixed;
    top: 0;
    left: 0;
}

这样是不是?

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style type="text/css">
        .one {
            background: red;
            height: 500px;
        }
        .two {
            background: blue;
            height: 500px;
        }
        .overlay {
            position: fixed;
            background: #ccc;
            opacity: 0.8;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
<div class="one"></div>
<div class="two"></div>
<div class="overlay"></div>
</body>
</html>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题