user2984539
user2984539

Reputation: 23

Change 'resize' to 'onload' JS

Currently, this script only works when I adjust the window - I'd like it to work on load but do not know-how. I think this is simple but I can't figure it out.

      $(window).on('resize', function() {
if($(window).height() > 300) {
    $('.card').addClass('hover');
    $('.card').removeClass('no');
}else{
    $('.card').addClass('no');
    $('.card').removeClass('hover');
}

})

Upvotes: 0

Views: 138

Answers (1)

gugateider
gugateider

Reputation: 2049

Just create a function and execute it onload and as a event listener of the onresize

function resizeHandler() {
  if ($(window).height() > 300) {
    $('.card').addClass('hover');
    $('.card').removeClass('no');
  } else {
    $('.card').addClass('no');
    $('.card').removeClass('hover');
  }
}

$(window).on('resize', resizeHandler);
$(document).ready(resizeHandler);

Upvotes: 1

Related Questions