我想让它有3个图像,当我点击查看更多按钮,它会显示4个图像。我做了html,css和javascript,但立即显示的图像是4。其应显示3,并且当点击查看更多时将显示4个图像。所以有很多图片,但只有3张图片显示,当你点击查看更多按钮,它会显示其他图片
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>
4条答案
按热度按时间nr9pn0ug1#
首先,将类
display: none
添加到您不希望在第一次加载时显示的图像中请参阅下面的代码逻辑,以显示更多按钮上的图像也不正确,所以我已经改变了这一点
xfyts7mz2#
ovfsdjhp3#
首先我们需要将
display: none
添加到css上的所有图像中,然后我们只显示加载的前3个图像,然后每当我们单击加载按钮时,我们都会显示从第4个开始的图像。7rtdyuoh4#
要创建一个“查看更多”按钮,您可以使用JavaScript和CSS在单击按钮时切换其他内容的可见性。下面是一个例子:
HTML:
CSS:
Javascript:
});
希望这个工作!