gpt4 book ai didi

html - 为幻灯片编码淡入/淡出

转载 作者:行者123 更新时间:2023-11-27 23:48:29 24 4
gpt4 key购买 nike

目前我的网站主页有一个幻灯片(4 张图片)作为背景。图像之间的过渡不是很流畅,并且会从一张照片跳到另一张照片。如何创建更平滑的过渡,是否可以淡入和淡出?

非常感谢所有帮助,谢谢


<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(document).ready(function(){
var header = $('body');

var backgrounds = new Array(
'url(http://urs2009.net/wp-content/uploads/2011/11/lights-of-city.jpg)'
, 'url(http://www.wallpaiper.com/wp-content/uploads/2014/06/amazing-wallpaper-hd-8.jpg)'
, 'url(http://hdwallpaperd.com/wp-content/uploads/2014/12/background-wallpaper-hd-1.jpg)'
, 'url(http://www.mrwallpaper.com/wallpapers/rocky-beach-hd.jpg)'
);

var current = 0;

function nextBackground() {
current++;
current = current % backgrounds.length;
header.css('background-image', backgrounds[current]);
}
setInterval(nextBackground, 5000);

header.css('background-image', backgrounds[0]);
});

</script>

最佳答案

将您的 JS 更改为此以使其更高效。如果您想要淡入淡出动画,最好创建一个覆盖整个 body 的 div。:

HTML:

<body>
<div id="div1"></div>
</body>

CSS:

#div1
{
position:absolute;
top:0;
left:0;
bottom:0;
right: 0;
}

Javascript:

 $(document).ready(function(){

var backgrounds = new Array(
'url(http://urs2009.net/wp-content/uploads/2011/11/lights-of-city.jpg)'
, 'url(http://www.wallpaiper.com/wp-content/uploads/2014/06/amazing-wallpaper-hd-8.jpg)'
, 'url(http://hdwallpaperd.com/wp-content/uploads/2014/12/background-wallpaper-hd-1.jpg)'
, 'url(http://www.mrwallpaper.com/wallpapers/rocky-beach-hd.jpg)'
);

var current = 0;

function nextBackground() {
if(current<backgrounds.length)
current++;
else
current=0;
$('#div1').fadeOut(function() {
$('#div1').css({background : backgrounds[current]) });
$('#div1').fadeIn(5000);
}, 5000);
}
setInterval(nextBackground, 5000);
});

关于html - 为幻灯片编码淡入/淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28496735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com