简体   繁体   English

滞后淡入淡出背景

[英]Laggy fade in fade out for a background

I want the background image for my web page to fade out as the next image fades in, i do it like so: 我希望网页的背景图片随着下一张图片的淡入淡出,我这样做是这样的:

    var backgrounds = [];
    backgrounds[0] = './img/bg.jpg';
    backgrounds[1] = './img/bg2.jpg';
    backgrounds[2] = './img/bg3.jpg';



function changeBackground() {
    currentBackground++;
if(currentBackground > backgrounds.length-1) currentBackground = 0;

$('.bgContainer').fadeOut(1500,function() {
    $('.bgContainer').attr('src', backgrounds[currentBackground]);
    $('.bgContainer').fadeIn(3000);
});



    setTimeout(changeBackground, 5000);
}

and its working fine, but each time the next image fades in any other elements start dropping the frames and become really laggy. 并且效果很好,但是每次下一个图像在其他任何元素中淡出时,它们都会开始掉落帧并变得迟钝。 is there any way to reduce the frame drops? 有什么办法可以减少丢帧?

.bgContainer{
position:fixed;
z-index: -1;
width:100vw;
height:100%;
top:0;
}

<img class="bgContainer" src = "./img/bg.jpg">

I don't know if you can use css transitions for browser compatibility but you could try something like: 我不知道您是否可以使用css转换来实现浏览器兼容性,但是可以尝试以下操作:

.bgContainer {
    transition: opacity 1500ms ease-in-out;
}

and

$('.bgContainer').css('opacity', 0);

setTimeout(function() {
    $('.bgContainer').attr('src', backgrounds[currentBackground]);
    $('.bgContainer').css('opacity', 1);
}, 1500);

This way you are not using javascript to change css properties but use CSS3 to fade your backgrounds opacity. 这样,您无需使用javascript来更改css属性,而是使用CSS3来淡化背景的不透明度。

Way less laggy then javascript animations but will only work on modern browsers. 相比javascript动画,它的延迟时间更少,但仅适用于现代浏览器。 ie10+ IE10 +

Use css transitions rather than jQuery. 使用CSS过渡而不是jQuery。 I found this especially true for mobile devices. 我发现这对于移动设备尤其如此。

Add a class via javascript or jQuery and apply the effects u want too that class. 通过javascript或jQuery添加一个类,然后将所需的效果应用到该类。

Take a look at this if u want to more about CSS transitions 看看这个如果u想要更多关于CSS过渡

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

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