50Days50Projects之day05-随机图片展示效果
<!-- run -->
<style>
.box {
font-family: 'Barlow', sans-serif;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
}
.title {
margin: 10px 0 30px;
letter-spacing: 3px;
text-transform: uppercase;
}
.container {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
max-width: 1000px;
}
.container img {
object-fit: cover;
margin: 10px;
height: 300px;
width: 300px;
max-width: 100%;
}
</style>
<div class="box">
<div class="container"></div>
</div>
<script>
const container = document.querySelector('.container')
const unsplashURL = 'https://source.unsplash.com/random/'
const rows = 9
for (let i = 0; i < rows * 3; i++) {
const img = document.createElement('img')
img.src = `${unsplashURL}${getRandomSize()}`
container.appendChild(img)
}
function getRandomNumber() {
return Math.floor(Math.random() * 10) + 300
}
// console.log(getRandomNumber())
function getRandomSize() {
return `${getRandomNumber()}x${getRandomNumber()}`
}
// console.log(getRandomSize())
</script>
JS
const container = document.querySelector('.container')
const unsplashURL = 'https://source.unsplash.com/random/'
const rows = 9
for (let i = 0; i < rows * 3; i++) {
const img = document.createElement('img')
img.src = `${unsplashURL}${getRandomSize()}`
container.appendChild(img)
}
function getRandomNumber() {
return Math.floor(Math.random() * 10) + 300
}
// console.log(getRandomNumber())
function getRandomSize() {
return `${getRandomNumber()}x${getRandomNumber()}`
}
// console.log(getRandomSize())
CSS
@import url('https://fonts.googleapis.com/css2?family=Barlow:wght@800&display=swap');
* {
box-sizing: border-box;
}
body {
font-family: 'Barlow', sans-serif;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
}
.title {
margin: 10px 0 30px;
letter-spacing: 3px;
text-transform: uppercase;
}
.container {
display: flex;
align-items: center;
justify-content: center;
flex-wrap: wrap;
max-width: 1000px;
}
.container img {
object-fit: cover;
margin: 10px;
height: 300px;
width: 300px;
max-width: 100%;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Random Image Feed</title>
</head>
<body>
<h1 class="title">2021 Yılı Anlar</h1>
<div class="container"></div>
<script src="app.js"></script>
</body>
</html>
效果如下👇
未经作者授权,禁止转载
本文来自博客园,作者:CoderWGB,转载请注明原文链接:https://www.cnblogs.com/wgb1234/articles/16361619.html
THE END

浙公网安备 33010602011771号