简体   繁体   English

使用CSS将divs(屏幕外)动画到一个位置

[英]Animate divs(off screen) to a position with CSS

When you get in to the site I want some divs animating(offscreen) to a position. 当您进入该站点时,我需要一些div动画(离屏)到某个位置。 I found something like this : 我发现了这样的事情:

 $( document ).ready(function() { $('.box-wrapper').each(function(index, element) { setTimeout(function(){ element.classList.remove('loading'); }, index * 500); }); }); 
 body { overflow-x: hidden; } .box-wrapper { -webkit-transition-duration: 600ms; transition-duration: 600ms; } .box-wrapper.loading:nth-child(odd) { transform: translate(100%); -webkit-transform: translate(100%); } .box-wrapper.loading:nth-child(even) { transform: translate(-100%); -webkit-transform: translate(-100%); } .box-wrapper.loading:nth-child(?) { transform: translate(-100%); -webkit-transform: translate(-100%); } .box-wrapper:nth-child(odd) #box1 { } .box-wrapper:nth-child(even) #box2 { } .box-wrapper:nth-child(?) #box3 { } #box1 { position: relative; display: block; margin: auto; width: 100px; height: 100px; background-color: aqua; } #box2 { position: relative; display: block; left:200px; width: 200px; height: 150px; background-color: aqua; } #box3 { position: relative; display: block; left:400px; width: 600px; height: 150px; background-color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <div class="box-wrapper loading"><div id="box1"></div></div> <div class="box-wrapper loading"><div id="box2"></div></div> <div class="box-wrapper loading"><div id="box3"></div></div> 

It works but I want to control over each box with different animations. 它可以工作,但是我想用不同的动画来控制每个盒子。 But I don't know how. 但是我不知道如何。 If I replace the "?" 如果我替换为“?” with "box3" or something it doesnt work. 与“ box3”或它不起作用。 It has to be "even" or "odd" but I want a different animation. 它必须是“偶数”或“奇数”,但我想要一个不同的动画。

Thanks! 谢谢!

Use this. 用这个。

$(document).ready(function () {
    var typeAnimation = ["style1", "style2"];

    $('.box-wrapper').each(function (index, element) {
        setTimeout(function () {
            var animClassName = typeAnimation[Math.random(typeAnimation.length)];
            element.classList.remove(animClassName);
        }, index * 500);
    });
});

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

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