随机图片展示 - 随机从文件夹中显示图片
<!DOCTYPE html>
<html>
<head>
<title>随机图片展示 - 随机从文件夹中显示图片</title>
<meta charset="UTF-8">
<style>
body {
background-color: #f5f5f5;
font-family: Arial, sans-serif;
text-align: center;
}
h1 {
margin-top: 50px;
}
img {
max-width: 90%;
max-height: 90%;
margin-top: 50px;
border: 5px solid #ddd;
box-shadow: 0 0 10px rgba(0,0,0,0.2);
}
button {
margin-top: 30px;
padding: 10px 20px;
font-size: 18px;
color: #fff;
background-color: #4CAF50;
border: none;
border-radius: 5px;
cursor: pointer;
}
</style>
</head>
<body>
<h1>随机图片展示 - 随机从文件夹中显示图片</h1>
<img id="random-img">
<button onclick="showRandomImage()">显示随机图片</button>
<pre><code><script>
function showRandomImage() {
var folder = 'photo/';
var images = ['image1.jpg', 'image2.jpg', 'image3.jpg', 'image4.jpg', 'image5.jpg', 'image6.jpg', 'image7.jpg', 'image8.jpg', 'image9.jpg', 'image10.jpg'];
var randomIndex = Math.floor(Math.random() * images.length);
var randomImage = images[randomIndex];
document.getElementById('random-img').src = folder + randomImage;
}
</script>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/nvZg 著作权归作者所有。请勿转载和采集!