LoginSignup
0
1

【javascript】【jquery】スクロール

Posted at

コード

    $(window).scroll(() =>{
        let scrollTop = $(window).scrollTop(); // スクロール上部の位置
        let areaTop = $("h2").offset().top; // 対象エリアの上部の位置
        let areaBottom = areaTop + $("h2").innerHeight(); // 対象エリアの下部の位置
        let windowHeight = $(window).height();  //ウィンドウの高さ
        // ↓要素がページの1番上を通過した場合
        // if (scrollTop > areaTop && scrollTop < areaBottom) 
        // ↓要素がページの1番下から見えた場合
        if (scrollTop > (areaTop - windowHeight + windowHeight/6)) {
            console.log('何らかの処理');
        } 
//「windowHeight/6」を最後に足して要素の可視範囲からウィンドウの高さ1/6の高さ分スクロールしたら表示
    });

0
1
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
1