Infinite Scroll in both directions - javascript

I've been working on a small website and I want it to be scrollable and looped both upwards and downwards.
I made a jsfiddle to show my problem. Currently I'm working with this method:
$(document).ready(function(){
$(window).scroll(function() {
if ( $(window).scrollTop() >= ($('body').height() - $(window).height()) ) {
$(window).scrollTop(1);
}
else if ( $(window).scrollTop() == 0 ) {
$(window).scrollTop($('body').height() - $(window).height() -1);
}
});
});
http://jsfiddle.net/2LDFA/
My problem is, that there is no transition, this method only works if the content at the top and bottom is exactly the same.
Any Ideas how I can add the same div to top and bottom every time the user reaches the end?

Here is a working fiddle: http://jsfiddle.net/2L23c/
And here is the javascript:
(function($){
$(document).ready(function(){
var html = $(".what").html();
var what = '<div class="what">'+html+'</div>';
$(window).scrollTop(1);
$(window).scroll(function() {
if ( $(window).scrollTop() >= ($('body').height() - $(window).height()) ) {
$(".what").last().after(what);
if ($(".what").length > 2) {
$(".what").last().prev().remove();
$(window).scrollTop($(window).scrollTop() - $(".what").first().height());
}
}
else if ( $(window).scrollTop() == 0 ) {
$(".what").first().before(what);
$(window).scrollTop($(".what").first().height());
if ($(".what").length > 2) {
$(".what").last().remove();
}
}
});
});
})( jQuery );

Well, if you're using jquery, you can move the divs around on the page fairly easily, using insertAfter or the like. When you get to the bottom, grab the first div on the page and move it down to right after the last one. There's some interesting and slightly finicky work to be done in making sure you always get the right one, but it should totally be doable.

Related

How to know what part of HTML is the user on

so I want to know how I can get what section or part of my html I’m currently on. An example
So how do I know if a user has already scrolled down over part 2 using JavaScript
Or if they’re currently at part 1
<html>
<head>
</head>
<body>
<section class=“part 1”>
</section>
<section class= “part2>
</section>
</body>
</html>
The following codes will give you a little idea about how to handle this situation. Essentially you are going to want to get the scrollbar position which you can do using:
document.documentElement.scrollTop
You also want to get a range where the element you are looking for resides, in our case, it is .part1 and .part2. We can get that range by using offsetTop as the beginning of the limit and offsetTop + clientHeight to determine the end.
You are going to have to keep track of the window scroll event.
The following example is generic:
$(window).scroll(function(e) {
if (document.documentElement.scrollTop > 0
&& document.documentElement.scrollTop < $('.part2').offset().top ) {
$('div').html("At part1")
} else {
$('div').html("At part2")
}
});
JSFiddle
Likewise, if you want a little bit of modularity:
$(window).scroll(function(e) {
let watchList = ['part1', 'part2', 'part3'];
let scrollTop = document.documentElement.scrollTop;
for (var classname of watchList) {
let el = document.getElementsByClassName(classname)[0];
if (scrollTop > el.offsetTop &&
scrollTop < el.offsetTop + el.clientHeight) {
$('div').html("At <strong>"+classname+"</strong>");
}
}
});
JSFiddle
The possibilities are limitless to continue and make this more useful, but I'll leave that up to you.
you can use is[":focus"] function to find which div has focus currently.
if($(".part1").is(":focus"))
{
//you code
}
else if($(".part2").is(":focus"))
{
//you code
}
you can use mouseenter function it fires when the mouse goes into that div for the first time.
$(".part1").on('mouseenter', function(){
//your command
});
you can use mouseover function to find where is mouse right now. it fires when mouse moves inside that div.
$(".part1").on('mouseover', function(){
//your command
});
You can use javascripts offsetTop functionality. This is a parameter that returns how far down from the top a div is in the number of pixels.
It can also return how far down the user has scrolled when called on the window object itself. Then it's just a matter of math. See if the user has scolled down far enough to be past the div in reference.
For example:
var part1DivOffset = document.getElementsByClassName("part 1")[0].offsetTop;
var part2DivOffset = document.getElementsByClassName("part2")[0].offsetTop;
var scrollTop = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop;
This code will get you 3 variables. The first 2 lines save the offsetTop of the div's. While the third line detects how far down the user has scrolled. Then you can do math with the variables:
if(scrollTop >= part1DivOffset){
//we are past part1
}
if(scrollTop >= part2DivOffset){
//We are past part 2
}
if(scrollTop >= part1DivOffset && scrollTop < part2DivOffset){
//We are past part 1 but not past part 2
}

jquery toggle class on scroll

I'm using bootstrap as my css framework. I want to be able to toggle a class on that navbar once the user has scrolled past the big header image at the top of the website.
EDIT:
I went full derp... so I drank some more coffee and figured this out. Now if it's the best way to do it, not sure but here is what I have, and it works..
$(window).scroll(function() {
if ($(".navbar").offset().top + $(".navbar").outerHeight(true) > $('.landing-header').outerHeight(true)) {
$(".navbar").addClass("darker-bg");
} else {
$(".navbar").removeClass("darker-bg");
}
});
#SetSailMedia also answered it so I went with their answer, which was cleaner than my imo
A better way to measure is to compare $(this).scrollTop during $(window).scroll() event.
$(window).scroll( function( e ){
if( $(this).scrollTop() > $('.landing-header').offset().top ){
$(".navbar").addClass("darker-bg");
} else {
$(".navbar").removeClass("darker-bg");
}
});
Forgive me, first post of this answer kept your original .offset().bottom property, which does not exist. I've updated to .offset().top. If you wanted to measure against bottom of the element, replace that line with:
if( $(document).scrollTop() > ($('.landing-header').offset().top + $('.landing-header').outerHeight()) ){
You can toggle class when scroll somewhere with something like this:
$(window).scroll(function() {
var scroll = $(window).scrollTop();
if (scroll >= 1) {
$(".header").addClass("change");
} else {
$(".header").removeClass("change");
}
});
Here is jsfiddle.

How do I add a class to the body using jQuery, scrollTop, and toggleClass

I have a question concerning jQuery's scrollTop functionality, and it's ability to toggle a class based on the amount of vertical scroll.
What I am trying to accomplish is on any page with a "section.banner" class, that after you scroll past the banner a class is applied to the body tag. This will allow me to change the fill colors of several SVGs that are in the site's header, as well as a fixed positioned side nav that is for pagination.
I am terrible at javascript, and have been stuck searching and trying to get this for hours. any help will be greatly appreciated. Here's the code that I'm working with now (CodeKit is telling me it is wrong, which I am not surprised). The value of 200 is just a placeholder and will be calculated by the height of a fluid image. Full disclosure, I have no idea if the brackets and parenthesis are correct.
// Header/Fixed Pagination Banner Scroll Recoloriing (toggle class)
// Check If '.banner' Exists
if( $('section.banner').length > 0) {
$('body').scrollTop(function)()
{
if $(window).scrollTop >= 200 {
$('body').toggleClass('downtown');
return false;
}
}
}
Try something like this :
if( $('section.banner').length > 0) {
$(window).scroll(function() {
{
if ($(window).scrollTop() >= $('section.banner').scrollTop()) {
$('body').toggleClass('downtown');
return false;
}
});
}
UPDATE
There was little mistake in my code : http://jsfiddle.net/t2yp15hq/
var top = $('section.banner').position().top;
if($('section.banner').length > 0) {
$(window).scroll(function() {
if ($(this).scrollTop() >= top) {
$('body').addClass('downtown');
}
else
{
$('body').removeClass('downtown');
}
});
}
It does not work with toogleClass, the background is flashing.
UPDATE
http://codepen.io/anon/pen/wBWzXy
The solution is to recalculate the top when the window is resized :
$(window).resize(function(){
top = $('section.story-intro').offset().top - 90;
});

Trigger a sticky footer once it enters the page to reveal hidden content

I am having trouble creating a dynamic sticky footer that only sticks to the bottom once it enters the page then reveals a hidden surprise when you continue to scroll.
I have created this method but it is not exactly as I had hoped: http://jsfiddle.net/R3n7s/
jQuery(window).load(function(){
jQuery(window).scroll(function() {
if(jQuery(window).scrollTop() + jQuery(window).height() > jQuery(document).height() - 200) {
jQuery('h2 a').fadeIn(400);
} else {
jQuery('h2 a').fadeOut(400);
}
});
});
This works okay but my goal is to have "This is the hidden footer that will be revealed at the end" to appear to scroll with the rest of the content and then stay put once it enters the page and then to have the surprise be revealed.
Would be grateful for any suggestions. Thank you.
EDIT: hope this helps to explain what I'm hoping to achieve. The above example is my fallback solution but hopefully this can be done:
EDIT 2: I have put together a new sample here but it's not quite right. When scrolling back down, it doesn't reverse the effect but it looks very close to what I'm hoping.
However, it's seems a bit of a hack... and it does not work on mobile. If anyone has any suggestions on how to do this better and make a conditional sticky footer, it would be great to hear your suggestions.
Here is a new sample where the text scrolls up with the page but then stops and the surprise fades in: http://jsfiddle.net/R3n7s/3/
jQuery(window).load(function(){
Query(window).scroll(function() {
if(jQuery(window).scrollTop() + jQuery(window).height() > jQuery(document).height() - 300) {
jQuery('#footer').css({"position":"fixed", "bottom":"0"});
} else {
jQuery('#footer').css({"position":"relative", "bottom":"auto"});
}
if(jQuery(window).scrollTop() + jQuery(window).height() > jQuery(document).height() - 70) {
jQuery('#surprise').fadeIn(600);
} else {
jQuery('#surprise').fadeOut(600);
}
});
});
You can try changing the height of '#footer' relative to the scrollTop value change when the scroll reached that jQuery(window).scrollTop() + jQuery(window).height() > jQuery(document).height() - 200 mark.
Also writing jQuery is tedious and hard to read for me :D. But if you're doing this because your using another library that uses the same dollar sign '$' alias, you can wrap your code in a jQuery function and alias '$' on it for brevity.
(function ($) {
$(window).load(function () {
$(window).scroll(function () {
var docViewTop = $(this).scrollTop(),
docViewBottom = docViewTop + $(this).height(),
docHeight = $(document).height(),
marginBottom = parseInt($('#wrapper').css('margin-bottom'));
if (docViewBottom >= docHeight - 200) {
$('#footer').height((docViewBottom + marginBottom) - docHeight)
}
if (docViewBottom >= docHeight - 50) {
$('h2 a').fadeIn(600);
} else {
$('h2 a').fadeOut(600);
}
})
});
})(jQuery);
See this jsfiddle.
Do you have something like this in mind? Inside of the if block:
{
$('h2 a').fadeIn(2000);
$('p').fadeIn(400);
} else { ... }
And with matching CSS:
#footer p {
display: none;
}
I did something very similar on this website. I set a data-attribute on each of the pages. As you scroll through them, I trigger various actions based on which slide is active and finally, on the 5th slide, I slide everything up slightly to make room for the footer, and the footer appears. If you scroll back up, the animation reverses and the footer retracts back down.
var activeslide = parseInt($(this).find('.active').prev().data('index'));
if (activeslide == 1) {}
if (activeslide == 2) {}
if (activeslide == 4) {}
if (activeslide == 5) { displayFooter(); }
var p = null;
function displayFooter() {
if(this===p) {
setTimeout(function(){
$('footer').css({'z-index':'0'});
},2000);
$('footer').stop().animate({'margin-top':0},2000);
$('.main').stop().animate({'top':0},2000);
$('#iphone').stop().animate({'top':'75px'},2000);
p = null;
return false;
} else {
$('footer').css({'z-index':'999'});
$('footer').stop().animate({'margin-top':'-255px'},2000);
$('.main').stop().animate({'top':'-65px'},2000);
$('#iphone').stop().animate({'top':'20px'},2000);
p = this;
return false;
}
}

Page Scrolling jQuery Navigation

I am building a website which can be viewed here: argit.bounde.co.uk
I have done the majority of the content and I am now trying to work on the navigation. I have three navigation bars (only one is ever visible) and need this method to work no matter which is showing. If you resize your browser to make your window narrower that will show a second, and then when you scroll the navigation that appears is a third.
I have got it working to a fashion but the problem is when I click a link it jumps to where it wants to go momentarily, then returns and then scrolls as it is meant to. This is because of the "href="#target" that i have left in the nav. I have tried including a "return false" but then if the broswer doesnt support JS then the navigation doesnt work at all.
The next problem is I want a way to make the target "over". Currently when you click a link it scrolls to the selected one and the nav updates which link is "over" as it passes them. I want this for when the user is scrolling up and down the page, but if they click a link I want that link to be "over" (and the respective links from other navigations) and not be affected by the scroll checks that would normally override it.
The solution I am using for my onClick navigation is below, I know there are plug ins that will do this kind of thing but I want to write it myself so i can get a better understanding of jQuery. Im not sure if the solution I am using at the moment is a good one, if not please advise me:
function navigation() {
$('html, body').stop().animate({
scrollTop: $($(this).attr('href')).offset().top
}, 1500);
}
The solution I am using for the scrolling checks I found by accident and is below, It works by over riding the equation above it and is actually quite simple. There is also an action to fix the navigation when scrolling.
function navCheck() {
var documentHeight = $(document).height();
var windowHeight = $(window).height();
var windowTop = $(window).scrollTop() + 100;
var navTop = $("#scrollanchor").offset().top;
var mobileTop = $("#mobileanchor").offset().top;
var mobileHeight = $("#mobileanchor").height();
var overviewTop = $("#slider").offset().top;
var bioTop = $("#bio").offset().top;
var solutionsTop = $("#solutions").offset().top;
var experianceTop = $("#experiance").offset().top;
var contactTop = $("#contact").offset().top;
if($(window).scrollTop() > navTop) {
$("#leftfixer").addClass("leftfix");
} else {
$("#leftfixer").removeClass("leftfix");
}
if($(window).width() < 1200){
if(windowTop - 90 > mobileTop + mobileHeight) {
$("#mobilefix").slideDown();
} else {
if(windowTop - 96 <= mobileTop) {
$("#mobilefix").hide();
}
}
} else {
$("#mobilefix").slideUp();
}
$("li").removeClass("over");
$("li.navoverview").addClass("over");
if(windowTop > bioTop) {
$("li").removeClass("over");
$("li.navbio").addClass("over");
}
if(windowTop > solutionsTop) {
$("li").removeClass("over");
$("li.navsolutions").addClass("over");
}
if(windowTop > experianceTop) {
$("li").removeClass("over");
$("li.navexperiance").addClass("over");
}
if(windowTop > contactTop || windowTop > documentHeight - windowHeight) {
$("li").removeClass("over");
$("li.navcontact").addClass("over");
}
}
This is my first post here so if I have missed out any information Im sorry! I have also looked for similar posts but it seems most people go for a plugin when doing this kind of thing. Thank you
UPDATE: The page jumping to the anchor has been fixed
Normally, if you use JavaScript to scroll the page, you would put return false; after calling the function. This prevents the page from scrolling momentarily to the anchor.
Something like
function navigation() {
$('html, body').stop().animate({
scrollTop: $($(this).attr('href')).offset().top
}, 1500);
return false;
}

Categories