在卡片上制作一个带有三个指示点的滑块。

huangapple go评论53阅读模式
英文:

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 -->

&lt;div class=&quot;slider&quot;&gt;
  &lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;card&quot;&gt;
      &lt;img src=&quot;https://via.placeholder.com/300x200&quot; alt=&quot;Product 1&quot;&gt;
      &lt;p&gt;$19.99&lt;/p&gt;
    &lt;/div&gt;
    
    &lt;div class=&quot;card&quot;&gt;
      &lt;img src=&quot;https://via.placeholder.com/300x200&quot; alt=&quot;Product 2&quot;&gt;
      &lt;p&gt;$29.99&lt;/p&gt;
    &lt;/div&gt;
  &lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

你需要在父元素上使用overflow-x:scrollflex-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 -->

&lt;div class=&quot;wrapper&quot;&gt;
  &lt;div class=&quot;card&quot;&gt;
    Card 1
  &lt;/div&gt;
  &lt;div class=&quot;card&quot;&gt;
    Card 2
  &lt;/div&gt;
  &lt;div class=&quot;card&quot;&gt;
    Card 3
  &lt;/div&gt;
  &lt;div class=&quot;card&quot;&gt;
    Card 4
  &lt;/div&gt;
  &lt;div class=&quot;card&quot;&gt;
    Card 5
  &lt;/div&gt;
  &lt;div class=&quot;card&quot;&gt;
    Card 6
  &lt;/div&gt;
  &lt;div class=&quot;card&quot;&gt;
    Card 7
  &lt;/div&gt;
  &lt;div class=&quot;card&quot;&gt;
    Card 8
  &lt;/div&gt;
&lt;/div&gt;

<!-- 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"

huangapple
  • 本文由 发表于 2023年4月7日 02:31:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75952688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定