HTML 手风琴相册



<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="style.css" />
<title>Expanding Cards</title>
<style>
@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');

* {
box-sizing: border-box;
}

body {
font-family: 'Muli', sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
overflow: hidden;
margin: 0;
}

.container {
display: flex;
width: 90vw;
}

.panel {
background-size: cover;
background-position: center;
background-repeat: no-repeat;
height: 80vh;
border-radius: 50px;
color: #fff;
cursor: pointer;
flex: 0.5;
margin: 10px;
position: relative;
-webkit-transition: all 700ms ease-in;
}

.panel h3 {
font-size: 24px;
position: absolute;
bottom: 20px;
left: 20px;
margin: 0;
opacity: 0;
}

.panel.active {
flex: 5;
}

.panel.active h3 {
opacity: 1;
transition: opacity 0.3s ease-in 0.4s;
}

@media (max-width: 480px) {
.container {
width: 100vw;
}

.panel:nth-of-type(4),
.panel:nth-of-type(5) {
display: none;
}
}
</style>
</head>

<body>
<div class="container"></div>

<script>
// 封装一个函数,接受 cardData 作为参数
function createPanels(cardData) {
const container = document.querySelector('.container');

// 动态创建面板元素
cardData.forEach((data, index) => {
const panel = document.createElement('div');
panel.classList.add('panel');
if (index === 0) {
panel.classList.add('active');
}
panel.style.backgroundImage = `url('${data.imageUrl}')`;

const h3 = document.createElement('h3');
h3.textContent = data.title;
panel.appendChild(h3);

container.appendChild(panel);
});

const panels = document.querySelectorAll('.panel');

panels.forEach(panel => {
panel.addEventListener('click', () => {
removeActiveClasses();
panel.classList.add('active');
});
});

function removeActiveClasses() {
panels.forEach(panel => {
panel.classList.remove('active');
});
}
}

// 定义一个数组,包含图片地址和对应的 h3 文本
const cardData = [
{
imageUrl: 'https://images.unsplash.com/photo-1558979158-65a1eaa08691?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80',
title: 'Explore The World'
},
{
imageUrl: 'https://images.unsplash.com/photo-1572276596237-5db2c3e16c5d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80',
title: 'Wild Forest'
},
{
imageUrl: 'https://images.unsplash.com/photo-1507525428034-b723cf961d3e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1353&q=80',
title: 'Sunny Beach'
},
{
imageUrl: 'https://images.unsplash.com/photo-1551009175-8a68da93d5f9?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1351&q=80',
title: 'City on Winter'
},
{
imageUrl: 'https://images.unsplash.com/photo-1549880338-65ddcdfd017b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80',
title: 'Mountains - Clouds'
}
];

// 调用函数,传入 cardData
createPanels(cardData);
</script>
</body>

</html>