// these files are stored on my system on these path : C:/Users/ABHISHEK/Desktop/Songify-master/Songify-master/assets/images/
var gifArr = ['source.gif',
'source1.gif',
'source2.gif',
'source3.gif',
'source4.gif',
'source5.gif',
'source6.gif',
'source7.gif',
'source9.gif',
'source10.gif',
'source11.gif',
'source12.gif',
'source13.gif'
];
// function to select the random gif andchange the gif after 5 seconds
function changeGif(){
setInterval(function(){
$('.add-gif').css({'background-image': 'url(C:/Users/ABHISHEK/Desktop/Songify-master/Songify-master/assets/images/' + gifArr[Math.floor(Math.random() * gifArr.length)] + ')'});
},
5000);
}
函数 changeGif() 将选择随机 gif 并在 5 秒后更改背景。该函数将在我的系统上运行,因为我在函数中定义了静态路径。 但我希望下载我的应用程序并运行代码的用户,然后路径将根据用户的系统设置。 那么如何根据用户的系统获取文件的路径呢?
最佳答案
您需要提供脚本文件中图像的相对路径
根据 W3
Partial URLs are interpreted relative to the source of the style sheet, not relative to the document
url 是相对于文件位置的,所以这应该对你有用:
url('../../images/image.jpg')
$('.add-gif').css('backgroundImage','url(images/' + gifArr[Math.floor(Math.random() * gifArr.length)] ) );
关于jquery - 如何访问任何用户系统上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45217716/