亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 JavaScript 在后臺輪播 GIF?

如何使用 JavaScript 在后臺輪播 GIF?

小唯快跑啊 2022-10-27 15:20:16
我很難使用 javascript 在身體背景中旋轉 gif。我已經在 css 正文中設置了背景,并在 style.css 中的 root 中設置了一個變量,以便在 javascript 中訪問這個變量。這種方式行不通。我的 javascript 代碼很完美,但我無法更改 style.css 中的 url。任何人都可以幫助我嗎?謝謝 var index=0;     var images = ['https://media.giphy.com/media/BHNfhgU63qrks/source.gif','https://media.giphy.com/media/l3q2LucQ5TmyO7dFS/source.gif','https://media.giphy.com/media/l0O9xcDNUrPMfYQAE/source.gif','https://media.giphy.com/media/xUOxeWFk7gEwF13wDS/source.gif','https://media.giphy.com/media/BTWsSlrSHGNTa/source.gif','https://media.giphy.com/media/3gWENmQ8qo896QNhPV/source.gif']; //get all the images and saved them into an array     var totalImages = images.length;     function slideImages(){       document.documentElement.style.setProperty('--bg-change-gif',url(images[index]));// get images by specific index               if(index<totalImages-1){             index++;        }        else        {            index=0;        }            setTimeout(slideImages,250)     }     window.onload = slideImages; :root {      --bg-change-gif:url(https://media.giphy.com/media/EfcqFUzY6asdq/source.gif);    }        body {      font-family: 'Montserrat', sans-serif;      min-height: 100vh;      max-height: 100vh;      margin: 0;      color: #fff;      display: -webkit-box;      display: flex;      -webkit-box-orient: vertical;      -webkit-box-direction: normal;              flex-direction: column;      background: var(--bg-change-gif);      background-size: cover;      height: 100%;      overflow: hidden;          }
查看完整描述

1 回答

?
慕絲7291255

TA貢獻1859條經驗 獲得超6個贊

您的代碼在這里有一點錯誤:


document.documentElement.style.setProperty('--bg-change-gif',url(images[index]));// get images by specific index

它會拋出一個錯誤,說url未定義。你應該把它改成這樣:


document.documentElement.style.setProperty('--bg-change-gif','url('+images[index]+')');// get images by specific index

這是您的更新片段!


var index=0;

 var images = ['https://media.giphy.com/media/BHNfhgU63qrks/source.gif','https://media.giphy.com/media/l3q2LucQ5TmyO7dFS/source.gif','https://media.giphy.com/media/l0O9xcDNUrPMfYQAE/source.gif','https://media.giphy.com/media/xUOxeWFk7gEwF13wDS/source.gif','https://media.giphy.com/media/BTWsSlrSHGNTa/source.gif','https://media.giphy.com/media/3gWENmQ8qo896QNhPV/source.gif']; //get all the images and saved them into an array

 var totalImages = images.length;

 function slideImages(){

   document.documentElement.style.setProperty('--bg-change-gif','url('+images[index]+')');// get images by specific index

   

    if(index<totalImages-1){ 

        index++;

    }

    else

    {

        index=0;

    }


    setTimeout(slideImages,250)

 }

 window.onload = slideImages;

:root {

  --bg-change-gif:url(https://media.giphy.com/media/EfcqFUzY6asdq/source.gif);

}


body {

  font-family: 'Montserrat', sans-serif;

  min-height: 100vh;

  max-height: 100vh;

  margin: 0;

  color: #fff;

  display: -webkit-box;

  display: flex;

  -webkit-box-orient: vertical;

  -webkit-box-direction: normal;

          flex-direction: column;

  background: var(--bg-change-gif);

  background-size: cover;

  height: 100%;

  overflow: hidden;

  

}


查看完整回答
反對 回復 2022-10-27
  • 1 回答
  • 0 關注
  • 97 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號