return-top.js 1.1 KB
/**
 * [公共底部]返回页面顶部
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/07/05
 */

var $ = require('yoho-jquery');

var $returnTop = $('.return-top');

/**
 * 是否显示返回顶部按钮
 */
function showOrNot() {
    if ($(window).height() >= $(document).height()) {
        $returnTop.addClass('hide');
    }
}

function reposReturnTop() {
    var $top = $returnTop.parent();

    if (!$top.hasClass('service-top')) {
        $top = $returnTop;
    }
    if ($(window).width() < 1380) {
        $top.addClass('for-min');
    } else {
        $top.removeClass('for-min');
    }
}

$returnTop.click(function() {
    $('html,body').animate({
        scrollTop: 0
    }, 500);
});

$(window).scroll(function() {
    if ($(window).scrollTop() === 0) {
        $returnTop.addClass('hide');
    } else {
        $returnTop.removeClass('hide');
    }
});

// 页面图片加载完成后根据页面高度决定是否显示
if ($returnTop.hasClass('hide')) {
    $('img').load(showOrNot);
}

reposReturnTop();

if ($returnTop.length) {
    $(window).resize(reposReturnTop);
}

exports.returnTopShowOrNot = showOrNot;