如何创建背景图像列表'URL,并使用jQuery在单击函数时应用它们
how can I create a list of background images' URLs and apply them on click function with jQuery?
我希望从图像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
});
你可以随机地做这件事&next&上一篇:
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];
}
});
相关文章:
- jquery单击隐藏的复选框
- jQuery单击隐藏其父元素
- 水平滚动不适用于jquery单击
- jQuery单击事件在一次之后不会触发
- Jquery单击按钮上没有类或id的事件
- 将JQuery单击事件直接指向同一元素的两个
- 如何通过使用jQuery单击该DIV来获取children DIV类名
- 使用 jQuery 单击链接时更改链接背景颜色
- JQuery:单击范围标签时切换日期选择器
- 如何使用jQuery单击其缩略图时显示主图像
- JQuery单击“选择子图像”
- 使用jQuery单击锚后,更改锚的文本
- jQuery单击并显示元素列表
- JQuery:单击具有特定URL的嵌套Img
- Jquery单击“选中最近的复选框”
- Jquery单击按钮时不显示警告框
- 突出显示iframe内的文本,同时使用jquery单击文档上的相同文本
- 无法识别JQuery单击事件
- jquery单击链接并获取单选输入值以更改span中的文本
- 使用jquery单击事件淡入隐藏文本