Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
0
Help us understand the problem. What are the problem?

More than 3 years have passed since last update.

jqueryでページトップを使用する(例)

ページトップのJSを使用する場合(例)

定義側js↓

pageTop.js
$(window).load(function(){
  function pageTop () {

    var $main         = $('html, body');
    var $pTop         = $('.pagetop');
    var $header       = $('header');
    var $scrollPos    = '';
    var $headerHeight = $header.outerHeight();
    var $headerOfset  = $headerHeight + $header.offset().top;


    //ページトップボタン非表示
    $pTop.hide();

    $(window).on('scroll', function() {

        $scrollPos = $(this).scrollTop();

        if ($headerOfset < $scrollPos) {
            $pTop.fadeIn();
        } else {
            $pTop.fadeOut();
        }

    });

    $pTop.on('click', function() {

        $main.animate({
            scrollTop:$main.offset().top
        }, 2000, "easeOutQuint");

        return false;

    });

  }
});

呼び出し側js↓

common.js
$(window).load(function(){
  pageTop();
});

定義側html↓

index.html
<script src="./js/jquery-1.11.0.min.js"></script>
<script src="./js/jquery.easing.1.3.js"></script>
<script src="./js/pageTop.js"></script>

jquery.easing.1.3.jsは別に読まなくてもページトップのjsを使用できるが、easingを使用する際は必須。
もし、必要ないなら、「easeOutQuint」を消す。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
0
Help us understand the problem. What are the problem?