英文:
Make a slider on cards with three indicator dots
问题
我需要在卡片上创建一个带有三个指示点的滑块。页面上有4张卡片,我想要添加更多的卡片,它们只需滚动,而不换行。
<div class="slider">
<div class="row">
<div class="card">
<img src="https://via.placeholder.com/300x200" alt="产品1">
<p>$19.99</p>
</div>
<div class="card">
<img src="https://via.placeholder.com/300x200" alt="产品2">
<p>$29.99</p>
</div>
</div>
</div>
请注意,这只是您原始代码的一部分。要实现带有滑块和指示点的效果,您需要进一步的HTML、CSS和JavaScript代码。
英文:
I need to make a slider on the cards with three indicator dots. I have 4 cards on the page, I want me to add cards, and they just scroll, and not go to a new line.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.row {
display: flex;
flex-wrap: wrap;
justify-content: center;
margin: 0 auto;
max-width: 1000px;
}
.card {
box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
margin: 10px;
padding: 10px;
text-align: center;
width: calc(25% - 20px);
background-color: #fff;
border: 3px solid #000;
}
.card img {
max-width: 100%;
}
.card p {
font-size: 20px;
font-weight: bold;
margin-top: 10px;
}
<!-- language: lang-html -->
<div class="slider">
<div class="row">
<div class="card">
<img src="https://via.placeholder.com/300x200" alt="Product 1">
<p>$19.99</p>
</div>
<div class="card">
<img src="https://via.placeholder.com/300x200" alt="Product 2">
<p>$29.99</p>
</div>
</div>
<!-- end snippet -->
答案1
得分: 1
你需要在父元素上使用overflow-x:scroll
和flex-wrap: nowrap;
来设置宽度。
.wrapper {
width: 100%;
overflow-x: scroll;
border: 1px solid blue;
background: wheat;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
.card {
border: 1px solid red;
padding: 80px;
margin: 0 10px;
}
<div class="wrapper">
<div class="card">
Card 1
</div>
<div class="card">
Card 2
</div>
<div class="card">
Card 3
</div>
<div class="card">
Card 4
</div>
<div class="card">
Card 5
</div>
<div class="card">
Card 6
</div>
<div class="card">
Card 7
</div>
<div class="card">
Card 8
</div>
</div>
如果你希望它自动滚动并带有指示点,你需要使用JavaScript。如果搜索"scrolling carousel",你可以找到成千上万个预制的轮播组件。
英文:
You'll want to use a width on the parent with overflow-x:scroll
and flex-wrap: nowrap;
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.wrapper {
width: 100%;
overflow-x: scroll;
border: 1px solid blue;
background: wheat;
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
.card {
border: 1px solid red;
padding: 80px;
margin: 0 10px;
}
<!-- language: lang-html -->
<div class="wrapper">
<div class="card">
Card 1
</div>
<div class="card">
Card 2
</div>
<div class="card">
Card 3
</div>
<div class="card">
Card 4
</div>
<div class="card">
Card 5
</div>
<div class="card">
Card 6
</div>
<div class="card">
Card 7
</div>
<div class="card">
Card 8
</div>
</div>
<!-- end snippet -->
https://jsfiddle.net/slingtruchoice/v8Lkp9zm/
If you want it to move on its own with indicator dots, you'll need to use javascript. There are thousands of prebuilt ones you can find online if you search "scrolling carousel"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论