Change top value of fixed element depending on scroll - javascript

I like to create an effect where my navigation bar hides when scrolling down, but appears when scrolling up, no matter how far you scrolled down.
I've managed to get as far as this jsfiddle, but I'm lost from there.
The navigation div has position: fixed with top: 0. I decrease the top for every number of pixels scrolled down. But at the moment, I'm not having the insight to increase top for every number of pixels scrolled UP, no matter how far down you scrolled.
I hope it's clear enough what I try to achieve.
jQuery
var topScroll = 0;
$(document).scroll(function () {
var scrolled = $(this).scrollTop();
if (scrolled > $('nav').height()) {
$('nav').css('top', ($('nav').height() - scrolled));
}
if (topScroll > scrolled) {
//scrolling up
} else {
//scrolling down
}
topScroll = scrolled;
});
-
Edit
I think I need a way of saving the scrollTop() value when the scroll direction is changed and then add the difference between that number and the new scrollTop() to the top value of my navbar. I just don't know how to do that.

Is this what you want ?
var lastScrollTop = 0;
$(window).scroll(function(event){
var st = $(this).scrollTop();
if (st > lastScrollTop){
$('nav').css('display', 'none');
} else {
$('nav').css('display', 'block');
}
lastScrollTop = st;
});

Similar to luidgi27's code but with a smooth animation :)
-> altered Fiddle
var lastScrollTop = 0;
$(window).scroll(function(event){
var st = $(this).scrollTop();
if (st > lastScrollTop){
$('nav').animate({height: "0px"}, 100);
} else {
$('nav').animate({height: "50px"}, 100);
}
lastScrollTop = st;
});
Edit
Im not sure what you want. Changed the Code a bit so it has steps while showing the Nav.
-> new altered Fiddle
var lastScrollTop = 0;
var height = 50;
$(window).scroll(function(event){
var st = $(this).scrollTop();
if (st > lastScrollTop && height > 0){
$('nav').animate({height: "-=25px"}, 30);
height = height - 25;
} else if(st < lastScrollTop && height < 50){
$('nav').animate({height: "+=25px"}, 30);
height = height + 25;
}
lastScrollTop = st;
});
Edit2
-> new altered Fiddle 2
Here you go with the third and most accurate hiding effect.
var lastScrollTop = 0;
var diffrence = 0;
var height = 50;
var isGrowing = false;
var isShrinking = false;
$(window).scroll(function(event){
var st = $(this).scrollTop();
diffrence = st - lastScrollTop;
$('#debug').html(diffrence + "<br />" + height + "<br />" + $('nav').height());
if (diffrence > 0 && height < 50 && !isGrowing){
if((diffrence + height) > 50){
height = 50;
isGrowing = true;
$('nav').stop().animate({height: "50px"}, 100, function(){
isGrowing = false;
});
}else{
height = height + diffrence;
$('nav').animate({height: "+="+diffrence+"px"}, 0);
}
} else if(diffrence < 0 && $('nav').height() > 0 && !isShrinking){
if((diffrence + height) < 0){
height = 0;
isShrinking = true;
$('nav').stop().animate({height: "0px"}, 100, function(){
isShrinking = false;
});
}else{
height = height + diffrence;
diffrence = diffrence*-1;
$('nav').animate({height: "-="+diffrence+"px"}, 0)
}
}
lastScrollTop = st;
});

Related

Javascript scrolling for navigation bar not showing up when scrolling back up [django]

I am new to programming and having an issue with my navigation bar. I have a fully functional nav bar and when I scroll down it hides as expected but does not pop back up when I scroll back up. This is the function I used for the scrolling:
var scrolled;
var lastscroll = 0;
var delta = 5;
var navHeight = $('header').outerHeight();
$(window).scroll(function(event){
scrolled = true;
});
setInterval(function() {
if (scrolled) {
hasScrolled();
scrolled = false;
}
}, 250);
function hasScrolled() {
var scrolltop = $(this).scrollTop();
if(Math.abs(lastscroll - scrolltop) <= delta)
return;
if (scrolltop > lastscroll && scrolltop > navHeight){
$('header').removeClass('nav-down').addClass('nav-up');
} else {
if(scrolltop + $(window).height() < $(document).height()) {
$('header').removeClass('nav-up').addClass('nav-down');
}
}
lastScrollTop = scrolltop;
}
Does anyone know what I am missing?

Do something when offset of div is smaler than fixed value

I want to fade the opacity of menu if it's offset to top of the window is smaler than 700px.
But I don't understand why this code doesn't work.
$(window).scroll(function() {
var offset = $(".navigation-top").offset();
var posY = offset.top - $(window).scrollTop();
if ($(posY) < 700) {
$('.navigation-top').animate({'opacity':'0.1'},500);
} else {
$('.navigation-top').animate({'opacity':'1'},500);
}
});
Thnaks to Carsten and Jeremy,
I ended up with this. But the .stop() is mantadory. Otherwise it works only with a extreme delay because of the mess of data from scrolling.
$(window).scroll(function() {
var offset = $(".navigation-top").offset();
var posY = offset.top - $(window).scrollTop();
if (posY < 700) {
$('.navigation-top').stop().animate({'opacity':'0.1'},500);
} else {
$('.navigation-top').stop().animate({'opacity':'1'},500);
}
});

Event scroll up and fixed header jquery

I have a function
$(document).ready(function () {
var lastScroll = 0;
$(window).scroll(function(event){
var st = $(this).scrollTop();
if ((lastScroll - st) == 5) {
$("header").css("position", "fixed");
}
else {
$("header").css("position", "absolute");
}
lastScroll = st;
});
});
But I want, when I scroll up on 5px, header again shows.
How I can follow event scroll up 5px?
You're updating your lastScroll too often, to make sure it has scrolled past a certain delta (in your case 5px) you should include that clause before checking if the user has scroll up or down.
This tutorial may help you, but I also created a fiddle with an updated version of your code.
But it would go like this:
$(document).ready(function () {
var lastScroll = 0;
$(window).scroll(function(event){
var st = $(this).scrollTop();
// Make sure they scroll more than delta
if(Math.abs(lastScroll - st) <= 5)
return;
if (st < lastScroll) {
$("header").css("position", "fixed");
} else {
$("header").css("position", "absolute");
}
lastScroll = st;
});
});

bottom menu hiding/showing on scroll up and down

I have the following code which hides my menu when scrolls down, shows when scrolls up.
js
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('nav').outerHeight();
$(window).scroll(function(event) {
didScroll = true;
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 250);
function hasScrolled() {
var st = $(this).scrollTop();
if (Math.abs(lastScrollTop - st) <= delta)
return;
if (st > lastScrollTop && st > navbarHeight ) {
// Scroll Down
$('nav').removeClass('nav-down').addClass('nav-up');
} else {
// Scroll Up
if (st + $(window).height() < $(document).height()) {
$('nav').removeClass('nav-up').addClass('nav-down');
}
}
lastScrollTop = st;
}
I would like it to hide and reveal in the same way but both scrolling up and down (it hides when you scroll either up or down then comes back down)

Stop a function when I click on a div

I am trying to stop a function when I click on a div "ego". The function displays or hides the header depending on the position of the scroll and should only work by default.
Here's my code:
$("#ego").click(function(e){
var $this = $(this);
if($this.data('clicked', false)){
hasScrolled();
}
e.stopPropagation();
});
var didScroll;
var lastScrollTop = 0;
var delta = 5;
var navbarHeight = $('header').outerHeight();
$(window).scroll(function(event){
didScroll = true;
});
setInterval(function() {
if (didScroll) {
hasScrolled();
didScroll = false;
}
}, 250);
function hasScrolled() {
var st = $(this).scrollTop();
// Make sure they scroll more than delta
if(Math.abs(lastScrollTop - st) <= delta)
return;
// If they scrolled down and are past the navbar, add class .nav-up.
// This is necessary so you never see what is "behind" the navbar.
if (st > lastScrollTop && st > navbarHeight){
// Scroll Down
$('header').animate({top:"-178px"}, 200, 'easeOutCubic');
} else {
// Scroll Up
if(st + $(window).height() < $(document).height()) {
$('header').animate({top:"0px"}, 200, 'easeOutCubic');
}
}
lastScrollTop = st;
}
Here's the fiddle:
https://jsfiddle.net/antoniobarcos/wL2vuv4h/2/
Any idea?
so if i get you right you want to stop hiding the navigation bar on top when you click a certain div:
you can achieve this by adding a class on click on the button to your #ego like stopNavigation. further you have to adjust your if clause where you set the top position of the header:
$("#ego").click(function(e){
var $this = $(this);
$this.toggleClass('stopNavigation');
});
and
if (st > lastScrollTop && st > navbarHeight && !$('#ego').hasClass('stopNavigation')){
// Scroll Down
$('header').animate({top:"-178px"}, 200);
}
i updated your fiddle for further information! i hope this is what you want to achieve: https://jsfiddle.net/wL2vuv4h/3/

Categories