如果窗口宽度小于768px,请勿触发function

如果页面加载和resize的窗口宽度小于768px,我不想showCover()函数。 使用下面的代码,即使窗口小于768px,它仍然被触发。

  function ipsThemeViewer() { jQuery(window).resize(function() { if ( jQuery(window).width() < 768 ) return false; showCover(); }).resize(); } function showCover() { jQuery('#ipsThemeViewerScreen').hover(function () { var t = jQuery(this); jQuery('.cover').stop().fadeIn('fast'); }, function () { var t = jQuery(this); jQuery('.cover').stop().fadeOut('fast'); }); } 

我会反过来说:

 jQuery(function($) { // DOM READY AND SECURE $ ALIAS var winIsSmall; function testWinSize(){ winIsSmall= $(window).width() < 768; // BOOLEAN } $(window).on("load resize", testWinSize); $('#ipsThemeViewerScreen').hover(function () { if(winIsSmall){ // need something here? }else{ $('.cover').stop().fadeToggle('fast'); } }); });