back-top.js 823 Bytes
/**
 *  back to top
 *  @author qi.li <qi.li@yoho.cn>
 *
 */

const $ = require('jquery');

(function backTop() {
    const $doc = $(document);
    const $win = $(window);

    let dom;
    let $btn;
    let show;

    dom = `<div class="back-top">
            <i class="icon icon-back-top1"></i>
        </div>`;

    $doc.find('body').append(dom);
    $btn = $('.back-top');
    $btn.click(function() {
        $('html,body').animate({scrollTop: 0}, 500);
    });

    $doc.scroll(function() {
        let top = $(this).scrollTop();

        if (top > $win.height()) {
            if (!show) {
                $btn.fadeIn('slow');
                show = true;
            }
        } else {
            if (show) {
                $btn.fadeOut('slow');
                show = false;
            }
        }
    });
}());