0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

スムーススクロール(ページ内移動)

Posted at

//ページ内リンクのURLが "#"から始まる時(ex. "#service"や"#about")

script.js
  $('a[href^="#"]').click(function () {
    // スクロールの速度
    var speed = 400; // ミリ秒で記述
    var href = $(this).attr('href');
    var target = $(href === '#' || href === "" ? 'html' : href);
    var position = target.offset().top;
    $('body,html').animate({ scrollTop: position }, speed, 'swing');
    return false;
  });

//ページ内リンクのURLが "/#"から始まる時(ex. "/#service"や"/#about")

script.js
$(document).on('click', 'a[href*="#"]', function() {
  let time = 400;
  let target = $(this.hash);
  if (!target.length) return;
  let targetY = target.offset().top;
  $('html,body').animate({scrollTop: targetY}, time, 'swing');
  return false;
});
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?