I'm trying to disable scroll counter after user scrolls up or down and while animation executing.
I tried with timeouts but nothing worked. Timeout waited for 800ms with scrolling, but counter was still incerasing in the background.
I'm trying to achieve this functionality: https://alvarotrigo.com/fullPage/
My results so far: https://objemnarave.si/voda/index.php?lang=en
$('html').on('wheel', function(event) {
if (event.originalEvent.deltaY > 0) {
//scroll down
counter++;
//Check if counter is larger that section number, then execute animation
if (!(counter > maxSections)) {
$('html, body').animate({
scrollTop: $( $(".sect-"+counter) ).offset().top
}, 800);
}
} else {
//scroll up
counter--;
//Check if counter is smaller than previous section
if (!(counter < 1)) {
$('html, body').animate({
scrollTop: $( $(".sect-"+counter) ).offset().top
}, 800);
}
}
if (counter <= 0) {
counter = 1;
}
else if (counter >= 13) {
counter = maxSections;
}
console.log(counter);
});
It's possible to disable scroll counter in the backround while
animation is executing?
Related
ScrollTop is a jquery plugin (go to top of page), trying to make slow Scroll Speed, but not working. I have changed scrollSpeed : 'fast', to scrollSpeed : 'slow', but it still fast, nothing change.
JS:
$.fn.extend({
addScrollTop: function(options) {
var defaults = {
useObjWindow : false,
scrollSpeed : 'fast',
zIndex: '99'
}
var options = $.extend(defaults, options);
if($('body').find('.scrollTop-btn').length == 0) {
$('body').append('<div class="scrollTop-btn" style="display:none;"><i class="fa fa-chevron-up"></i></div>');
}
if(options.useObjWindow) {
var parentWindow = this;
var scrollWindow = this;
}
else {
var parentWindow = window;
var scrollWindow = 'html, body';
}
$(document).ready(function() {
$('.scrollTop-btn').on('click', function() {
$(scrollWindow).animate({scrollTop:0}, options.scrollSpeed);
});
$(parentWindow).scroll(function() {
$('.scrollTop-btn').hide();
var aTop = $('.scrollTop-btn').height() + 20;
if($(this).scrollTop() >= (aTop + 20)) {
$('.scrollTop-btn').css('z-index', options.zIndex);
$('.scrollTop-btn').show();
}
else {
if($('.scrollTop-btn').is(":visible")) {
$('.scrollTop-btn').hide();
}
}
});
});
}
});
Call:
jQuery(document).ready(function() {
jQuery("body").addScrollTop();
});
How to make it slower or smoother, when it go to top?
use jquery animate()
$('html,body').animate({ scrollTop: 0 }, 'slow');
refer this stack overflow question
Only with CSS:
html {
scroll-behavior: smooth;
}
Using jQuery
If you want you can customize how much time you would like the "scrolling" to last. Or do something else when scroll effect is finished.
I have a: <a href="#" class="scrollToTop">
And want to scroll to an element with class "beginning"
$('.scrollToTop').on('click', function(event){
event.preventDefault();
$('html, body').stop().animate({scrollTop: $('.beginning').offset().top}, 500);
});
The last part where you have the 500. You can set there how much time you want the effect to last. In milliseconds.
http://api.jquery.com/animate/
Replace 'slow' with - Ex. 1000, 5000, 10000
$('html,body').animate({ scrollTop: 0 }, <milliseconds>);
// Scroll 2 sec
$('html,body').animate({ scrollTop: 0 }, 2000);
// Scroll 5 sec
$('html,body').animate({ scrollTop: 0 }, 5000);
I am creating a swipe page system using the library touchSwipe.js.
With the method I have, I can swap between pages however, I cannot figure out to go back to previous page properly.
Right now if I swipe right and left few times the counting system goes out summing the swipes.
How can I make the counting system properly in order to move between pages?
var swipeRight = 0;
var swipeLeft = 0;
var swipePage = 0;
function swipe1(event, direction, distance, duration, fingerCount) {
if (direction == "left") {
swipeLeft++;
if (swipeLeft == 5) {
swipeLeft = 0;
}
}
if (direction == "right") {
swipeRight++;
if (swipeRight == 5) {
swipeRight = 0;
}
}
swipePage = swipeLeft - swipeRight;
if (swipePage == 0) {
$("html, body").animate({
scrollLeft: 0,
}, 1500);
swipeLeft = 0;
swipeRight = 0;
}
if (swipePage == 1) {
$("html, body").animate({
scrollLeft: $("#hwwPage").offset().left
}, 1500);
}
if (swipePage == 2) {
$("html, body").animate({
scrollLeft: $("#projPage").offset().left
}, 1500);
}
if (swipePage == 3) {
$("html, body").animate({
scrollLeft: $("#digiPage").offset().left
}, 1500);
}
if (swipePage == 4) {
$("html, body").animate({
scrollLeft: $("#contPage").offset().left
}, 1500);
}
console.log(swipeRight + "+" + swipeLeft);
console.log(swipePage);
}
I have fixed your code: you don't really need swipeleft and swiperight i think. Just increase or decrease the swipePage value depending on the swipe direction:
var swipePage = 0;
function swipe1(event, direction, distance, duration, fingerCount) {
if (direction == "left")
if (swipePage > 0)
swipePage++;
if (direction == "right")
if (swipePage < 4)
swipePage--;
var pageIds = ["",
"#hwwPage",
"#projPage",
"#digiPage",
"#contPage"
];
$("html, body").animate({
scrollLeft: $(pageIds[swipePage]).offset().left
}, 1500);
}
I found this code for autoscroll a page.
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(document).ready(function () {
var myInterval = false;
myInterval = setInterval(AutoScroll, 5000);
function AutoScroll() {
var iScroll = $(window).scrollTop();
iScroll = iScroll + 500;
$('html, body').animate({
scrollTop: iScroll
}, 1000);
}
$(window).scroll(function () {
var iScroll = $(window).scrollTop();
if (iScroll == 0) {
myInterval = setInterval(AutoScroll, 5000);
}
if (iScroll + $(window).height() == $(document).height()) {
clearInterval(myInterval);
setTimeout(function(){ window.location.href = "index.php"; },3000)
}
});
});
});//]]>
</script>
My page looks like this:
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
<div id="div5"></div>
My goal is to autoscroll from div to div after 20 seconds en after the last div back to top.
My Question is:
How to make it work scrolling from div to div?
I use window.location.href = "index.php" for refreshing the page en start over again. Is there a different way to achieve the same without a refresh? So go back to the top div and refresh the content of the page?
To scroll from div to div, you could define an array of the selectors for each div. Then in your AutoScroll function, get the element at the current index, get the offset from the top of the page for that element, and scroll to that. Then increment the index value.
To scroll to the top of the page, set the index back to 0 when there are no more elements to scroll to
Something like this should work:
<script type='text/javascript'>
$(window).load(function(){
$(document).ready(function () {
var myInterval = false;
var index = 0;
var elements = ["#div1","#div2","#div3","#div4","#div5"];
myInterval = setInterval(AutoScroll, 5000);
function AutoScroll() {
$('html, body').animate({
scrollTop: $(elements[index]).offset().top
}, 1000);
if(index < (elements.length - 1)){
index++;
} else {
index = 0;
}
}
});
});
</script>
See this JSFiddle: https://jsfiddle.net/3gb5uLgs/
I added this function
$(function() {
$('a[href*=#]:not([href=#])').click(function() {
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
2: You can use location.reload();.
You need to add ScrollTop function, and use it when you reach bottom.
$(document).ready(function () {
var myInterval = false;
myInterval = setInterval(AutoScroll, 5000);
function AutoScroll() {
var iScroll = $(window).scrollTop();
iScroll = iScroll + 500;
$('html, body').animate({
scrollTop: iScroll
}, 1000);
}
//The function scroll to 0 position.
function scrollTop()
{
$('html, body').animate({
scrollTop: 0
}, 1000);
}
$(window).scroll(function () {
var iScroll = $(window).scrollTop();
if (iScroll == 0) {
clearInterval(myInterval);
myInterval = setInterval(AutoScroll, 5000);
}
if (iScroll + $(window).height() == $(document).height()) {
clearInterval(myInterval);
//Instead refresh, I just scrolled to top.
setTimeout(scrollTop,5000)
}
});
});
I try to use the jQuery for my header animation, the animation slows down after I added:
else if (headeranimated && $(this).scrollTop() > 1200)
else if (headeranimated2 && headeranimated && $(this).scrollTop() < 1000)
I have to wait a couple of seconds for the second part of animation. Is there anything wrong with this code?
Thank you
// header animation
var headeranimated2 = false;
var headeranimated = false;
var headeranimated3 = false;
$(window).scroll(function() {
if ($(window).width() > 800) {
if (!headeranimated && $(this).scrollTop() > 500) {
$('#headerpattern').animate({
left: "-40%"
}, 800);
headeranimated = true;
} else if (headeranimated && $(this).scrollTop() > 1200) {
$('#headerpattern').animate({
top: "-20%"
}, 200);
headeranimated2 = true;
} else if (headeranimated2 && headeranimated && $(this).scrollTop() < 1000) {
$('#headerpattern').animate({
top: "0"
}, 200);
headeranimated2 = false;
headeranimated3 = true
} else if (headeranimated3 && !headeranimated2 && $(this).scrollTop() < 400) {
$('#headerpattern').animate({
left: "0"
}, 800);
headeranimated = false;
headeranimated3 = false;
}
} else {
if (!headeranimated && $(this).scrollTop() > 500) {
$('#headerpattern').animate({
top: "-8%"
}, 1200);
headeranimated = true;
} else if (headeranimated && $(this).scrollTop() < 400) {
$('#headerpattern').animate({
top: "0"
}, 800);
headeranimated2 = false;
}
}
});
well.. you are calling the scroll listener which occurs evry moment while you are scrolling. but you also play an animation which is relatevly slow to scroll. when you call the scroll listener by scrolling, you create multiple nimations calls which try to play all at once (and that is why it slows down the ui).
the solution: if animation still played - don't scroll
var animScroll;
$(window).scroll(function()
{
if (animScroll) return;
if ($(window).width() > 800)
{
if (!headeranimated && $(this).scrollTop() > 500)
{
animScroll = true;
$('#headerpattern').animate({ left: "-40%"}, 800, function()
{
animScroll = false;
});
headeranimated = true;
}
// rest of code
A scroll even is an event the is emitted continuously while scrolling, so it will be triggered multiple times a second while you are scrolling.
Whenever you call .animate for an element, the animation is added to a queue. And the animations are executed one after the other, in the order they where added to the queue. As your animations have a duration of in a range of 200 to 1200 you might result in an animation queue that has a duration of several seconds.
One solution would be to call .stop() before you call .animate:
$('#headerpattern').stop().animate(....)
But this might break your desired effect.
Another solution would be to check if there is an animation that is currently running and if so, then do not start a new animation. But this will have some kind of stop and go or delay effect.
I'm using the mousewheel and waypoints plugin to scroll sections of my page; The problem I am having is when I scroll using the apple mighty mouse the scrolling is too sensitive and the function gets triggered more then once when the animation is complete. I tried to set a timeout function and variable to check if the animation is complete but neither of these worked.
I would like to replicate an effect similar to the one on this website.
JQUERY
$('body').mousewheel(function(event, delta, deltaX, deltaY) {
clearTimeout(interval);
console.log('test');
$('section').waypoint(function(direction){
thisID = $(this);
},{ offset: '350' });
indexPos = thisID.index('section');
if (completed == true) {
completed = false;
var interval = "";
if (delta > 0) {
interval = setTimeout(function(){
if ($(this).not(":first-child")) {
//$(this).animate(function(){
$('html, body').stop().animate({
scrollTop: thisID.prev().offset().top - 200
}, 1000, 'swing' , function() { completed = true; });
//});
}else {
$('html, body').stop().animate({
scrollTop: thisID.offset().top - 200
}, 1000, 'swing' , function() { completed = true; });
}
},400);
}
else if (delta < 0) {
interval = setTimeout(function(){
if ($(this).not(":first-child")) {
$('html, body').stop().animate({
scrollTop: thisID.next().offset().top - 200
}, 1000, 'swing' , function() { completed = true; });
}
else {
$('html, body').stop().animate({
scrollTop: thisID.offset().top - 200
}, 1000, 'swing' , function() { completed = true; });
}
},400);
}
};
return false; // prevent default
});
I don't know what this is doing: indexPos = thisID.index('section'); but before doing anything, I would check if ins't anything in progress already:
$('body').mousewheel(function(event, delta, deltaX, deltaY) {
if($('html').is(':animated') || $('body').is(':animated')) return false;
// else, do your stuff...
});
You can use underscore js http://underscorejs.org/
and do something like this:
$('body').mousewheel(_.debounce(function() {
//handle the mouse wheel event in here
}, 30)
This will wait for 30 ms from the last mousewheel event before firing the callback
This website doesn't seem to use scrolling. It merely moves to a new anchor (watch the url when scrolling) which is triggered by moving (scrolling) your mouse up or down as a trigger which feels like lagged scrolling (but in fact, you don't have any control over the direction once it moves). You can use jquery animate to do that.