问题
If the window width on page load AND resize is less than 768px, I don't want to fire the showCover()
function. With the below code, even when the window is less than 768px, it's still being fired.
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');
});
}
回答1:
I would go the other way around:
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');
}
});
});
来源:https://stackoverflow.com/questions/20941597/if-window-is-less-than-768px-wide-dont-fire-function