如何使用JavaScript在后台旋转GIF?

如何使用JavaScript在后台旋转GIF?,javascript,html,css,Javascript,Html,Css,我很难使用javascript在身体背景中播放gif。我在css正文中设置了背景,并在style.css的root中设置了一个变量,以便在javascript中访问该变量。这种方式行不通。我的javascript代码非常完美,但我无法在style.css中更改url。有人能帮我吗?谢谢 var指数=0; 变量图像=['https://media.giphy.com/media/BHNfhgU63qrks/source.gif','https://media.giphy.com/media/l3

我很难使用javascript在身体背景中播放gif。我在css正文中设置了背景,并在style.css的root中设置了一个变量,以便在javascript中访问该变量。这种方式行不通。我的javascript代码非常完美,但我无法在style.css中更改url。有人能帮我吗?谢谢

var指数=0;
变量图像=['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'];//获取所有图像并将其保存到数组中
var totalImages=images.length;
函数slidemages(){
document.documentElement.style.setProperty('--bg change gif',url(images[index]);//按特定索引获取图像

如果(index您的代码在这里有一点错误:

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指数=0;
变量图像=['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'];//获取所有图像并将其保存到数组中
var totalImages=images.length;
函数slidemages(){
document.documentElement.style.setProperty('--bg change gif','url('+images[index]+'));//按特定索引获取图像

非常感谢你。