JSFiddle - React, Tailwind, and code Playground
by AlexGun
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="styles.css" />
<script defer src="script.js"></script>
<title>Карточки | Проект 1</title>
</head>
<body>
<div class="container">
<div class="slide">
<h3>Spring</h3>
</div>
<div class="slide">
<h3>Summer</h3>
</div>
<div class="slide">
<h3>Autumn</h3>
</div>
<div class="slide">
<h3>Winter</h3>
</div>
</div>
</body>
</html>
CSS
@import url('https://fonts.googleapis.com/css?family=Muli&display=swap');
* {
box-sizing: border-box;
}
body {
font-family: 'Muli', sans-serif;
overflow: hidden;
margin: 0;
background:black;
height: 100vh;
display:flex;
justify-content: center;
align-items: center;
}
.container {
width: 100%;
display: flex;
padding: 0 20px;
}
.slide {
position:relative;
height: 80vh;
border-radius: 20px;
margin: 10px;
cursor:pointer;
color: white;
flex: 1;
transition:all .8s ease-in-out;
background-size:cover;
background-position: center;
background-repeat: no-repeat;
}
.slide h3 {
position:absolute;
font-size: 40px;
bottom:30px;
left:30px;
margin: 0;
opacity:0;
}
.slide.active {
flex:6;
}
.slide.active h3 {
opacity: 1;
transition: opacity .3s ease-in .6s;
}
JavaScript
const image_links = [
'https://images.unsplash.com/photo-1560258018-c7db7645254e?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1632&q=80',
'https://images.unsplash.com/uploads/14121010130570e22bcdf/e1730efe?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1170&q=80',
'https://images.unsplash.com/photo-1476820865390-c52aeebb9891?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1170&q=80',
'https://images.unsplash.com/photo-1581196607303-95c00f31c676?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1170&q=80'
]
const all_image = document.querySelectorAll(".slide")
all_image.forEach((item,index) => {
item.style.backgroundImage = `url(${image_links[index]})`
item.addEventListener('click', event => choiceClass(event))
})
function choiceClass(event) {
let all_active_image = document.querySelectorAll(".slide.active")
let temp_arr = Array.from(all_active_image).slice()
all_active_image.forEach( item => {
item.classList.remove("active")
})
if (temp_arr.indexOf(event.target) === -1) {
event.target.classList.add("active")
}
}