How to keep the header static, always on top while scrolling?

前端 未结 8 788
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 08:15

How would I go about keeping my header from scrolling with the rest of the page? I thought about utilizing frame-sets and iframes, jus

相关标签:
8条回答
  • 2020-12-02 09:07

    here is one with css + jquery (javascript) solution.

    here is demo link Demo

    //html
    
    <div id="uberbar">
        <a href="#top">Top of Page</a>
        <a href="#bottom">Bottom of Page</a>
    
    </div>
    
    //css 
    
    #uberbar    { 
        border-bottom:1px solid #eb7429; 
        background:#fc9453; 
        padding:10px 20px; 
        position:fixed; 
        top:0; 
        left:0; 
        z-index:2000; 
        width:100%;
    }
    
    //jquery
    
    $(document).ready(function() {
        (function() {
            //settings
            var fadeSpeed = 200, fadeTo = 0.5, topDistance = 30;
            var topbarME = function() { $('#uberbar').fadeTo(fadeSpeed,1); }, topbarML = function() { $('#uberbar').fadeTo(fadeSpeed,fadeTo); };
            var inside = false;
            //do
            $(window).scroll(function() {
                position = $(window).scrollTop();
                if(position > topDistance && !inside) {
                    //add events
                    topbarML();
                    $('#uberbar').bind('mouseenter',topbarME);
                    $('#uberbar').bind('mouseleave',topbarML);
                    inside = true;
                }
                else if (position < topDistance){
                    topbarME();
                    $('#uberbar').unbind('mouseenter',topbarME);
                    $('#uberbar').unbind('mouseleave',topbarML);
                    inside = false;
                }
            });
        })();
    });
    
    0 讨论(0)
  • 2020-12-02 09:11

    .header {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 88px;
      z-index: 10;
      background: #eeeeee;
      -webkit-box-shadow: 0 7px 8px rgba(0, 0, 0, 0.12);
      -moz-box-shadow: 0 7px 8px rgba(0, 0, 0, 0.12);
      box-shadow: 0 7px 8px rgba(0, 0, 0, 0.12);
    }
    
    .header__content-text {
      text-align: center;
      padding: 15px 20px;
    }
    
    .page__content-container {
      margin: 100px auto;
      width: 975px;
      padding: 30px;
    }
    <div class="header">
      <h1 class="header__content-text">
        Header content will come here
      </h1>
    </div>
    <div class="page__content-container">
      <div style="height:600px;">
        <a href="http://imgur.com/k9hz3">
          <img src="http://i.imgur.com/k9hz3.jpg" title="Hosted by imgur.com" alt="" />
        </a>
      </div>
      <div style="height:600px;">
        <a href="http://imgur.com/TXuFQ">
          <img src="http://i.imgur.com/TXuFQ.jpg" title="Hosted by imgur.com" alt="" />
        </a>
      </div>
    </div>

    0 讨论(0)
提交回复
热议问题