【问题标题】:Hide header on scrolling, content comes above it滚动时隐藏标题,内容位于其上方
【发布时间】:2015-01-11 14:33:13
【问题描述】:

我希望创建一个带有 autoHeight 的“固定”标题,当您滚动内容时,它会出现。

这是一个例子 http://clapat.ro/berger/about.html

基本上,它是一个主页/介绍部分(id #hero),它具有自动高度并使用视差效果,滚动时看起来内容在其上方。

这是我在检查元素和源代码中发现的:

标题具有 autoHeight(默认为 ~500px,使用检查元素时为 321px)在滚动时增加了上边距并降低了不透明度:

height: 321.3px;
top: 400px;
opacity: -0.246105919003115;

这是我找到的一些函数:

function HeroHeight() {
    if( $('#hero').length > 0 ){

        if ($('#hero').hasClass('hero-big')) {
            var heights = window.innerHeight;
            document.getElementById("hero").style.height = heights * 0.85 + "px";
        } else if ($('#hero').hasClass('hero-small')) {
            var heights = window.innerHeight;
            document.getElementById("hero").style.height = heights * 0.40 + "px";               
        } else  {           
            var heights = window.innerHeight;
            document.getElementById("hero").style.height = heights + "px";
        } 

    }

以及负责视差效果的代码

    function HeroParallax() {

    var page_title = $('body');
        var block_intro = page_title.find('#hero');
        if( block_intro.length > 0 ) var block_intro_top = block_intro.offset().top;    
    $( window ).scroll(function() {
        var current_top = $(document).scrollTop(); 
        var hero_height = $('#hero').height();
        if( $('#hero').hasClass('parallax-hero')){            
            block_intro.css('top', (current_top*0.5));          
        }
        if( $('#hero').hasClass('static-hero')){              
            block_intro.css('top', (current_top*1));            
        }
        if( $('#hero').hasClass('opacity-hero')){                
            block_intro.css('opacity', (1 - current_top/hero_height*1));
        }
    });

}

这是我在 google-ing 时发现的一个简单效果

http://jsfiddle.net/EWefL/

这个 CSS 效果太简单了,因为我没有 JS/jQuery 经验,所以我很想得到一些帮助。

我的问题是: - 我怎样才能做那个自动高度的东西? - 如何在滚动时增加上边距并降低标题的不透明度?

非常感谢,任何形式的帮助将不胜感激。

【问题讨论】:

    标签: jquery css header scroll fixed


    【解决方案1】:

    好的,我找到了解决方案。

    http://codyhouse.co/gem/pull-out-intro-effect/

    我使用了这个 sn-p,去掉了缩放效果,只使用了不透明度。

    如果有人在寻找它,这里是编辑过的 javascript:

    jQuery(document).ready(function($){
    var introSection = $('#cd-intro-background'),
        introSectionHeight = introSection.height(),
        //change scaleSpeed if you want to change the speed of the scale effect
        scaleSpeed = 0.3,
        //change opacitySpeed if you want to change the speed of opacity reduction effect
        opacitySpeed = 1; 
    
    //update this value if you change this breakpoint in the style.css file (or _layout.scss if you use SASS)
    var MQ = 1170;
    
    triggerAnimation();
    $(window).on('resize', function(){
        triggerAnimation();
    });
    
    //bind the scale event to window scroll if window width > $MQ (unbind it otherwise)
    function triggerAnimation(){
            $(window).on('scroll', function(){
                //The window.requestAnimationFrame() method tells the browser that you wish to perform an animation- the browser can optimize it so animations will be smoother
                window.requestAnimationFrame(animateIntro);
            });
    }
    //assign a scale transformation to the introSection element and reduce its opacity
    function animateIntro () {
        var scrollPercentage = ($(window).scrollTop()/introSectionHeight).toFixed(5);
        //check if the introSection is still visible
        if( $(window).scrollTop() < introSectionHeight) {
            introSection.css({
                'opacity': 1 - scrollPercentage*opacitySpeed
            });
        }
    }
    
    });
    

    【讨论】:

      【解决方案2】:

      您可以使用scroll 函数并更改其height,因为scroll 变得超过一定数量FIDDLE

      $(window).scroll(function(){
          if ($(window).scrollTop() > 20){
              $('header').height('60px');
              $('header').css('z-index' , '300');
          }
          else{
               $('header').height('100px');
              $('header').css('z-index' , '100');
          }
      });
      body {
          padding:0;
          margin:0;
      }
      header {
          background:#cff;
          height:100px;
          position:fixed;
          right:0;
          left:0;
          z-index:100;
      }
      section {
          background: #579;
          height:600px;
          top:100px;
          position:relative;
          z-index:200;
      }
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
      <header><a href="/">Working header link</a>
      </header>
      <section>Section</section>

      【讨论】:

      • 这不是我要找的,但非常感谢您的回复! :)
      • 好吧,我希望页眉不断增加上边距,降低不透明度,并有一个自动高度。在你的代码中,它只会改变它的高度,一次。
      猜你喜欢
      • 2012-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多