Disable (but still allow) scrolling - javascript

I'm looking to recreate the scrolling effect on this site: misfitwearables.com. So far, my approach has been to disable user scrolling, but still detect the scroll direction to allow jQuery to control how the page scrolls. The thing is that it works (yay!) .. but it only works on the first scroll. After the first scroll, it stops responding. How can I get it to listen/respond on every scroll?
$(this).bind( 'mousewheel', function ( e ) {
if (e.originalEvent.wheelDelta < 0) {
// scroll down
$("html, body").animate({ scrollTop: (+50) }, 900, "easeInOutQuart" );
} else {
// scroll up
$("html, body").animate({ scrollTop: (-50) }, 900, "easeInOutQuart" );
}
return false;
});

The animating property value is incorrect, should be "+=50" and "-=50":
$(this).bind( 'mousewheel', function ( e ) {
if (e.originalEvent.wheelDelta < 0) {
// scroll down
$("html, body").animate({ scrollTop: "+=50" }, 900, "easeInOutQuart" );
} else {
// scroll up
$("html, body").animate({ scrollTop: "-=50" }, 900, "easeInOutQuart" );
}
return false;
});
It was only working once because you were always passing 50 and -50 as the value.
Here's a working fiddle, I also added a check to perform only one animation per scroll event

Related

When I scroll down a bit I want it to scroll below the slider

I am trying to make a slider and when I scroll down I want it to jump into the content
I tried this but it doesn't feel smooth
var up = 0;
$(window).scroll(function()
{
if ($(window).scrollTop()> 50 && up==0)
{
$("html, body").animate({ scrollTop: (
($(".vc_custom_1449248739486").offset().top)-50) }, 500);
up++;
}
if( $(window).scrollTop()<20)
{
up=0;
}
});
I want to scroll and to slide it down to the content.
You can check the code and inspect it here:
http://junglelodge.nevadastudios.com/
You can try this for smooth scrolling:
window.scrollTo({
top: document.querySelector(".vc_custom_1449248739486").offsetTop - 50,
left: 0,
behavior: 'smooth'
});

preventing from animating further on some event

I've got this code here:
$(document).ready(function()
{
$("#nav_items > p:first-child").click(function()
{
$('html,body').animate(
{
scrollTop: $('#main_div').offset().top
}, 500);
});
$("#nav_items > p:last-child").click(function()
{
$('html,body').animate(
{
scrollTop: $('#about_us').offset().top
}, 800);
});
});
On element(p) click it scrolls the document to a #main_div or #about_us element. How can I stop it from keep on scrolling if I for example start scrolling with my mouse wheel?
You can listen to the mousewheel event and use the stop method:
$(window).on('mousewheel', function() {
$('body, html').stop();
});
Here is a method, combining the use of $(window).scroll() and $('body').on('mousewheel'), that will demonstrate how to do what you wish:
jsFiddle Demo
var scrollPause = 0;
menuItems.click(function(e){
var href = $(this).attr("href"),
offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight+1;
scrollPause = 1;
$('html, body').stop().animate({
scrollTop: offsetTop
}, 300, function(){
setTimeout(function(){
scrollPause = 0;
},5000);
});
e.preventDefault();
});
$('body').on({
'mousewheel': function(e) {
if (scrollPause == 0) return;
e.preventDefault();
e.stopPropagation();
}
})
Notes:
In the jsFiddle, the sp div is used to visually show status of the scrollPause variable
Upon clicking a top menu item, the scrollPause is set to 0 (disallow scroll) and a setTimeout is used to re-enable it after an 8-second pause. Therefore, immediately after the scroll-to-element, mouse wheel scroll will be disabled for 8 seconds.

slideToggle animates "show" but not "hide"

I have a website with 100% height that has a hidden footer, that needs to slide up and show it when a button is clicked, and when that button is clicked again, it should slide down and hide it.
The problem is that the sliding animation is only working when the footer slides up, and when it should slide down, it bumps without animation.
You can see the problem right here, by clicking on the "More" button in the footer.
The JS code used to manipulate that button is the following:
$(document).ready(function(){
$(".footer_container").hide();
$(".show_hide").show();
$('.show_hide').click(function(){
var speed = "500";
$(".footer_container").slideToggle(speed);
$('html, body').animate({
scrollTop: $(document).height()
}, speed);
});
});
Thanks in advance!
Update: I just tried this code:
$('.show_hide').click(function(){
var speed = "500";
$(".footer_container").toggle(speed);
$('html, body').animate({
scrollTop: $(".footer_container").offset().top + $('window').height()
}, speed);
});
And aparently there's an animation going on the footer that I didn't know exist. Maybe that's the cause of this problem?
alright so i gave this a shot:
$('.show_hide').unbind()
$('.show_hide').click(function () {
var speed = "500";
$(".footer_container").toggle(speed);
if ($(".footer_container").data('can-see')) {
var displaced = $('.footer_container').height();
$('.twitter_footer').animate({
marginTop: "600px",
}, {
duration: speed,
complete: function () {
$('.twitter_footer').css('margin-top', "0");
}
});
}
$('html, body').animate({
scrollTop: $(".footer_container").offset().top + $('window').height()
}, speed);
$(".footer_container").data('can-see', !$(".footer_container").data('can-see'))
});
demonstration at http://jsfiddle.net/DPq5Z/
same result, another way (using absolute positioning in order to keep elements above undisturbed):
$('.show_hide').unbind()
$('.show_hide').click(function () {
var speed = "500";
$(".footer_container").fadeToggle(speed);
if ($(".footer_container").data('can-see')) {
slide_down('.twitter_footer', speed);
slide_down('.button_bg', speed);
}
$('html, body').animate({
scrollTop: $(".footer_container").offset().top + $('window').height()
}, speed);
$(".footer_container").data('can-see', !$(".footer_container").data('can-see'))
});
function slide_down(c, speed){
var tp = $(c).offset().top;
$(c).css({
'position': 'absolute',
'top': tp + "px"
});
$(c).animate({
top: tp + 170 + "px",
}, {
duration: speed,
complete: function () {
$(c).css({
'position': "relative",
'top': '0'
});
}
});
}
demonstration at http://jsfiddle.net/9R6L4/
It works as how default animations in jQuery work. If you want to customize this. You need to use jQuery easing plugin. It takes as parameter the easing effect, like easeIn, easeOut, Bounce etc.. that controls the flow. By default it is linear and that is what you see.
Easing Plugin: https://github.com/gdsmith/jquery.easing
$('.show_hide').click(function(){
var speed = "500";
$(".footer_container").fadeToggle(speed);
$('html, body').animate({
scrollTop: $(".footer_container").offset().top + $('window').height()
}, speed);
});
jsFiddle: http://jsfiddle.net/vvmYH/4/

Localscroll toggle

For many sites (including mine) it would really be useful if we could use jQuery localscroll to send someone to the end of the site, and then when they reach the end, reverse the direction, so the next click would take you back up to the top of the site.
Has anyone done something like this? Any ideas on how to accomplish it (I'm a js newbie).
Well this code may do what you want? DEMO
var bottom = false;
$('body').delegate('span', 'click', function() {
if (bottom === true) {
$("html, body").animate({
scrollTop: '0px'
}, "slow");
bottom = false;
} else {
$("html, body").animate({
scrollTop: $(document).height()
}, "slow");
bottom = true;
}
});

JQuery stop element from fading back in

I've made this little snippet to scroll the window to the top of the page.
$(window).scroll(function(){
$("#scrollup").fadeIn("slow");
});
$("#scrollup").click(function(){
$('html, body').animate({ scrollTop: 0 }, 'normal', function() {
$("#scrollup").fadeOut("slow");
});
});
However, when the scrollup div fades out after the window scrolls, it fades back in. How do I stop this from happening? Thanks.
I think I have found a reasonable solution
$(window).scroll(function(){
if ($(this).scrollTop() > 100) {
$('.scrollup').fadeIn();
} else {
$('.scrollup').fadeOut();
}
});
Would this be easier than changing my original code?
Check out .stop(): http://api.jquery.com/stop/
$(window).scroll(function(){
if ($(this).scrollTop() > 100) {
$('.scrollup').stop().fadeIn();
} else {
$('.scrollup').stop().fadeOut();
}
});
You should make sure that the scrollup div isn't faded in when you are at the top or when it is already faded in ( visible )
$(window).scroll(function(){
if ( $(window).scrollTop() !== 0 or $("#scrollup").is(":hidden") ) then {
$("#scrollup").fadeIn("slow");
}
});
You could just add an if statement to check if it is at the top:
$(window).scroll(function(){
if($("body").scrollTop()!=0)
$("#scrollup").fadeIn("slow");
});
$("#scrollup").click(function(){
$('html, body').animate({ scrollTop: 0 }, 'normal', function() {
$("#scrollup").fadeOut("slow");
});
});​

Categories