移动端全屏滑动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <script> document.documentElement.style.fontSize = document.documentElement.clientWidth / 37.5 + 'px' </script> <style> body { margin: 0; font-size: 5rem; } html, body, #app { height: 100%; } .slider-container { position: relative; height: 100%; width: 100%; overflow: hidden; } .scroll-wrapper { height: 100%; width: 400%; display: flex; position: absolute; } .slider-item { height: 100%; width: 25%; } .inner { height: 100%; display: flex; justify-content: center; align-items: center; } .slider-item:nth-child(1) { background: cornflowerblue; } .slider-item:nth-child(2) { background: chocolate; } .slider-item:nth-child(3) { background: antiquewhite; } .slider-item:nth-child(4) { background: chartreuse; } </style> </head> <body> <div id="app"> <div class="slider-container"> <div class="scroll-wrapper"> <div class="slider-item"> <div class="inner">PAGE-1</div> </div> <div class="slider-item"> <div class="inner">PAGE-2</div> </div> <div class="slider-item"> <div class="inner">PAGE-3</div> </div> <div class="slider-item"> <div class="inner">PAGE-4</div> </div> </div> </div> </div> </body> <script> let sliderContainer = document.querySelector('.slider-container'), scrollWrapper = document.querySelector('.scroll-wrapper'), oldSliderItems = sliderContainer.querySelectorAll('.slider-item'), innerWidth = document.documentElement.clientWidth let startX = 0, moveX = 0, distance = 0, pageIndex = 0, isMove = false const init = () => { bindEvent() } function bindEvent() { scrollWrapper.addEventListener('touchstart', handleTouchStart) scrollWrapper.addEventListener('touchmove', handleTouchMove) scrollWrapper.addEventListener('touchend', handleTouchEnd) } function handleTouchStart(e) { startX = e.touches[0].clientX } function handleTouchMove(e) { moveX = e.touches[0].clientX if ((moveX < startX && pageIndex === oldSliderItems.length - 1) || (moveX > startX && pageIndex === 0)) { return } distance = moveX - startX setTranslateX(-innerWidth * pageIndex + distance) isMove = true } function handleTouchEnd(e) { if (isMove) { if (Math.abs(distance) >= innerWidth / 3) { if (distance > 0) { pageIndex-- } if (distance < 0) { pageIndex++ } } setTranslateX(-innerWidth * pageIndex) } // 还原状态 startX = 0 distance = 0 isMove = false } function setTranslateX(tranX) { scrollWrapper.style.transition = 'all .1s' scrollWrapper.style.transform = `translateX(${tranX}px)` } init() </script> </html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具