簡體   English   中英

如何在 Backstretch 中淡入字幕

[英]How to Fade-in Captions in Backstretch

以下腳本(在 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>

我想你可以在 css 中添加一個 css transitiont ex: #caption { transition: opacity 3s linear 0s }或者用 vanilla js 添加它:

document.getElementById('caption').style.transition = 'opacity 3s linear 0s'

我不太了解 jQuery 但我確定有辦法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM