Reputation: 23
I'm looking to create a "fixed" header with autoHeight, and when you scroll the content will come over it.
Here's an example http://clapat.ro/berger/about.html
Basically, it's a home/intro section(id #hero), which has an autoheight and using a parallax effect, it looks like the content comes above it when scrolling.
This is what I found looking with inspect element and source code:
The header has autoHeight( ~500px default, 321px when using inspect element ) gets increased top margin and decreased opacity while scrolling:
height: 321.3px;
top: 400px;
opacity: -0.246105919003115;
Here's some functions I found:
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";
}
}
and a code responsible for the parallax effect
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));
}
});
}
And here's a simple effect I found while google-ing
This CSS effect is way too simple, and since I don't have JS/jQuery experience I'd love some help.
My questions are: - How can I make that autoHeight thing? - How can I increase top margin & decrease opacity of header while scrolling?
Thanks a lot, any kind of help would be much appreciated.
Upvotes: 1
Views: 1185
Reputation: 23
Ok so I've found a solution.
http://codyhouse.co/gem/pull-out-intro-effect/
I used this snippet, got rid of the zoom-effect and use just opacity.
Here's edited javascript if anyone alse is looking for it:
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
});
}
}
});
Upvotes: 1
Reputation: 14348
You can use a scroll
function and change its height
as scroll
becomes more than a certain amount 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>
Upvotes: 0