【发布时间】:2018-02-02 15:36:37
【问题描述】:
我想了解这个纯 CSS 滑块背后的数学原理是如何工作的? https://codepen.io/dudleystorey/pen/kFoGw?limit=all&page=1&q=image+slider
我的目标是把它变成一个七图像滑块。
例如,每次我将另一个图像添加到旋转中时,它都会弄乱滑块(两个图像堆叠在一起。)我想了解计算,因此每个图像都会以滑块的全宽显示30 多岁。
@import url(https://fonts.googleapis.com/css?family=Istok+Web);
@keyframes slidy {
0% { left: 0%; }
20% { left: 0%; }
25% { left: -100%; }
45% { left: -100%; }
50% { left: -200%; }
70% { left: -200%; }
75% { left: -300%; }
95% { left: -300%; }
100% { left: -400%; }
}
* {
box-sizing: border-box;
}
body, figure {
margin: 0; background: #101010;
font-family: Istok Web, sans-serif;
font-weight: 100;
}
div#captioned-gallery {
width: 100%; overflow: hidden;
}
figure.slider {
position: relative; width: 500%;
font-size: 0; animation: 30s slidy infinite;
}
figure.slider figure {
width: 20%; height: auto;
display: inline-block; position: inherit;
}
figure.slider img { width: 100%; height: auto; }
figure.slider figure figcaption {
position: absolute; bottom: 0;
background: rgba(0,0,0,0.4);
color: #fff; width: 100%;
font-size: 2rem; padding: .6rem;
}
<base href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/4273/">
<div id="captioned-gallery">
<figure class="slider">
<figure>
<img src="hobbiton-lake.jpg" alt>
<figcaption>Hobbiton, New Zealand</figcaption>
</figure>
<figure>
<img src="wanaka-drowned-tree.jpg" alt>
<figcaption>Wanaka, New Zealand</figcaption>
</figure>
<figure>
<img src="utah-peak.jpg" alt>
<figcaption>Utah, United States</figcaption>
</figure>
<figure>
<img src="bryce-canyon-utah.jpg" alt>
<figcaption>Bryce Canyon, Utah, United States</figcaption>
</figure>
<figure>
<img src="hobbiton-lake.jpg" alt>
<figcaption>Hobbiton, New Zealand</figcaption>
</figure>
</figure>
</div>
【问题讨论】:
-
基本上每张幻灯片您需要将
left移动-100%,left保持不变的百分比是滑块在继续移动之前在该幻灯片上花费的时间。因此,如果您想添加另一张幻灯片,则需要转到left:-500%并重新分配左侧下方的百分比,您可能还希望将额外幻灯片的动画时间从 30 秒增加到 35 秒 -
这是一个更新的 7 个图像的滑块,其中有几个 cmets 对我所做的更改:codepen.io/anon/pen/…
标签: css