LoginSignup
0
0

More than 1 year has passed since last update.

横に長いWebページで、右端までスクロールされたかを判定する【JavaScript】

Posted at

もしかしたら他の方が記事にしていらっしゃるかもしれませんが、自分用メモとして。
最下部スクロールなどの記事はよく見かけるのですが、右端スクロールはあまり見ないので。

var lastScrollx = 0;

$(function() {

    lastScrollx = window.pageXOffset;

    window.addEventListener('scroll', function(event) {
        var direction = 'vertical';
        var scrollValue = window.pageXOffset;

        if (lastScrollx != scrollValue) {
            if (lastScrollx > scrollValue) {
                direction = 'left';
            } else {
                direction = 'right';
                var docW = $(document).innerWidth();
                var winW = $(window).innerWidth();
                var maxScrollValue = docW - winW;
                if (scrollValue == maxScrollValue) {
                    console.log('右端に到達');
                }
            }
        } else {
            direction = 'vertical';
        }
        lastScrollx = scrollValue;
    });
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0