jroakes

image_load

Jul 15th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. function showNotification(){
  4.  
  5.     var preItem = document.querySelector(".details__separator")
  6.  
  7.     var tooltip = document.createElement('p');
  8.     tooltip.id = "tooltip";
  9.     tooltip.style.cssText = "color: #5aa2f4; width:100%; text-align:center; padding:20px 10px; font-weight:bold;";
  10.     tooltip.innerHTML = "Interactive form loaded. Try filling out below.";
  11.  
  12.     preItem.after(tooltip);
  13.  
  14. }
  15.  
  16.  
  17.  
  18. // Only start when a scroll event is detected.
  19. window.addEventListener('scroll', function(e) {
  20.  
  21.     // This is the target which is observed.
  22.     var target = document.querySelector('#imageTemplate');
  23.  
  24.     if (target) {
  25.  
  26.         console.log("Observing target.")
  27.  
  28.         // Observer.
  29.         var observer = new IntersectionObserver(onIntersection,{
  30.             root: null,
  31.             rootMargin: '30px',
  32.             threshold: 0.1
  33.         });
  34.  
  35.         // Start observing.
  36.         observer.observe(target);
  37.  
  38.         // Catches visibility changed
  39.         function onIntersection(entries) {
  40.             entries.forEach(entry=>{
  41.                 console.log(entry.intersectionRatio)
  42.                 target.classList.toggle('visible', entry.intersectionRatio > 0.5);
  43.  
  44.                 // Are we in viewport?
  45.                 if (entry.intersectionRatio > 0.5) {
  46.                     // Stop watching
  47.                     console.log('In View. Switching Form.')
  48.                     switchToFormTemplate();
  49.                     showNotification();
  50.                     observer.unobserve(entry.target);
  51.                     entry.target.remove();
  52.                 }
  53.             }
  54.             );
  55.         }
  56.  
  57.     }
  58.  
  59. }, true);
Add Comment
Please, Sign In to add comment