<div class="menu-container"> <a href="#">菜单项1</a> <a href="#">菜单项2</a> <a href="#">菜单项3</a> <a href="#">菜单项4</a> <a href="#">菜单项5</a> </div>
.menu-container{ display: flex; overflow-x: scroll; scroll-snap-type: x mandatory; -webkit-overflow-scrolling: touch; } a{ flex: 0 0 100px; height: 100px; background: #ccc; color: #fff; text-align: center; line-height: 100px; font-size: 18px; scroll-snap-align: center; margin-right: 10px; } a:last-child{ margin-right: 0; }
var menu = document.querySelector('.menu-container'); var startX, currentX, moveX, flag = false; menu.addEventListener('touchstart', function (e) { startX = e.touches[0].pageX; currentX = startX; flag = true; }); menu.addEventListener('touchmove', function (e) { if (flag) { moveX = e.touches[0].pageX - currentX; currentX = e.touches[0].pageX; menu.scrollLeft -= moveX; } }); menu.addEventListener('touchend', function (e) { flag = false; });
本文为翻滚的胖子原创文章,转载无需和我联系,但请注明来自猿教程iskeys.com