jQuery - bind listeners as one

0 votes
283 views
added Jul 9, 2019 in jQuery by LC Marshal Captain (25,790 points)
function functionTop(element) {
  return $(element).offset().top >= $(window).scrollTop() && $(element).offset().top <= $(window).scrollTop() + $(window).height();
 }

// instead of one by one

// Load
$(window).load(function () {
  functionTop($('myelement'));
});

// Resize
$(window).resize(function () {
   functionTop($('myelement'));
});

// Scroll
$(window).scroll(function () {
  functionTop($('myelement'));
});

// Combine them all using on
$(window).on("load resize scroll",function(e){
    functionTop($('myelement'));
});

 

1 Response

0 votes
responded Jul 9, 2019 by LC Marshal Captain (25,790 points)
$(window).on('load scroll resize', function() {
  var heades = $('.header'),
      heroes = $('.hero'),
      maines = $('.main');
  if (window.innerWidth > 1199 && window.outerWidth > 1199) {
    if ($(this).scrollTop() > parseInt(maines.innerHeight() - heroes.innerHeight())) {
      heroes.addClass('sticky');
      heroes.css('top', parseInt(maines.innerHeight() - heroes.innerHeight() + heades.innerHeight()) + 'px');
    } else {
      heroes.removeClass('sticky');
      heroes.removeAttr('style');
    }
  } else {
    heroes.removeClass('sticky');
    heroes.removeAttr('style');
  }
});

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...