LoginSignup
0
0

More than 5 years have passed since last update.

offsetのメモ

Posted at

offset()に関してのメモ

実装上でちょっとハマったのでメモがわりとして

実装

特定のページで固定フッターCTAを設置

エラー

目的のページ以外でUncaught TypeError: Cannot read property 'top' of undefinedと表示させる

解決

(function () {
  $(window).scroll( function(){
    var $target = $('.target')
    //ここで判定
    if ($target.length) {
      var $ele = $('.cta');
      var $pos = $('.box').offset().top;
      var $win = $(window).scrollTop();

      if( $win > $pos ){
        $($ele).fadeIn(1000)
      }else{
        $($ele).fadeOut(1000)
      }
    }
  });
})();

まとめ

offsetでごにょるときは、すごい人ならもっとスッキリ描くのでしょうが、一旦メモとして記録

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