給 video 標籤的 src 設置隨機url地址
var videoList = [
"http://1570433780207_9396.mp4",
...
'xxx.mp4'
];
function autoVideo() {
var rand = Math.floor(Math.random() * videoList.length + 1) - 1;
console.log(rand, 'rand')
var randIndex = videoList[rand];
console.log(randIndex)
var str = '';
var url = randIndex;
str +=
'<video id="video" autoplay="autoplay" controls="controls" x5-video-player-type="h5" x5-video-player-fullscreen="true" style="object-fit:fill">'
str += '<source src="' + url + '" type="video/mp4">'
str += '</source>'
str += '</video>'
$('.videoBox').html(str)
};
autoVideo()
window.video.onended = function () {
var rand = Math.floor(Math.random() * videoList.length + 1) - 1;
var randIndex = videoList[rand];
console.log(randIndex, 'randIndex')
this.src = randIndex;
}
複製代碼
給 img 標籤加隨機背景圖片,刷新頁面隨機切換背景圖片
<style>
.wrap {
width: 100%;
height: 13.34rem;
font-size: 0.3rem;
background-color: #ffe8bc;
color: #875a5a;
margin: auto;
text-align: center;
}
.wrap .randomImg {
width: 100%;
height: 10rem;
}
</style>
<body>
<div class="wrap">
<!--設置隨機背景圖片-->
<div class="randomImg" id="randomImg"></div>
</div>
</body>
<script>
bgImg = new Array(2);
bgImg[0] = '../img/1.png';
bgImg[1] = '../img/2.png';
bgImg[2] = '../img/3.png';
bgImg[3] = '../img/4.png';
index = Math.floor(Math.random() * bgImg.length);
$('.randomImg').css('background', 'url(' + bgImg[index] + ')');
$('.randomImg').css('backgroundSize', 'cover');
</script>
複製代碼