简体   繁体   English

使用js滚动的CSS动画无法正常工作

[英]CSS Animation on scroll with js is not working

I tried to make an animation on my landing page for whenever the user scrolls down and up. 每当用户上下滚动时,我都会尝试在目标网页上制作动画。

I use WOW.js and Animate.css on this project. 我在此项目上使用WOW.jsAnimate.css I've looked for many references but none worked. 我寻找了很多参考资料,但都没有用。 I think there is a fault in my code, so this my code: 我认为我的代码有错误,因此这是我的代码:

<div class="row">
    <div class="col-sm-12">
        <h4 class="title-colcenter animation-test" data-wow-delay="1s" id="fiturtitle">FITUR</h4>
    </div>
</div>

this is what I do on style.js 这是我在style.jsstyle.js

$(document).ready(function(){

    $("#fiturtitle").hide();

    $(function(){
        $(window).scroll(function(){
            $(".animation-test").each(function(){
                var imagePos = $(this).offset().top;
                var imageHeight = $(this).height();
                var topOfWindow = $(window).scrollTop();

                if(imagePos < topOfWindow + imageHeight && imagePos + imageHeight > topOfWindow){
                    $('#fiturtitle').addClass("bounce");
                }
                else{
                    $('#fiturtitle').removeClass("bounce");
                }
            });
        }); });
    });

JSFiddle 的jsfiddle

Finally: I've made some changes to make it work, still I guess you want to do something different: fiddle 最后:为了使它起作用,我进行了一些更改,但我仍然想您要做一些不同的事情: 提琴

if(imagePos < topOfWindow + imageHeight && imagePos + imageHeight > topOfWindow){

        $('#fiturtitle').removeClass("bounce");
    }else{
        $('#fiturtitle').addClass("bounce");
        $("#fiturtitle").show(); //this makes the title appear
    }

And

<h4 class="title-colcenter animation-test wow" id="fiturtitle" data-wow-delay="0.1s">

Added wow class and changed to data-wow-delay="0.1s" 添加了wow类,并更改为data-wow-delay="0.1s"

It seems that wow class is needed for the animation bounce . 看来, wow需要为动画类bounce

UPDATE : I've changed somethings in the code to reset the animation and make it repeat when you go up and down again. 更新 :我已经更改了代码中的某些内容以重置动画,并使其在您再次上下移动时重复播放。

New version fiddle 新版本提琴

CSS: CSS:

.fitur-wrap {
        margin-top: 100px;
    }
    .content-fitur{
        position:relative;
        z-index: 2;
    height: 900px;
        background: -moz-linear-gradient(top,  rgba(247,247,247,1) 0%, rgba(247,247,247,0) 37%, rgba(0,0,0,0) 100%); /* FF3.6-15 */
        background: -webkit-linear-gradient(top,  rgba(247,247,247,1) 0%,rgba(247,247,247,0) 37%,rgba(0,0,0,0) 100%); /* Chrome10-25,Safari5.1-6 */
        background: linear-gradient(to bottom,  rgba(247,247,247,1) 0%,rgba(247,247,247,0) 37%,rgba(0,0,0,0) 100%); /* W3C, IE10+, FF16+, Chrome26+, Opera12+, Safari7+ */
        filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#f7f7f7', endColorstr='#00000000',GradientType=0 ); /* IE6-9 */
    }

    .content-fitur .image-center{
        text-align: center;
        margin:0 auto 40px auto;
    }

    .toanim {
        position: fixed;
        background-color: white;
        top: 0;
        left: 0;
        height: 80px;
        width: 100%;
        display: none;
        z-index: 9999;
    }
    .title-colcenter{
    text-align: center;
    font-weight: bold; 
    color: #e45b5b;
    padding-top: 25px;
    padding-bottom: 25px;
    margin: 0;
    font-size: 30px;
    }

 .show {
    display:block;
 }

HTML: HTML:

<div class="toanim">
    <h4 class="fiturtitle title-colcenter wow" data-wow-delay="0.1s">FITUR</h4>
</div>

<div class="fitur-wrap content-fitur">
    <div class="container">
        <div class="row row-vertical-middle">
            <div class="col-xs-12 col-sm-4">
                <div class="image-center wow bounceInLeft" data-wow-delay="0.1s">
                    <img src="http://placekitten.com/g/200/300" alt="Bootstrap Image Preview" class="image_container">
                </div>
            </div>
        </div>
    </div>
</div>

JS: JS:

new WOW().init();
var obj = '<h4 class="fiturtitle title-colcenter animation-test wow" data-wow-delay="0.1s">FITUR</h4>';
    $(window).scroll(function(){
        console.log($(window).scrollTop());
            if($(window).scrollTop() == 0){ 
                $('.toanim').removeClass("show");
                $('.fiturtitle').removeClass("bounce");
                $(".toanim").append(obj);
                $(".toanim").find(':first-child').remove();
            }
            else{
                if(!$( ".fiturtitle" ).hasClass( "bounce" )) {
                    $('.fiturtitle').addClass("bounce");
                    $('.toanim').addClass("show");
                    console.log("added bounce");
                }
            }
        });

Check this: q7Lwqqf2/2 检查此: q7Lwqqf2 / 2

Based on this-jQuery-docs 基于此jQuery-docs

$( document ).ready(function() {
  // Handler for .ready() called.
});

Equivalent to the recommended way of calling: 等效于推荐的呼叫方式:

$(function() {
  // Handler for .ready() called.
})

So, some changes were: 因此,进行了一些更改:

(script): (脚本):

  • Do NOT ignite WOW object too soon. 不要太早点燃WOW对象。 Wait document ready, so it should be wrapped under your 等待文档准备就绪,因此应将其包装在

    $(function(){ <..code..> })

(styles): (样式):

.content-fitur .image-center{
 :
 :
 visibility: hidden;
}

By default image-center should be hidden and let animate lib assign inline-css to them. 默认情况下,应隐藏image-center并让animate lib将inline-css分配给它们。


By the way, I'm not sure what you are doing with this line: 顺便说一句,我不确定您在此行中正在做什么:

if(imagePos < topOfWindow + imageHeight && imagePos + imageHeight > topOfWindow){

What do you intend to do with the .animation-test on scroll event?? 您打算对滚动事件进行.animation-test做什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM