sunlanda
11/28/2016 - 1:36 PM

置顶按钮js效果

置顶按钮js效果

var throttle = function (fn, delay) {
    var timer = null;
    return function () {
        var context = this, args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function () {
            fn.apply(context, args);
        }, delay);
    };
};

var $top = $('.mod_hang_top');
var $win = $(window);
function topShow() {
    if ($win.scrollTop() > $win.height()) {
        $top.show();
    } else {
        $top.hide();
    }
};

window.onscroll = throttle(topShow, 100);

$top.on({
    click: function () {
        $('body,html').animate({
            scrollTop: 0
        }, 800)
    }
})