使用animate.css自定义水平滚动

时间:2017-10-16 16:50:59

标签: html css animate.css mcustomscrollbar wow.js

我有一些代码:

$(window).load(function(){
	// Horizontal scroll
    if($(".js-page-scroll").length){
        $(".js-page-scroll").mCustomScrollbar({
        	axis:"x",
            theme:"dark-3",
            // scrollbarPosition: 'outside',
            advanced:{ autoExpandHorizontalScroll:true },

            callbacks:{
                    whileScrolling:function(){
                    	
                    	new WOW().init();
                        
                    }
                }
        });
    }
});
main {
    display: flex;
    flex-flow: row nowrap;
    align-items: center;
    align-content: center;
    justify-content: space-between;
}

section {
    display: block;
    width: 300px;
    border: 1px solid #000;
    padding: 1rem;
}

.js-page-scroll {
    width: 100%;
    overflow-x: auto;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css" />
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/wow/1.1.2/wow.min.js"></script>  

  
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.5/jquery.mCustomScrollbar.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.5/jquery.mCustomScrollbar.min.js"></script>
  
  <div class="js-page-scroll">
    <main>
      <section>
        <h2>1</h2>
        <p class="wow fadeInUp">
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
        </p>
      </section>
      <section>
        <h2>2</h2>
        <p class="wow fadeInUp">
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
        </p>
      </section>
      <section>
        <h2>3</h2>
        <p class="wow fadeInLeft">
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
        </p>
      </section>
      <section>
        <h2>4</h2>
        <p class="wow fadeInUp">
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
        </p>
      </section>
      <section>
        <h2>5</h2>
        <p class="wow bounceIn">
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
        </p>
      </section>
    </main>
  </div>

我使用自定义滚动(水平) - malihu custom scrollbar

对于动画 - animate.css和wow.js

问题在于,使用水平滚动时,块中的文本会平滑地显示(动画),因此也会顺序显示(如使用wow.js时)。

问题:如何为水平滚动配置wow.js(或者,也许是另一个插件)? or如何为自定义滚动配置animate.css(当滚动块按顺序顺利显示时)?

2 个答案:

答案 0 :(得分:3)

在你的代码片段中,问题是你在滚动时启动了WOW插件。这就是为什么同时触发所有段落标记的动画。

在您的代码段中,我添加了自定义代码,而不是使用 WOW插件。 滚动时,我调用了我animateContent();

添加的自定义函数

JS CODE

// Custome code for smooth animation 

                  function animateContent() {

                   var divWidth = $(".js-page-scroll").width();
                   var divWidthLg = $(".js-page-scroll").width() - 20;
                   var divWidthMd = $(".js-page-scroll").width() - 40;
                   var divWidthSm = $(".js-page-scroll").width() - 100;
                        //console.log(divWidth);
                         //console.log(divWidthLg);console.log(divWidthMd);


                    var section =$("section");


                     section.each(function(){


                     var offset = $(this).offset().left;
                     var ind = $(this).index() + 1;

                    // console.log(ind, offset);

                     if(offset > divWidth) {
                         $("p" , this).addClass("animated fadeOutUp");

                      }

                     if ( (offset < divWidthLg)){

                     if ($("p" , this).hasClass("fadeOutUp")){
                     $("p" , this).removeClass("fadeOutUp");
                     $("p" , this).addClass("animated fadeInUp");
                     setTimeout(function(){
                     $("p" , this).removeClass("fadeInUp");
                     }, 1500);
                     }
                     else {
                     $("p" , this).addClass("animated fadeInUp");
                     }

                     }

                     if(offset < divWidthSm) {
                      // $("p" , this).removeClass("fadeOutUp");
                       // $("p" , this).removeClass("fadeInUp");
                     }

                     });

                }

我做了什么:

  • 首先,我找到了外div
  • 的总宽度
  • 然后在它旁边我找到了每个section
  • 的偏移位置
  • 如果偏移位置小于外部div的总宽度和右偏移位置,我添加了animated fadeInUp类。

样本

$(window).load(function(){
	// Horizontal scroll
    if($(".js-page-scroll").length){
        $(".js-page-scroll").mCustomScrollbar({
        	axis:"x",
            theme:"dark-3",
            // scrollbarPosition: 'outside',
            advanced:{ autoExpandHorizontalScroll:true },

            callbacks:{
                    whileScrolling:function(){
                    	animateContent();
                    	
                        
                    }
                }
        });
    }
});


// Custome code for smooth animation 
 
                  function animateContent() {
                
              
                
                   var divWidth = $(".js-page-scroll").width();
                   var divWidthLg = $(".js-page-scroll").width() - 20;
                   var divWidthMd = $(".js-page-scroll").width() - 40;
                   var divWidthSm = $(".js-page-scroll").width() - 100;
                        //console.log(divWidth);
                         //console.log(divWidthLg);console.log(divWidthMd);
                         
                         
                    var section =$("section");
                          
                          
                     section.each(function(){
                     
                 
                     var offset = $(this).offset().left;
                     var ind = $(this).index() + 1;
                     
                    // console.log(ind, offset);
                     
                     if(offset > divWidthLg) {
                     
											$("p" , this).addClass("animated fadeOutUp");
                      
                      }
                      
                     if ( (offset < divWidthLg)){
                     
                     if ($("p" , this).hasClass("fadeOutUp")){
                     $("p" , this).removeClass("fadeOutUp");
                     $("p" , this).addClass("animated fadeInUp");
                     setTimeout(function(){
                     $("p" , this).removeClass("fadeInUp");
                     }, 1500);
                     }
                     else {
                     $("p" , this).addClass("animated fadeInUp");
                     }
                     
                     }
                     
                     if(offset < divWidthSm) {
                      // $("p" , this).removeClass("fadeOutUp");
                       // $("p" , this).removeClass("fadeInUp");
                     }
                     
                     });

                            
                
                
                
                }
main {
    display: flex;
    flex-flow: row nowrap;
    align-items: center;
    align-content: center;
    justify-content: space-between;
}

section {
    display: block;
    width: 300px;
    border: 1px solid #000;
    padding: 1rem;
}

.js-page-scroll {
    width: 100%;
    overflow-x: auto;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css" />
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/wow/1.1.2/wow.min.js"></script>  

  
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.5/jquery.mCustomScrollbar.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.5/jquery.mCustomScrollbar.min.js"></script>
  
  <div class="js-page-scroll">
    <main>
      <section>
        <h2>1</h2>
        <p class="wow fadeInUp">
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
        </p>
      </section>
      <section>
        <h2>2</h2>
        <p class="wow fadeInUp">
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
        </p>
      </section>
      <section>
        <h2>3</h2>
        <p class="wow fadeInUp">
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
        </p>
      </section>
      <section>
        <h2>4</h2>
        <p class="wow fadeInUp">
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
        </p>
      </section>
      <section>
        <h2>5</h2>
        <p class="wow fadeInUp">
          Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
        </p>
      </section>
    </main>
  </div>

以下是Fiddle.

中的演示

UPDATE(每个元素的单独动画)

我已附上Fiddle demo link.

我在这里做了什么: 我只是获得动画 IN OUT 类形式data attribute,并在必要时附加所需的动画。

通过这种方式,您可以处理每个部分的 IN OUT 动画。

$(window).ready(function(){
    	// Horizontal scroll
        if($(".js-page-scroll").length){
            $(".js-page-scroll").mCustomScrollbar({
            	axis:"x",
                theme:"dark-3",
                // scrollbarPosition: 'outside',
                advanced:{ autoExpandHorizontalScroll:true },

                callbacks:{
                        whileScrolling:function(){
                     animateContent();
                        }
                    }
            });
        }
    });
                //	new WOW().init();
                
                function animateContent() {
                
              
                
                   var divWidth = $(".js-page-scroll").width();
                   var divWidthLg = $(".js-page-scroll").width() - 20;
                   var divWidthMd = $(".js-page-scroll").width() - 40;
                   var divWidthSm = $(".js-page-scroll").width() - 100;
                        //console.log(divWidth);
                         //console.log(divWidthLg);console.log(divWidthMd);
                         
                         
                    var section =$("section");
                          
                          
                     section.each(function(){
                     
                 			var inAnimation = $("p" , this).data("inanimation");
                     var outAnimation = $("p" , this).data("outanimation");
                     var offset = $(this).offset().left;
                     var ind = $(this).index() + 1;
                     
 						// console.log(ind, offset, inAnimation, outAnimation);
                     
                     if(offset > divWidthLg) {
                     
											$("p" , this).addClass("animated "+ outAnimation);
                      
                      }
                      
										if ( (offset < divWidthLg)){
                     
                     if ($("p" , this).hasClass("animated")){
                     $("p" , this).removeClass();
                     $("p" , this).addClass("animated "+ inAnimation);
                     
                     setTimeout(function(){
                     $("p" , this).css("background", "red");
                     }, 1500);
                     
                     }
                     
                     else {
                     $("p" , this).addClass("animated " + inAnimation);
                     }
                     
                     }
                     										if ( (offset < 0)){
                                          $("p" , this).addClass("animate "+ outAnimation);
                                             setTimeout(function(){
                                           $("p" , this).removeClass();
                                             }, 1500);
                                        }
                     if(offset < divWidthSm) {
                      // $("p" , this).removeClass("fadeOutUp");
                       // $("p" , this).removeClass("fadeInUp");
                     }
                     
                     });

                            
                
                
                
                }
main {
        display: flex;
        flex-flow: row nowrap;
        align-items: center;
        align-content: center;
        justify-content: space-between;
    }

    section {
        display: block;
        width: 300px;
        border: 1px solid #000;
        padding: 1rem;
    }

    .js-page-scroll {
        width: 100%;
        overflow-x: auto;
    }
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css" />
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/wow/1.1.2/wow.min.js"></script>  

  
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.5/jquery.mCustomScrollbar.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/malihu-custom-scrollbar-plugin/3.1.5/jquery.mCustomScrollbar.min.js"></script>
  
        <div class="js-page-scroll">
        <main>
          <section>
            <h2>1</h2>
            <p class="wow" data-inanimation="fadeInUp" data-outanimation="fadeOutUp" data-inanimationn="dsfsdg">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
            </p>
          </section>
          <section>
            <h2>2</h2>
            <p class="wow" data-inanimation="fadeInUp" data-outanimation="fadeOutUp">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
            </p>
          </section>
          <section>
            <h2>3</h2>
            <p class="wow" data-inanimation="fadeInLeft" data-outanimation="fadeOutLeft">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
            </p>
          </section>
          <section>
            <h2>4</h2>
            <p class="wow" data-inanimation="fadeInUp" data-outanimation="fadeOutUp">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
            </p>
          </section>
          <section>
            <h2>5</h2>
            <p class="wow" data-inanimation="bounceIn" data-outanimation="bounceOut">
              Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
            </p>
          </section>
        </main>
      </div>

希望这会对你有所帮助。

感谢。

答案 1 :(得分:1)

使用一些简单的JS可以实现水平滚动检测,而无需额外的库。您正在使用JQuery,因此这是一种可能的解决方案。

我使用了双子座定制的scollbar,因为它非常轻巧且用户友好。

我还包含了一个偏移量,因此您可以决定何时显示动画,而不是在元素处于视图中时立即将其关闭。

在这里查看小提琴:http://jsfiddle.net/zfd2t31h/1/

<强> JS

// Add custom scrollbar
var myScrollbar = new GeminiScrollbar({
  element: document.querySelector('.js-page-scroll')
}).create();

// Add scroll events
var scrollContainer = $('.gm-scroll-view');
var scrollElem = $('section');
var offset = 300;

scrollContainer.scroll(function() {
  var $thisContainer = $(this);

  $thisContainer.find(scrollElem).each(function(n) {
    var $thisElem = $(this);

    if ($thisElem.position().left + $thisElem.width() > 0 && $thisElem.position().left < $thisContainer.width() - offset) {
      $('p', $thisElem).addClass('animated fadeInUp');
    }
  });
});

<强> HTML

<script src="https://cdn.jsdelivr.net/npm/gemini-scrollbar@1.5.3/index.js"></script>
<link href="https://cdn.jsdelivr.net/npm/gemini-scrollbar@1.5.3/gemini-scrollbar.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="js-page-scroll">
  <main>
    <section>
      <h2>1</h2>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
      </p>
    </section>
    <section>
      <h2>2</h2>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
      </p>
    </section>
    <section>
      <h2>3</h2>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
      </p>
    </section>
    <section>
      <h2>4</h2>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimi neque laudantium, facilis explicabo?
      </p>
    </section>
    <section>
      <h2>5</h2>
      <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Minima dolorum cumque fugiat ducimus rem dicta sequi neque laudantium, facilis explicabo?
      </p>
    </section>
  </main>
</div>

<强> CSS

main {
  display: flex;
  flex-flow: row;
  align-items: center;
  align-content: center;
  justify-content: space-between;
}

section {
  display: block;
  border: 1px solid #000;
  padding: 1rem;
  flex: 1 0 300px;
}

section p {
  opacity: 0;
}

.gm-scrollbar .thumb {
  background: darkblue;
}