JSFiddle - React, Tailwind, and code Playground

by Twisty

HTML

There is an orange div lower on the screen. Click any box to alert if the orange box is within the visible viewport.

<div class="box orange" id="boxorange"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>

CSS

.box {
  width: 200px;
  height: 200px;
  margin-bottom: 10px;
  background: blue;
}

.orange {
  background: orange;
}

JavaScript

$.fn.isOnScreen = function() {

  var win = $(window);

  var viewport = {
    top: win.scrollTop(),
    left: win.scrollLeft()
  };
  viewport.right = viewport.left + win.width();
  viewport.bottom = viewport.top + win.height();

  var bounds = this.offset();
  bounds.right = bounds.left + this.outerWidth();
  bounds.bottom = bounds.top + this.outerHeight();

  return (!(viewport.right < bounds.left || viewport.left > bounds.right || viewport.bottom < bounds.top || viewport.top > bounds.bottom));

};

$(window).on("wheel", function(e) {
  if ($('#boxorange').isOnScreen() == true) {
    if (e.originalEvent.wheelDelta / 120 > 0) {
      console.log('Wheel Up');
    } else {
      console.log('Wheel Down');
    }
    $('#boxorange').css({
      "height": "400px"
    });
    $('#boxorange').css({
      "width": "400px"
    });
  } else {
    $('#boxorange').css({
      "height": "200px"
    });
    $('#boxorange').css({
      "width": "200px"
    });
  }
});