LoginSignup
0
0

More than 3 years have passed since last update.

jQuery スクロール

Posted at

animateを用いた記述

$(function () {
  // 上に戻るボタンの初期化
  let topBtn = $('#scrollTop');
  topBtn.hide();

  // ある程度スクロールされたら、上に戻るボタンを表示する
  $(window).scroll(function () {
    if ($(this).scrollTop() > 200) {
      topBtn.fadeIn(); // フェードインで表示
    } else {
      topBtn.fadeOut();  // フェードアウトで非表示
    }
  });

  // クリックで上に戻るボタン
  topBtn.click(function (event) {
    event.preventDefault();
    $('body,html').animate({
      scrollTop: 0
    }, 500);
  });
});


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