jQuery: Scroll down page a set increment (in pixels) on click?

前端 未结 6 499
既然无缘
既然无缘 2020-12-07 18:48

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

6条回答
  •  没有蜡笔的小新
    2020-12-07 18:53

    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);
    }
    
    

提交回复
热议问题