在滚动时隐藏标题,内容位于其上方

时间:2015-01-11 14:33:14

标签: jquery css header scroll fixed

我希望创建一个"固定"带有autoHeight的标题,当你滚动内容时,它会越过它。

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

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

这是我用inspect元素和源代码查找的内容:

标题有autoHeight(默认为~500px,使用inspect元素时为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经验,所以我很乐意帮助。

我的问题是: - 我怎样才能制作出autoHeight的东西? - 我怎样才能增加上边距和滚动时减少标题的不透明度?

非常感谢,非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

您可以使用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>

答案 1 :(得分:0)

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

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

我使用了这个片段,摆脱了缩放效果,只使用了不透明度。

这是编辑过的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
        });
    }
}

});