I want to make it there are 3 images and when I click on see more button it will show 4 images.
I have made html, css and javascript, but the image that is immediately displayed is 4. which should display 3 and when clicked see more will display 4 images.
so there are many pictures, but only 3 pictures are displayed, and when you click the see more button it will display other pictures
let loadMoreBtn = document.querySelector('.load-more');
let currentItem = 3;
loadMoreBtn.onclick = () =>{
let boxes = [...document.querySelectorAll('.container .hobby-list .hobby')];
for (var i = currentItem; i < currentItem + 3; i++){
boxes[i].style.display = 'inline-block';
}
currentItem += 3;
if(currentItem >= boxes.length){
loadMoreBtn.style.display = 'none';
}
}
#Hobby{
padding: 50px 0;
}
.hobby-list{
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
grid-gap: 40px;
}
.hobby{
border-radius: 10px;
position: relative;
}
.hobby img{
width: 100%;
border-radius: 10px;
display: block;
transition: transform 0.5s;
}
.layer{
width: 100%;
height: 0;
background: linear-gradient(rgba(0,0,0,0.6), #31c48d);
position: absolute;
left: 0;
bottom: 0;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
padding: 0 40px;
text-align: center;
font-size: 14px;
transition: height 0.5s;
}
.layer a{
margin-top: 15px;
color: #31c48d;
text-decoration: none;
font-size: 18px;
line-height: 60px;
background: #fff;
width: 60px;
height: 60px;
border-radius: 50%;
text-align: center;
}
<div id="Hobby">
<div class="container">
<div class="hobby-list">
<div class="hobby">
<img src="Assets/hobby_1.png">
<div class="layer">
<h3>Playing soccer</h3><br>
</div>
</div>
<div class="hobby">
<img src="Assets/hobby_2.png">
<div class="layer">
<h3>Playing game</h3><br>
</div>
</div>
<div class="hobby">
<img src="Assets/hobby_3.png">
<div class="layer">
<h3>Playing basketball</h3><br>
</div>
</div>
<div class="hobby">
<img src="Assets/hobby_4.png">
<div class="layer">
<h3>Painting</h3><br>
</div>
</div>
</div>
<div class="load-more">See more</div>
</div>
</div>
2
Answers
First of all add class
display: none
to the image you donot want to display at first loadSee the code below
the logic to show the image on see more button was also not correct so I have changed that also
Hide first the image you don’t want to show and add make it visible once user click the button to See more.