jquery auto scroll website with pause - javascript

How can I set jQuery auto scroll web page and with pause / stop for a specific px and continue auto scrolling? It's something like a user scrolling on the web page reading an article, like scroll and stop and continue scrolling something like that. I can't seem to find a good example on the internet and all I got the answer from searching is only jQuery auto scroll example only.
If you can't understand my question it's something looks like this: Example from codepen
Here is my code:
$("html, body").animate({ scrollTop: $(document).height() }, 1000);
setTimeout(function() {
$('html, body').animate({scrollTop:0}, 1000); // 1000 is the duration of the animation
},500);
setInterval(function(){
$("html, body").animate({ scrollTop: $(document).height() }, 500); // Speed from Bottom to top
setTimeout(function() {
$('html, body').animate({scrollTop:0}, 5000); // Speed from Top to bottom
},500); // What is this speed refer to?
},1000); // What is this speed refer to?
By the way, I am new in jQuery, do you mind explain a little bit to me what is the meaning of both of the 500 and 1000 second meaning? I know it refers to second but what is the meaning of adding 2 of it? Thanks!

Here is an working example
setInterval(function scroll() {
$("section").each(function(i, e) {
$("html, body").animate({
scrollTop: $(e).offset().top
}, 500).delay(500); // First value is a speed of scroll, and second time break
});
setTimeout(function() {
$('html, body').animate({
scrollTop: 0
}, 5000); // This is the speed of scroll up
}, 4000); //This means after what time should it begin (after scrolling ends)
return scroll;
}(), 9000); //This value means after what time should the function be triggered again
//(It should be sum of all animation time and delay) 9000 = 5000 + 4000
main {
background: #EEE;
}
main section {
background: #DDD;
width: 90%;
margin: 30px auto;
padding: 10px 15px;
min-height: 1000px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<main>
<section>
</section>
<section>
</section>
<section>
</section>
<section>
</section>
</main>
EDIT
I edited a little bit snippet so that the code was not twice. I declare function (scroll()) and use it inside interval. Thanks to that there is no need for the same code at the begining.
EDIT2
If you want the scroll to stop depending on px and not section just change this:
setInterval(function scroll() {
$("section").each(function(i, e) {
$("html, body").animate({
scrollTop: $(e).offset().top
}, 500).delay(500); // First value is a speed of scroll, and second time break
});
...
To this:
setInterval(function scroll() {
for (var i = 0; i < 4000; i += 800) {
$("html, body").animate({
scrollTop: i
}, 500).delay(500);
}
...
EDIT3
If you want to scroll to bottom at the end, you can do it like this:
setInterval(function scroll() {
for (var i = 0; i < 4000; i += 800) {
$("html, body").animate({
scrollTop: i
}, 500).delay(500);
if (i + 800 >= 4000) {
$("html, body").animate({
scrollTop: $(document).height()
}, 500).delay(500);
}
}
...

Related

Create JS chrome snippet to auto scroll page up and down in infinite loop

I need to show content of Jira dashboard on screen and scroll it up and down automaticly inside infinite loop.
I tried to create Chrome Snippet but for/while loops seems not work.
function scroll(speed) {
$('html, #ghx-pool').animate({ scrollTop: $(document).height() - $(window).height() }, speed, function() {
$(this).animate({ scrollTop: $(document).height() }, speed);
});
}
while(true){
setInterval(scroll(25000), 10);
}

autoscroll jquery with infinite scroll loop

I have a page that I would like to have it auto scroll as soon as the full page is loaded, scroll to the bottom and basically automatically loop to the start of the page (essentially loading the same page underneath it) to make it an infinite loop. I have the following script with does this but it breaks after a few scroll top/bottom. So far I'm using jQuery to help. I found this snippet on another StackOverflow post but cannot find it.
function scroll(element, speed) {
element.animate({ scrollTop: $(document).height() }, speed,'linear', function() {
$(this).animate({ scrollTop: 0 }, 3000, scroll(element, 4000));
});
}
setTimeout(function() {
scroll($('html, body'), 900000)
}, 3000);
I was able to solve it using a more simple method:
window.onload = function () {
var Delay = $(document).height() * 65;
var DelayBack = $(document).height() * 5;
function animateBox() {
$('html, body')
.animate({scrollTop: $(document).height()}, Delay, 'linear')
.animate({scrollTop: 0}, DelayBack, animateBox);
}
setInterval(animateBox, 100);
}
I feel like your issue is related to this line...
$(this).animate({ scrollTop: 0 }, 3000, scroll(element, 4000));
Try changing it to the following to make it a callback and not an immediate invocation:
$(this).animate({ scrollTop: 0 }, 3000, function(){ scroll(element, 4000); });
fetch data using php and ajax apply scroll function to window event
$(document).ready(function(){
$(window).scroll(function(){
var lastID = $('.load-more').attr('lastID');
if ($(window).scrollTop() == $(document).height() - $(window).height() && lastID != 0){
$.ajax({
type:'POST',
url:'getdata.php',
data:'id='+lastID,
beforeSend:function(html){
$('.load-more').show();
},
success:function(html){
$('.load-more').remove();
$('#postList').append(html);
}
});
}
});
});

Execute scrollTop animation on $(window).scroll with jquery

I am trying to move the slide once every time that I make scroll in the page. But scroll event doesn't stop and repeat the event. How can I make an animation to move to the next slide color with scrollTop inside $(window).scroll just once for every time? See my Fiddle
And this is the piece of code which doesn't work :(
$('html, body').animate({
scrollTop: ($(next).offset().top)
},500);
My target is something like that http://www.sincedilla.com/
this is probably what you need.
The scroll event ve to be prevented until the animation is finished ,
docs for animation http://api.jquery.com/animate/ read the callback section
$(this).bind('mousewheel', function (e) {
if (!animating) {
animating = true;
if (e.originalEvent.wheelDelta < 0) {
next = $(first).next();
first = $(next);
// scroll down
$("html, body").animate({
scrollTop: ($(next).offset().top)
}, 900, function(){
animating = false;
});
} else {
first = $(next).prev();
next = $(first);
// scroll up
$("html, body").animate({
scrollTop: ($(first).offset().top)
}, 900,function(){
animating = false;
});
}
}
return false;
});
working fiddle http://jsfiddle.net/fdbh0no8/

waypoint js click to next div

Working on a function with waypoint.js that takes the current div in the viewport and finds the next div when clicking a button.
Currently I’m getting a undefined value for the ‘next’. Not sure what could be wrong I guess the value can’t move from the waypoint function to the click function. Any help would be lovely.
$('.wrap').waypoint(function() {
var next = $(this).next();
$(".button").click(function() {
$("html, body").animate({ scrollTop: next.offset().top }, 1000);
});
});
i suggest you to chain it instead of doing this in the callback:
$('.wrap').waypoint().addBack(this).find(".button").click(function() {
var next = $(this).closest('.wrap').next();
$("html, body").animate({ scrollTop: next.offset().top }, 1000);
});
or could be something like this:
$('.wrap').waypoint().done(function(){
$(this).find(".button").click(function() {
var next = $(this).closest('.wrap').next();
$("html, body").animate({ scrollTop: next.offset().top }, 1000);
});
});

Remain in bottom while animating footer

I have a footer#footer which contains company location. When I load contact page with hash #locationjavascript scrolls to the bottom of the page, and makes a callback to enlarge the footer, as it is the important element. How do I increase the size of the footer while the page remains at the bottom (so it seems like footer pushing up the content).
var hash = window.location.hash;
if (hash=="#location") {
var scrollpos = $(document).height()-$(window).height()+"px"
$("body,html").delay(200).animate({
scrollTop: scrollpos
}, "slow", function() { exfoot() });
}
function exfoot(){
var $f = $("footer#footer");
$f.animate({
height: $f.height()*2
}, "slow");
}
I added this to the function
$("html, body").animate({ scrollTop: $(document).height() }, "slow");
so it looks
function exfoot(){
var $f = $("footer#footer");
$f.animate({
height: $f.height()*2
}, "slow");
$("html, body").animate({ scrollTop: $(document).height() }, "slow");
}
Both the speed of the $f.animate and body,html.animate have to be the same, so they act simultaneously.

Categories

Resources