美文网首页
滚动到顶部

滚动到顶部

作者: _朽木 | 来源:发表于2018-04-12 13:41 被阅读0次
$('.totop').toTop({
    autohide: true,//是否自动隐藏
    offset: 420,//距离顶部的高度
    speed: 500,//速度
    position: true,//定位
    right: 0,
    bottom: 30
});


(function ($) {
    'use strict';

    $.fn.toTop = function (opt) {

        //variables
        var elem = this;
        var win = $(window);
        var doc = $('html, body');

        //Extended Options
        var options = $.extend({
            autohide: true,
            offset: 420,
            speed: 500,
            position: true,
            right: 15,
            bottom: 30
        }, opt);

        elem.css({
            'cursor': 'pointer'
        });

        if (options.autohide) {
            elem.css('display', 'none');
        }

        if (options.position) {
            elem.css({
                'position': 'fixed',
                'right': options.right,
                'bottom': options.bottom,
            });
        }

        elem.click(function () {
            doc.animate({ scrollTop: 0 }, options.speed);
        });

        win.scroll(function () {
            var scrolling = win.scrollTop();

            if (options.autohide) {
                if (scrolling > options.offset) {
                    elem.fadeIn(options.speed);
                }
                else elem.fadeOut(options.speed);
            }

        });

    };

}(jQuery));

相关文章

网友评论

      本文标题:滚动到顶部

      本文链接:https://www.haomeiwen.com/subject/xgvxkftx.html