First, few improvement to your code, if window size just changing above 1024 (ex: 1300px changed to 1250px) or below 1024 (600px changed to 700px) you don't need to do anything, it is only the point you cross the 1024px you need to call the method.
On init, lastWinWidth = 0, so curWinWidth >= 1024 && lastWinWidth == 0 will be true if on page load your window width more than 1024.
So this will do the trick:
if ((curWinWidth >= 1024 && lastWinWidth < 1024) || (curWinWidth >= 1024 && lastWinWidth == 0)) { } else if ((curWinWidth < 1024 && lastWinWidth >= 1024) || (curWinWidth < 1024 && lastWinWidth == 0)) { } lastWinWidth = curWinWidth;
Also jQuery allowed you to do multiple-selector so you can combine your code into one line:
$('#business_image, #business_description').fadeIn('500');
Add $('#sport, #business').off(); to the else statement so remove the hover events for both #sport and #business.
jQuery
.off()
Description: Remove an event handler.
REF: http://api.jquery.com/off/
As the comments said, try to clean up your code. My solution will only help you remove the events when the window width < 1024.
$(document).ready(function() { var lastWinWidth = 0; $(window).resize(resizeHandler); function resizeHandler() { var curWinWidth = $(window).width(); if ((curWinWidth >= 1024 && lastWinWidth < 1024) || (curWinWidth >= 1024 && lastWinWidth == 0)) { console.log('Window Width >= 1024 now'); $('#business').hover(function() { $('#business_image, #business_description').fadeIn('500'); }, function() { $('#business_image, #business_description').fadeOut('500'); }); $('#sport').hover(function() { $('#sport_image, #sport_description').fadeIn('500'); }, function() { $('#sport_image, #sport_description').fadeOut('500'); }); } else if ((curWinWidth < 1024 && lastWinWidth >= 1024) || (curWinWidth < 1024 && lastWinWidth == 0)) { console.log('Window width < 1024 now'); $('#sport, #business').off(); $('#business_image, #business_description, #sport_image, #sport_description').hide(); } lastWinWidth = curWinWidth; } //init $('#business_image, #business_description, #sport_image, #sport_description').hide(); resizeHandler(); });
#business, #sport { display: inline-block; background: aqua; margin: 10px; width: 150px; height: 100px; float: left; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <br><br><br> <div id="business">business <div id="business_image">business_image</div> <div id="business_description">business_description</div> </div> <div id="sport">sport <div id="sport_image">sport_image</div> <div id="sport_description">sport_description</div> </div>