简体   繁体   English

具有固定的侧边栏滚动的视差

[英]Parallax with a fixed sidebar scrolling

I want to make a parallax where when you scroll down, it'll show you the content and the sidebar. 我想制作一个视差,当您向下滚动时,它将显示内容和侧边栏。 As you scroll further, when the side bar reaches the top, it stops there (fixed). 当您进一步滚动时,当侧边栏到达顶部时,它会在那里停止(固定)。 At the moment, my sidebar is already fixed and blocks the whole image of the parallax. 目前,我的侧边栏已经修复,并且会遮挡视差的整个图像。

Here's how it looks like. 这是它的样子。

CSS: CSS:

body, html {
    height: 100%;
}

#parallax { 
    background-image: url("https://image.jpg");
    height: 100%; 
    background-attachment: fixed;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
}

#sidebar {
    padding: 100px;
    position: fixed;
 }

HTML: HTML:

<div id="maincontent">
   <div id="sidebar">
        sidebar content here
   </div>
   <div id="content">
   </div>
</div>

I'm not sure what to change or add for the maincontent div to get everything to appear after the parallax. 我不确定为maincontent div更改或添加什么,以使所有内容都出现在视差之后。

hope this is what you are searching for:) You need jQuery for my solution: 希望这就是您要搜索的内容:)您需要jQuery作为我的解决方案:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

This example is just for getting the concept and is way overloaded. 本示例仅用于了解概念,并且方法很繁重。 I would just suggest you play with the jquery thing 我只是建议你玩jQuery的东西

#parallax { 
    background-image: url("https://tailandfur.com/wp-content/uploads/2016/03/40-Big-Dog-Puppy-Pictures-7.jpg");
    height: 100vh; 
    width:100vw;
    background-attachment: fixed;
    background-size: cover;
}

#sidebar {
    height:20%;
    width:20%;
    float:left;
    background-color: orange;
 }
.content{
    float:right;
    height:120%;
    width:80%;
}

The jQuery thing: Everytime the scroll event is fired the browser checks, if the bottom of the parallax div is reached with the top of the browser window --> if browser window top is bigger than the parallax the sidebar is fixed jQuery的问题:每次触发滚动事件时,浏览器都会检查视差div的底部是否与浏览器窗口的顶部达到->如果浏览器窗口的顶部大于视差,则侧边栏是固定的

$(document).scroll(function() {
    if($(document).scrollTop() >= $("#parallax").offset().top + $("#parallax").height()){
        $("#sidebar").css("position", "fixed").css("top", "0").css("left", "0");
    }
    else{
        $("#sidebar").css("position", "relative");
    }
});

HTML: HTML:

<div id="parallax"></div>
<div id="maincontent">
    <div id="sidebar" class="relative">sidebar content here</div>
    <div class="content" style="background-color: green;"></div>
    <div class="content" style="background-color: blue;"> </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM