前几天在 segmentfault 上看到有人问下面这个效果(segmentfault app 中的)怎么实现,感受这个效果体验还不错(在移动端没有滚动条的状况可以提示有更多内容能够滚动),就用 overflow-x: auto + width: 80%
在 codepen 上写了个 demo。css
恰好最近看到这篇文章 Introducing CSS Scroll Snap Points ,若是结合 Scroll snap points 每次滚动的时候每一个条目始终在中间这样效果就更好了。<!-- more -->html
.snap-slider { overflow-x: auto; -webkit-overflow-scrolling: touch; scroll-snap-type: mandatory; scroll-snap-destination: 50% 0; &__scroller { display: table; text-align: center; } &__item { scroll-snap-coordinate: 50% 0%; display: table-cell; line-height: 150px; font-size: 2em; &:not(:first-child) { border-left: 1px solid #ccc; } } &__width { width: 80vw; } }
属性的具体介绍,参考本文最后的参考连接。git
效果以下:github
查看 Demoweb
遗憾的是目前(7/9/2016)只有 Firefox 和 iOS 上的 Safari 和 Chrome 原生支持(须要前缀),好在这种能够当作渐进加强,即便浏览器不支持也不影响功能。segmentfault
PS:这里有个 polyfill,可是本例中加了彷佛不起做用。浏览器
https://css-tricks.com/introducing-css-scroll-snap-points/app
https://webkit.org/blog/4017/scroll-snapping-with-css-snap-points/ide
原文地址:https://uedsky.com/2016-07/css-scroll-snap-points/
获取最佳阅读体验并参与讨论,请访问原文