jQuery实现刷新页面随机变换背景图片
通过使用jquery实现访问页面或刷新时随机变化容器的背景图,当然也可以用于变化其他图片,实现随机的个性化效果。下面是代码:
$(document).ready(function(e) {
//随机背景
var pictures = new Array();
pictures[0] = "/static/img/homepage-header-talent1.png";
pictures[1] = "/static/img/homepage-header-talent2.png";
pictures[2] = "/static/img/homepage-header-talent3.png";
pictures[3] = "/static/img/homepage-header-talent4.png";
var rd = parseInt(Math.random()*4);
$('.jumbotron').css("background-image", "url("+pictures[rd]+")");
});
下面是实现的效果:
注:文中素材仅为直观的展示代码实际效果而使用。