代码之家  ›  专栏  ›  技术社区  ›  Haox

如何不透明的背景标题与滚动

  •  1
  • Haox  · 技术社区  · 6 年前

    我期待着不透明的背景颜色,我的导航栏在滚动。

    事实上,当我滚动我的导航栏是混合的内容,我不能阅读任何东西。

    我试过很多教程,但是我的javascript知识很差,什么都不管用。

    我只想背景标题是不透明0时,我们在页面的顶部,并成为0.7时,我们滚动。

    /*sticky_navbar*/
    
    window.onscroll = function() {
      myFunction()
    };
    
    var navbar = document.getElementById("header");
    var sticky = navbar.offsetTop;
    
    function myFunction() {
      if (window.pageYOffset >= sticky) {
        navbar.classList.add("sticky")
      } else {
        navbar.classList.remove("sticky");
      }
    }
    
    
    $(window).scroll(function() {
      var threshold = 200; // number of pixels before bottom of page that you want to start fading
      var op = (($(document).height() - $(window).height()) - $(window).scrollTop()) / threshold;
      if (op <= 0) {
        $("#header").hide();
      } else {
        $("#header").show();
      }
      $("#header").css("opacity", op);
    });
    #header {
      display: flex;
      justify-content: flex-end;
      background: rgba(139, 139, 157, 0.7);
      z-index: 2;
    }
    
    .navbar {}
    
    #Title {
      margin: 0 auto 0 0;
      height: 20px;
      margin-top: 15px;
      padding-left: 10px;
      border-bottom: 1px solid white;
      padding-top: 5px;
      padding-bottom: 30px;
      flex: 1;
    }
    
    #navbar {
      overflow: hidden;
      display: flex;
      justify-content: flex-end;
      border-bottom: 5px solid white;
      padding-bottom: 10px;
      padding-top: 15px;
    }
    
    .menu:nth-child(1) {
      order: 1;
    }
    
    .menu:nth-child(2) {
      order: 4;
    }
    
    .menu:nth-child(3) {
      order: 3;
    }
    
    .menu:nth-child(4) {
      order: 2;
    }
    
    .menu:nth-child(5) {
      order: 5;
    }
    
    IMG.background {
      display: block;
      margin-left: auto;
      margin-right: auto;
      z-index: 1;
      width: 60%;
    }
    
    #navbar a {
      display: block;
      color: #FFF;
      text-align: center;
      padding: 10px 16px;
      text-decoration: none;
      font-size: 17px;
    }
    
    #navbar a:hover {
      background-color: #ddd;
      color: black;
    }
    
    #navbar a.active {
      background: rgba(217, 78, 68, 0.5);
      color: white;
    }
    
    .content {
      padding: 16px;
      color: #ddd;
      background-color: #FFF
    }
    
    .sticky {
      position: fixed;
      top: 0;
      width: 100%;
    }
    
    .sticky+.content {
      padding-top: 60px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="header" class="navbar">
      <div id="Title">
        <a href="Accueil"><img src="IMAGES/PNG/logo.png" alt="logo" /></a>
      </div>
      <div id="navbar">
        <div class="menu"> <a class="active" href="javascript:void(0)">Blog</a></div>
        <div class="menu"> <a href="blog">Contact</a></div>
        <div class="menu"> <a href="blog">L'électrophotonique</a></div>
        <div class="menu"> <a href="blog">Qui sommes nous?</a></div>
      </div>
    </div>
    2 回复  |  直到 6 年前
        1
  •  1
  •   Mandalina    6 年前

    我还添加了一个css转换,使其淡出,但这只是个人喜好的问题。如果你愿意的话,你可以把它去掉。

    CODEPEN

    css格式

       #header {
          display: flex;
          justify-content: flex-end;
          background: rgba(139, 139, 157, 0);
            -webkit-transition: background 0.5s ease-in-out;
          -moz-transition: background 0.5s ease-in-out;
          -ms-transition: background 0.5s ease-in-out;
          -o-transition: background 0.5s ease-in-out;
          transition: background 0.5s ease-in-out;
          z-index: 2;
          position: fixed;
          top: 0;
          left: 0;
          right: 0;
        }
    
    #header.isSticky {
      background: rgb(139, 139, 157, 0.7);
        -webkit-transition: background 0.5s ease-in-out;
      -moz-transition: background 0.5s ease-in-out;
      -ms-transition: background 0.5s ease-in-out;
      -o-transition: background 0.5s ease-in-out;
      transition: background 0.5s ease-in-out;
    }
    

    js公司

    $(document).ready(function(){// checks vertical position of scroll bar 
    var scrollY = $(this).scrollTop();
    // when user refreshes psge
    if (scrollY > 0) {
      // if it is anywhere but the top change opacity by adding class .isSticky
      $('#header').addClass('isSticky');
    } else {
      $('#header').removeClass('isSticky');
    }
    
    
    $(window).on('scroll', function(){
      // while uesr scrolls check the scrollTop position
      var scrollY = $(this).scrollTop();
      if (scrollY > 0) {
       $('#header').addClass('isSticky');
      } else {
       $('#header').removeClass('isSticky');
      }
    });
    });
    
        2
  •  1
  •   Mattia Astorino    6 年前

    .isSticky 例如)通过选中元素offset top来滚动js。

    #header {
      display: flex;
      justify-content: flex-end;
      background: rgba(139, 139, 157, 0);
      z-index: 2;
    }
    
    #header.isSticky {
      background: rgba(139, 139, 157, 0.7);
    }