添加视差会破坏我其他div的背景图像

时间:2019-12-13 15:50:15

标签: javascript html css

我有一个网站,该网站分为三个div作为页面。我已经在第一页上添加了视差效果:

header {    
    height: 100%;
    background-image: url("/home/george/NewVolume/JavaScript/ACG/img/ps4-controller-min.jpeg");
    background-attachment: fixed;
    background-position: bottom center;
    background-repeat: no-repeat;
    background-size: cover;
    text-align: center;
    scroll-snap-align: center;
}

然后,我尝试将下一个效果添加到第二个页面/ div:

$(document).on('mouseover', 'container_page_1 .column', function() {
  $(this).addClass('active').siblings().removeClass('active');
})
body {
  margin: 0;
  padding: 0;
}

.container_page_1 {
    width: 100%;
    height: 100vh;
    /* background-color: red; */
/*     scroll-snap-align: center; */
    overflow: hidden;
}

.container_page_1 .column {
    width: 25%;
    height: 100%;
    float: left;
    border-right: 2px solid rgba(0, 0, 0, 0.5);
    box-sizing: border-box;
    z-index: 1;
}

.container_page_1 .column:last-child {
    border-right: none;
}

.container_page_1 .column .content {
    position: relative;
    height: 100%;
}

.container_page_1 .column .content h1 {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 100%;
    background: rgba(0, 0, 0, .1);
    text-align: center;
    margin: 0;
    padding: 0;
    color: rgba(255, 255, 255, .2);
    font-size: 15em;
    border-top: 2px solid rgba(0, 0, 0, 0.5);
    border-bottom: 2px solid rgba(0, 0, 0, 0.5);
}

.container_page_1 .column .content .box {
    position: absolute;
    top: 50%;
    transform: translateY(100%);
    box-sizing: border-box;
    padding: 40px;
    background: rgba(255, 255, 255, 1);
    text-align: center;
    transition: 0.5s;
    opacity: 0;
}

.container_page_1 .column.active .content .box {
    opacity: 1;
    transform: translateY(-50%);
}

.container_page_1 .column .content .box h2 {
    margin: 0;
    padding: 0;
    font-size: 30px;
    color: #262626;
}

.container_page_1 .column .content .box p {
    color: #262626;
    font-size: 18px;
}

.container_page_1 .column .bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: -1;
}

.container_page_1 .column.active .bg.bg1 {
    background: url('https://i.postimg.cc/ZRYZ24Ss/ac-odyssey-min.jpg');
    background-size: cover;
    background-attachment: fixed;
    background-position: center;
    transition: 0.5s;
}
.container_page_1 .column.active .bg.bg2 {
    background: url('https://i.postimg.cc/x8h2XmbB/borderlands-3-min.jpg');
    background-size: cover;
    background-attachment: fixed;
    background-position: center;
    transition: 0.5s;
}
.container_page_1 .column.active .bg.bg3 {
    background: url('https://i.postimg.cc/1RjPCkYM/god-of-war-4-min.jpg');
    background-size: cover;
    background-attachment: fixed;
    background-position: center;
    transition: 0.5s;
}
.container_page_1 .column.active .bg.bg4 {
    background: url('https://i.postimg.cc/qqzrBwg8/sw-jedi-fallen-order-min.jpg');
    background-size: cover;
    background-attachment: fixed;
    background-position: center;
    transition: 0.5s;
}
<!DOCTYPE html>
<html lang="en">
<body>
    <div id="page2" class="container_page_1">
        <div class="column active">
            <div class="content">
                <h1>1</h1>
                <div class="box">
                    <h2>What is lorem ipsum</h2>
                    <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Inventore necessitatibus possimus fuga voluptate incidunt enim eius sed, ad suscipit error quasi ex blanditiis ipsa, at vero officiis voluptatem a modi!                                
                    </p>
                </div>
            </div>
            <div class="bg bg1"></div>
        </div>
        <div class="column">
            <div class="content">
                <h1>2</h1>
                <div class="box">
                    <h2>What is lorem ipsum</h2>
                    <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Inventore necessitatibus possimus fuga voluptate incidunt enim eius sed, ad suscipit error quasi ex blanditiis ipsa, at vero officiis voluptatem a modi!                                
                    </p>
                </div>
            </div>
            <div class="bg bg2"></div>
        </div>
        <div class="column">
            <div class="content">
                <h1>3</h1>
                <div class="box">
                    <h2>What is lorem ipsum</h2>
                    <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Inventore necessitatibus possimus fuga voluptate incidunt enim eius sed, ad suscipit error quasi ex blanditiis ipsa, at vero officiis voluptatem a modi!                                
                    </p>
                </div>
            </div>
            <div class="bg bg3"></div>
        </div>
        <div class="column">
            <div class="content">
                <h1>4</h1>
                <div class="box">
                    <h2>What is lorem ipsum</h2>
                    <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Inventore necessitatibus possimus fuga voluptate incidunt enim eius sed, ad suscipit error quasi ex blanditiis ipsa, at vero officiis voluptatem a modi!                                
                    </p>
                </div>
            </div>
            <div class="bg bg4"></div>
        </div>
    </div>
  <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script type="text/javascript">
    $(document).on('mouseover', '.container_page_1 .column', function(){
        $(this).addClass('active').siblings().removeClass('active');
    })
</script>
</body>
</html>

但事实是两种效果都“使用相同的背景属性”。两者都使用background-attachment: fixed;,在这种情况下,它们会将所有背景图像放在同一位置,彼此重叠。

这是正常的100%first page

这是the first page set at 50%

我该如何解决?

如何将header视差背景和.container_page_1 .column.active .bg.bg1背景图像分开?

0 个答案:

没有答案