【发布时间】: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 时发现的一个简单效果
这个 CSS 效果太简单了,因为我没有 JS/jQuery 经验,所以我很想得到一些帮助。
我的问题是: - 我怎样才能做那个自动高度的东西? - 如何在滚动时增加上边距并降低标题的不透明度?
非常感谢,任何形式的帮助将不胜感激。
【问题讨论】:
标签: jquery css header scroll fixed