简体   繁体   English

使用jquery淡化并将“position:absolute”元素从上到下翻译

[英]Fade and translate the “position:absolute” element from top to bottom using jquery

The Class "anim" needs to fade in from top to bottom “anim”类需要从上到下淡入淡出

HTML code HTML代码

<div class="col-xs-6" style="position:relative;">
    <img src="<?= site_url('assets/image/right_bg.png'); ?>" style="width:100%; position:absolute; top:0; left:0;">
    <img class="anim" src="<?= site_url('assets/image/right_cover_1-11.png'); ?>">
    <img class="anim" src="<?= site_url('assets/image/right_cover_2-11.png'); ?>">
    <img class="anim" src="<?= site_url('assets/image/right_cover_3-11.png'); ?>">
    <img class="anim" src="<?= site_url('assets/image/right_cover_4-11.png'); ?>">
    <img class="anim" src="<?= site_url('assets/image/right_cover_5-11.png'); ?>">
    <img class="anim" src="<?= site_url('assets/image/right_cover_6-11.png'); ?>">
    <img class="anim" src="<?= site_url('assets/image/right_cover_7-11.png'); ?>">
    <img class="anim" src="<?= site_url('assets/image/right_cover_8-11.png'); ?>">
    <img class="anim" src="<?= site_url('assets/image/right_cover_9-11.png'); ?>">
    <img src="<?= site_url('assets/image/right_bg_mid.png'); ?>" style="width:100%; position:absolute; top:0; left:0;">
</div>

CSS code CSS代码

.anim {
    display:none;
    width:100%; 
    position:absolute; 
    top:-100px; 
    left:0;
}

I would like to work with show() and top:0px in jquery animation, thanks a lot for helping 我想在jquery动画中使用show()top:0px ,非常感谢帮助

The CSS way: CSS方式:

Create a costum animation 创建一个costum动画

@keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Firefox < 16 */
@-moz-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Safari, Chrome and Opera > 12.1 */
@-webkit-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Internet Explorer */
@-ms-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* Opera < 12.1 */
@-o-keyframes fadein {
    from { opacity: 0; }
    to   { opacity: 1; }
}

Then, apply to the divs you want to fade 然后,申请你想要淡化的div

.anim {
    display:none;
    width:100%; 
    position:absolute; 
    top:-100px; 
    left:0;

-webkit-animation: fadein 2s; /* Safari, Chrome and Opera > 12.1 */
   -moz-animation: fadein 2s; /* Firefox < 16 */
    -ms-animation: fadein 2s; /* Internet Explorer */
     -o-animation: fadein 2s; /* Opera < 12.1 */
        animation: fadein 2s;
}

if you want the divs to fade in from top to bottom, you can give each of the divs a unique class name and then create a selector for each of them with the animation css command (the last five lines in the selector above) . 如果你想让div从上到下淡入,你可以给每个div一个唯一的类名,然后使用animation css命令为每个div创建一个选择器(上面选择器中的最后五行)。 The only thing you change is the argument of the seconds. 你唯一改变的是秒的参数。 The top should fade the fastest (suppose 0.2s) and then increase it until the last (all the code here should be in one cssfile) 顶部应该最快消失(假设0.2s),然后将其增加到最后(这里的所有代码应该在一个cssfile中)

The jQuery way jQuery的方式

Put all the divs in an array and then animate each of the while increasing the delay 将所有div放在一个数组中,然后设置每个div的动画,同时增加延迟

$(document).ready(function() {
    $(".anim").each(function(index, value) {
        $(value).fadeTo(index * 1000, 1);

    });
});

While you can play with the fading time (suppose, add index*2000 instead of 1000) Make sure opacity is 0 before fading 虽然你可以玩褪色时间(假设,添加索引* 2000而不是1000)确保褪色前不透明度为0

Fiddle 小提琴

You can do it in Css3, you just need to change the className of your element. 您可以在Css3中执行此操作,只需更改元素的className即可。

.beforeAnim {
    opacity: 0;
    width:100%; 
    position:absolute; 
    top:-100px; 
    left:0;
    transition: all 1s ease-in-out;
}

.anim {
    opacity: 1;
    width:100%; 
    position:absolute; 
    top: 0px; 
    left:0;
    transition: all 1s ease-in-out;
}

for supplment to show one by one : 供应逐一展示:

    var show_time = 0;

    function show_ball(ball) {
        ball.addClass('after');
    }

    $(".anim").each(function (index) {
        var ball = $(this);

        setInterval(function () {
            show_ball(ball);
        }, show_time);

        show_time += 500;
    });

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

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