简体   繁体   English

当元素可见时运行动画-自定义javascript

[英]Run animation when element is visible - custom javascript

I am creating a website with few animation, below is the piece of javascript and css which is revealing image from left to right 我正在创建一个动画很少的网站,下面是一段从左到右显示图像的javascript和css

  var i = 0; var interval = setInterval(function () { $('.mask').width(i + "%"); i++; if (i == 101) { clearInterval(interval); } }, 20); 
  .mask { background:white; max-width: 640px; z-index:1; height: 426px; position: relative; overflow: hidden; } .img { width: 100%; position: absolute; top: 0; left: 0; } 
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="mask"> <div class="img"> <img src="https://beebom-redkapmedia.netdna-ssl.com/wp-content/uploads/2016/01/Reverse-Image-Search-Engines-Apps-And-Its-Uses-2016.jpg" /> </div> </div> 

and then an javascript which reveals image from right to left. 然后是一个从右到左显示图像的JavaScript。

 var i = 0; var interval = setInterval(function () { $('.mask3').css({ left: -i + "%" }); i++; if (i === 0) { clearInterval(interval); } }, 20); 
  .wrapper { width: 640px; height: 430px; position: relative; } .mask3 { position: absolute; top: 0; left: 0; background:white; width: 100%; height: 100%; z-index:1; } .img { width: 100%; position: absolute; top: 0; left: 0; z-index:0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="mask3"> </div> <div class="img"> <img src="https://beebom-redkapmedia.netdna-ssl.com/wp-content/uploads/2016/01/Reverse-Image-Search-Engines-Apps-And-Its-Uses-2016.jpg" /> </div> </div> 

Now I am trying to load this animation only when div is being displayed, or user scroll down to specific div, and then the animation shall roll back. 现在,我尝试仅在显示div时加载该动画,或者用户向下滚动到特定的div,然后动画将回滚。

I tried few of the scripts, but all of them guided to use if else statement although I am not able to pick anything which is working, can someone please help me out on this one. 我尝试了很少的脚本,但是所有脚本都被指导使用if else语句,尽管我无法选择任何有效的脚本,有人可以帮我解决这个问题。

This is a very rough answer, but you can do this if you want. 这是一个非常粗糙的答案,但是您可以根据需要执行此操作。 You will still need to work on your animation function more to make it hide and then display based on how you want it to be. 您仍然需要更多地使用动画功能,以使其隐藏然后根据您的期望显示。

var elem = $('.img'); //use a better identifier like an id.
var counter = 0
elem.hide();

function myAnimation() {
    elem.show();
    var i = 0;
    var interval = setInterval(function () {
    $('.mask').width(i + "%");
    i++;
    if (i == 101) {
    clearInterval(interval);
    }
    }, 20);
    counter++;
}

$(window).scroll(function(){
    var docViewTop = $(window).scrollTop();
    var docViewBottom = docViewTop + $(window).height();

    var elemTop = $(elem).offset().top;
    var elemBottom = elemTop + $(elem).height();

    if ((elemBottom <= docViewBottom) && (elemTop >= docViewTop) && counter < 1) {
        myAnimation();
    }
});

The counter is basically to animate it once, because you will trigger this animation on every scroll. 计数器基本上只对它进行一次动画处理,因为您将在每次滚动时触发此动画。

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

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