仅使用CSS向下滚动时,如何使导航栏固定在顶部并变得透明?

时间:2019-07-20 16:28:48

标签: html css

我是html / css的新手,我想仅使用HTML / CSS来使导航栏始终保持在顶部,而向下滚动则变为透明。这是可能的?现在,这让我发疯,我无法在导航栏下创建背景图像。我应该在哪里更改CSS部分? 这就是我期望成为的
 [链接] https://frankyeah.github.io/Front-Enter/index.html

我要做的是以下

HTML:

<nav>
     <div>
         <a href="" class="logo"><img src="./image/FE_logo-4.png"></a>
         Logo
     </div>

     <div class="navlist">
     <a href="">explore</a>
     <a href="">Skill</a>
     <a href="">login</a>
     </div>

     <div>
     <a href="" class="search"><img src="./image/FE_search_green.png"> 
     </a> 
     </div>
</nav>

<main>
     <div class="mainview"></div>
     <div class="mainview2"></div>
     <div></div>
     <div></div>
     <div></div>
</main>

CSS:


 <style>
        nav{
            display: flex;
            justify-content: space-between;
            flex-flow:row nowrap;
            position: fixed;
            width:100%;
            background-color: white;
            opacity:0.7;
            z-index:999 ;
            margin: 0 auto;
            padding: 0;
        }
        .logo>img{
            display: flex;
            flex-flow:row nowrap;
            width:50%;
            margin-top:5%;
            margin-left:20%;
        }
        .navlist{
            display: flex;
            flex-flow:row nowrap;
            width:40%;
            justify-content: space-evenly;
            margin-top:2.5%;
            margin-left:40%;
            font-family:"arial";
        }
        a:hover{
            color:  #66FFFF;
        }
        div a{
            text-decoration: none;
            color:#AAAAAA;
        }
        .search>img {
            display: flex;
            flex-flow:row nowrap;
            margin:30px 50px;
            width:20%;
        }
        .mainview{
    overflow: hidden;
    background-image:url("../image/key-visual.jpg");
    background-repeat:no-repeat;
    background-attachment:scroll; 
    background-size: cover;
    widows: 100%;
    height: 100%;
    padding-top: 50%; 
}
.mainview2{
    background-image:url("../image/second-img.jpg");
    background-attachment:scroll; 
    background-position:top left;
    background-repeat:no-repeat;
    background-size: cover;
    widows: 100%;
    height: 100%;
    padding-top: 50%; 
    margin: 0 0;
}
    </style>

我希望背景图片会在导航栏下方,并且向下滚动导航栏时将显示透明图像,但是出了点问题。

2 个答案:

答案 0 :(得分:0)

我认为您需要了解视差滚动效果。您可以从以下链接中学习:https://www.w3schools.com/howto/howto_css_parallax.asp

  <style>
.parallax { 
  /* The image used */
  background-image: url("img_parallax.jpg");

  /* Set a specific height */
  height: 500px; 

  /* Create the parallax scrolling effect */
  background-attachment: fixed;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}
</style>

<!-- Container element -->
<div class="parallax"></div>

可以使用css和javascript根据滚动位置更改导航栏的透明度。我已经修改了您的代码,您可以在以下链接中看到它:https://jsfiddle.net/SyamKumarD/r37n6cp8/20/

希望这会有所帮助

答案 1 :(得分:0)

您无法检测是否仅使用CSS进行滚动。您必须使用JavaScript / jQuery。

但是在您的网站示例中,标题看起来始终是透明的,并且仅停留在页面顶部的白色div上方。

尝试将div添加到页面顶部,并将其高度设置为与网站类似的导航栏高度。