下面的脚本(在Stackoverflow成员的帮助下)现在可以很好地显示带有标题的随机图像。然而,除了图像淡入,你如何淡出-在字幕以及?
// Preload
$(images).each(function(){
$("<img/>")[0].src = this.url;});
var images = new Array(); //array of imgs objects
images[0] = {url: "/images/backgrounds/l&h.jpg", caption: "Stan Laurel and Oliver Hardy"};
images[1] = {url: "/images/backgrounds/sherlock-watson.jpg", caption: "Basil Rathbone and Nigel Bruce"};
images[2] = {url: "/images/backgrounds/powell-loy.jpg", caption: "William Powell and Myrna Loy"};
images[3] = {url: "/images/backgrounds/conried-bergman-bogart.jpg", caption: "Paul Heinreid, Ingrid Bergman and Humphrey Bogart"};
function loadRandomImage(imgs) {
var index = Math.floor(Math.random() * imgs.length);
console.log("loadRandomImages(): index = "+ index);
$.backstretch(imgs[index].url, {fade: 1500, speed: 5000});
$("#caption").html(imgs[index].caption);
}
// we run the function once at the beginning.
loadRandomImage(images);
// Change images every 5 seconds
setInterval(loadRandomImage, 5000, images);<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-backstretch/2.0.4/jquery.backstretch.min.js"></script>
<div id="caption"></div>发布于 2019-11-20 19:43:59
我认为您可以在css中添加一个css转换表ex:#caption { transition: opacity 3s linear 0s },或者将它添加到普通js中:
document.getElementById('caption').style.transition = 'opacity 3s linear 0s'我不太了解jQuery,但我肯定有办法
https://stackoverflow.com/questions/58961500
复制相似问题