2017-08-29 7 views
1

成功:次のコードが機能します。 divが表示にスクロールすると、css(表示、アニメーションなど)が有効になります。ビューから外してください:ビューで一度トリガーしないでください

問題:私はそれを1回だけトリガしたい(私はそれを過ぎてスクロールするときに再び隠れるかリセットしたくない)。あなたが提供することができますどのような援助がいただければ幸いです

;(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); 

は、ここでは、コードです。以前はコードをカスタマイズしていましたが、私は専門家ではありません。

+0

**フラグ**の値を 'false'にして、**フラグ**の値を' true'に変更し、コード内の同じ条件をチェックします。 – Shiladitya

+0

これは有望なShiladityaと聞こえる。私はまだjQueryの初心者です。コード内でどのように見えるでしょうか? – user7498278

答えて

0

が、私はそれはあなたがsessionStorageの使用に必要なものだと思う。このリンクPopup on website load once per session

を訪れてみ、I'vedは前にこれを試してみました正常に動作します。

0

私はこの問題を100%解決できませんでしたが、実行可能な解決策があります。

アイテムの中心には、項目が表示外になった(過去にスクロールしていた)ときに、このdivを再び非表示にしたくありませんでした。その結果、次のコードを削除しました。

scrolled > offsetBottom || 

これは基本的に、スクリプトがダウンスクロールでのみトリガーすることを意味します。 divの上からページをスクロールしない限り、要素を再び非表示/トリガーしません。

関連する問題