JAVASCRIPT   30
viewportchecker
Guest on 8th March 2023 12:13:24 PM


  1. /*
  2.     Version 1.3.2
  3.     The MIT License (MIT)
  4.  
  5.     Copyright (c) Dirk Groenen
  6.  
  7.     Permission is hereby granted, free of charge, to any person obtaining a copy of
  8.     this software and associated documentation files (the "Software"), to deal in
  9.     the Software without restriction, including without limitation the rights to
  10.     use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11.     the Software, and to permit persons to whom the Software is furnished to do so,
  12.     subject to the following conditions:
  13.  
  14.     The above copyright notice and this permission notice shall be included in all
  15.     copies or substantial portions of the Software.
  16. */
  17.  
  18. (function($){
  19.     $.fn.viewportChecker = function(useroptions){
  20.         // Define options and extend with user
  21.         var options = {
  22.             classToAdd: 'visible',
  23.             offset: 100,
  24.             callbackFunction: function(elem){}
  25.         };
  26.         $.extend(options, useroptions);
  27.  
  28.         // Cache the given element and height of the browser
  29.         var $elem = this,
  30.             windowHeight = $(window).height();
  31.  
  32.         this.checkElements = function(){
  33.             // Set some vars to check with
  34.             var scrollElem = ((navigator.userAgent.toLowerCase().indexOf('webkit') != -1) ? 'body' : 'html'),
  35.                 viewportTop = $(scrollElem).scrollTop(),
  36.                 viewportBottom = (viewportTop + windowHeight);
  37.  
  38.             $elem.each(function(){
  39.                 var $obj = $(this);
  40.                 // If class already exists; quit
  41.                 if ($obj.hasClass(options.classToAdd)){
  42.                     return;
  43.                 }
  44.  
  45.                 // define the top position of the element and include the offset which makes is appear earlier or later
  46.                 var elemTop = Math.round( $obj.offset().top ) + options.offset,
  47.                     elemBottom = elemTop + ($obj.height());
  48.  
  49.                 // Add class if in viewport
  50.                 if ((elemTop < viewportBottom) && (elemBottom > viewportTop)){
  51.                     $obj.addClass(options.classToAdd);
  52.  
  53.                     // Do the callback function. Callback wil send the jQuery object as parameter
  54.                     options.callbackFunction($obj);
  55.                 }
  56.             });
  57.         };
  58.  
  59.         // Run checkelements on load and scroll
  60.         $(window).scroll(this.checkElements);
  61.         this.checkElements();
  62.  
  63.         // On resize change the height var
  64.         $(window).resize(function(e){
  65.             windowHeight = e.currentTarget.innerHeight;
  66.         });
  67.     };
  68. })(jQuery);

Raw Paste

Login or Register to edit or fork this paste. It's free.