我希望从图像 URL 列表中提取 HTML 正文的背景图像,并在每次单击页面上的按钮时随机或按顺序(顺序首选)应用一个新图像。
所以它必须是这样的:
var images = ["URL1", "URL2", "URL3"];
$("button").onclick(function(){
//choose a random/sequential image from var images and use it as a background image for my body
// on another click choose a different background-image from images and use it as background
// and so on
});
最佳答案
you can do it randomly & next & prev like that:
var imagesArr = ["1.jpg","2.jpg"];
var selectedImage = 0;
$("button").click(function(){
var item = imagesArr[Math.floor(Math.random()* imagesArr.length)];
document.getElementById('body').style.backgroundImage = item;
});
$(".nextButton").click(function(){
if(selectedImage < imagesArr.length){
selectedImage++;
document.getElementById('body').style.backgroundImage = imagesArr[selectedImage];
}
});
$(".prevButton").click(function(){
if(selectedImage > 1){
selectedImage--;
document.getElementById('body').style.backgroundImage = imagesArr[selectedImage];
}
});
关于javascript - 如何创建背景图片的 URL 列表并将它们应用到 jQuery 的点击功能上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37115810/