Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery scrollLeft not working

I just launched http://elliewauters.com & have a little problem that I'd like to fix, the site uses a lot of animated horizontal scrolling to go from one 'page' to the next, but on refresh most browsers remember the scroll position & go back to where I was before, I do not want this. Try going to the 'About' page & then refreshing. You'll see that the logo & menu are in the middle of the page as they where when you arrived at the site for the first time.

I would like help with either of two things:

  1. To reset the scroll position back to 0,0 on page refresh OR
  2. To move the logo & menu to the top if the scrollLeft value is greater than 1.

I have tried using $(window).scrollLeft(0); to no avail &

scrollLeft = $(window).scrollLeft();
console.log(scrollLeft)
if (scrollLeft>1) {
    $('#header').addClass('notLeft').css('top','0%');
} else {
    $('#header').addClass('left').css('top','25%');
}

does not work, does anyone please have an idea of how I could achieve what I want?

Thanks in advance

like image 456
Zander Avatar asked May 31 '26 02:05

Zander


2 Answers

Well Firefox wasn't working because of the console.log(scrollLeft) so it should work after removing that. As for the problem you are having, I'm guessing it is only happening in IE? The solution is to not use $(window) but instead replace it with $(document). So use:

scrollLeft = $(document).scrollLeft();
like image 149
Mottie Avatar answered Jun 01 '26 17:06

Mottie


I know this is an old thread, but .scrollLeft() in jQuery didn't work for me on Android, too, under jQuery 1.9.1 today (April, 2013). Try this:

window.scrollTo(x, y);

This worked for me on all recent platforms and OS's, including Android phones.

like image 40
SellRex Avatar answered Jun 01 '26 17:06

SellRex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!