77

I'm trying to make a page scroll down 150px from the current position when an element is clicked. So lets say you're roughly halfway scrolled down a page. You click this link, and it will slide you down an additional 150 pixels.

Is this possible with jQuery?

I've been looking at scrollTop and the scrollTo plugin, but I can't seem to connect the dots.

bcWeb
  • 961
  • 1
  • 8
  • 7
  • Love to see the code you're working with, it may be most of the way there and there's just a simple problem. – Chuck Vose Apr 17 '10 at 17:48
  • I don't have code to show yet. As an alternate solution, I'm scrolling down to a specific #ID, but I'm trying to make this more dynamic by scrolling down 150px from where you were at. – bcWeb Apr 19 '10 at 13:21

6 Answers6

122
var y = $(window).scrollTop();  //your current y position on the page
$(window).scrollTop(y+150);
Mohamed Hafez
  • 8,621
  • 7
  • 41
  • 49
  • 29
    Thanks, this worked for me.. for a kind of page down effect.  `var y = $(window).scrollTop(); $("html, body").animate({ scrollTop: y + $(window).height() }, 600); ` – The HCD Apr 03 '13 at 14:11
  • **Add** to move the scrollbar down, which moves the page up, to see a lower part of the page. **Subtract** to move the scrollbar up, which moves the page down to see a higher part of the page. (I believe the only people who get this right the first time are those who make TWO mistakes in their thinking, and they cancel out.) – Bob Stein Sep 02 '20 at 14:45
51

Just check this:

$(document).ready(function() {
    $(".scroll").click(function(event){
        $('html, body').animate({scrollTop: '+=150px'}, 800);
    });
});

It will make scroller scroll from current position when your element is clicked

And 150px is used to scroll for 150px downwards

Yatin Khullar
  • 1,580
  • 1
  • 12
  • 26
8

You can do that using animate like in the following link:

http://blog.freelancer-id.com/index.php/2009/03/26/scroll-window-smoothly-in-jquery

If you want to do it using scrollTo plugin, then take a look the following:

How to scroll the window using JQuery $.scrollTo() function

Community
  • 1
  • 1
Mahesh Velaga
  • 21,633
  • 5
  • 37
  • 59
  • These both seem to be set increments from the top of the browser, not from the point at when you are currently viewing. – bcWeb Apr 19 '10 at 13:22
3

You might be after something that the scrollTo plugin from Ariel Flesler does really well.

http://demos.flesler.com/jquery/scrollTo/

JVDL
  • 1,157
  • 9
  • 16
  • the **relative px** version of the **_scrollTo plugin_** mentioned here should do the trick perfectly. the **relative px(hash)** method will allow you to spec a dif **_left_** from **_top_** value. – Jeremy Oct 29 '15 at 22:18
1

Pure js solution for newcomers or anyone else.

var scrollAmount = 150;
var element = document.getElementById("elem");

element.addEventListener("click", scrollPage);

function scrollPage() {
    var currentPositionOfPage = window.scrollY;
    window.scrollTo(0, currentPositionOfPage + scrollAmount);
}

Martin54
  • 1,349
  • 2
  • 13
  • 34
0

Updated version of HCD's solution which avoids conflict:

var y = $j(window).scrollTop(); 
$j("html, body").animate({ scrollTop: y + $j(window).height() }, 600);
Pixelomo
  • 6,373
  • 4
  • 47
  • 57