Out of view: Hide then trigger once on view

112 views Asked by At

Successful: The following code works. It will activate css (appear, animate, etc) when the div scrolls into view.

Problem: I only want it to trigger once (I don't want it to hide again or reset when you scroll past it).

Here's the code:

;(function($) {
  $.outOfView = {
      init: function() {
        $("[data-outofview]").css('transition', 'none').addClass('outofview');
        window.setTimeout(function(){
          $("[data-outofview]").css('transition', '');
          $.outOfView.scroll();
        }, 100);
      }
    , scroll: function() {
       var $window = $(window)
         , scrolled = $(window).scrollTop()
         , windowHeight = $(window).height();

       $("[data-outofview].outofview").each(function() {
         var $this = $(this)
           , dataCoefficient = $(this).data('outofview-coefficient')
           , coefficient = dataCoefficient ? parseInt(dataCoefficient, 10) / 100 : 1
           , windowHeightPadded = windowHeight * coefficient
           , offsetTop = $this.offset().top
           , offsetBottom = offsetTop + $this.outerHeight() * coefficient;

          // If the distance from the bottom of the element to the top of the document
          // is greater than the distance from the top of the window to the top of the
          // document, OR the distance from the top of the element is less than the distance
          // from the bottom of the window to the top of the document... remove the class.
          // The element is in view.
         if ( scrolled < offsetBottom || scrolled + windowHeightPadded > offsetTop) {
           if ($this.data('outofview-timeout')) {
             window.setTimeout(function() {
               $this.removeClass('outofview');
             }, parseInt($this.data('outofview-timeout'), 10));
           } else {
             $this.removeClass('outofview');
           }
         }
       });
       // Hidden...
       $("[data-outofview]:not(.outofview)").each(function () {
         var $this = $(this)
           , dataCoefficient = $(this).data('outofview-coefficient')
           , coefficient = dataCoefficient ? parseInt(dataCoefficient, 10) / 100 : 1
           , windowHeightPadded = windowHeight * coefficient
           , offsetTop = $this.offset().top
           , offsetBottom = offsetTop + $this.outerHeight() * coefficient;

          // If the distance from the bottom of the element to the top of the document
          // is less than the distance from the top of the window to the top of the
          // document, OR the distance from the top of the element is greater than the distance
          // from the bottom of the window to the top of the document... add the class.
          // The element is out of view.
         if ( scrolled > offsetBottom || scrolled + windowHeightPadded < offsetTop) {
           $(this).addClass('outofview');
         }
       });
      }
  };

  // Reveal animations as they appear on screen
  $(window).on('scroll', $.outOfView.scroll);
  $.outOfView.init();
})(jQuery);

Any assistance you can provide would be greatly appreciated. While I have customised code before, I'm not an expert.

2

There are 2 answers

0
bellabelle On

Try visit this link Popup on website load once per session

I think that is what you need with the used of sessionStorage, I'ved tried this before and works fine.

0
Bainn On

I wasn't able to 100% resolve this problem, but I do have a workable solution.

At the heart of it, I didn’t want this hiding the div again, once the item was out of view (scrolled past). As a result, I removed the following code…

scrolled > offsetBottom ||

This essentially means the script will trigger on the down scroll only. It will not hide/trigger the element again unless you are scrolling down the page from above the div.