$(function () { var $navbar = $('[data-navbar]'); var $logo = $('[data-float-logo]'); var showFlag = getNeedShow(); function getNeedShow() { return (window.scrollY >= $navbar.outerHeight(true)); } function checkVisibility() { var needShow = getNeedShow(); if (needShow != showFlag) { showFlag = needShow; $logo.toggleClass('show', needShow); } } var callback = _.throttle(checkVisibility, 100); $(window).on('scroll', callback); $(window).on('resize', callback); });