原生撸移动端顶部滚动菜单栏,实现可滚动控制滚动边界动态样式

简介: 本文介绍了如何使用原生HTML、CSS和JavaScript创建一个移动端可滚动的顶部菜单栏。文章提供了详细的HTML结构、CSS样式和JavaScript代码,实现了菜单项的横向滚动、边界控制和动态样式变化。同时,还展示了如何通过触摸事件监听来控制菜单项的滚动和激活状态。

原生撸移动端顶部滚动菜单栏

实现的效果为:
在这里插入图片描述
在这里插入图片描述
可以自由滚动,设置滚动的左边界和有边界

html和css为

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>头部移动菜单例子</title>
    <style>
        * {
   
            padding: 0;
            margin: 0;
        }

        #tabsBox {
   
            position: relative;
            width: 100%;
            background-color: #fff;
            height: 60px;
            overflow-x: auto;
            overflow-y: hidden;
            border-bottom: 1px solid #8A39FF;
        }

        #tabsBox .tabsBoxChild {
   
            padding: 11px 15px;
            width: max-content;
            position: absolute;
            left: 0;
        }

        #tabsBox .tabsBoxChild>span {
   
            display: inline-block;
            line-height: 38px;
            padding: 0 20px;
        }

        /* 点击 激活的样式 */
        #tabsBox .tabsBoxChild>.active {
   
            color: #8A39FF;
        }

        /* 隐藏 滚动条 */
        /* webkit     */
        #tabsBox .tabsBoxChild::-webkit-scrollbar {
   
            width: 0 !important
        }

        /* IE 10+ */
        #tabsBox .tabsBoxChild {
   
            -ms-overflow-style: none;
        }

        /* Firefox */
        #tabsBox .tabsBoxChild {
   
            overflow: -moz-scrollbars-none;
        }
    </style>
</head>

<body>
    <div id="tabsBox">
        <div class="tabsBoxChild">
            <span class="childSpan" ontouchstart="tab(0)">
                菜单1
            </span>
            <span class="childSpan" ontouchstart="tab(1)">
                菜单2
            </span>
            <span class="childSpan" ontouchstart="tab(2)">
                菜单3
            </span>
            <span class="childSpan" ontouchstart="tab(3)">
                菜单4
            </span>
            <span class="childSpan" ontouchstart="tab(4)">
                菜单5
            </span>
            <span class="childSpan" ontouchstart="tab(5)">
                菜单6
            </span>
            <span class="childSpan" ontouchstart="tab(6)">
                菜单7
            </span>
            <span class="childSpan" ontouchstart="tab(7)">
                菜单8
            </span>
            <span class="childSpan" ontouchstart="tab(8)">
                菜单1
            </span>
            <span class="childSpan" ontouchstart="tab(9)">
                菜单9
            </span>
            <span class="childSpan" ontouchstart="tab(10)">
                菜单10
            </span>
            <span class="childSpan" ontouchstart="tab(11)">
                菜单11
            </span>
            <span class="childSpan" ontouchstart="tab(12)">
                菜单12
            </span>

        </div>
    </div>
</body>

</html>

上面的css和html已经实现所有元素在一行显示的效果,但是不可以滚动,我们需要使用js来动态控制它的定位,来实现它的滚动效果。

点击事件更换高亮样式active

    <script>

        // 第一次点击的默认值
        let tabsBoxChildStartPageX = 0
        // move默认值
        let tabsBoxChildMovePageX = 0
        // start move的差值
        let tabsBoxChildPageX = 0
        // 获取滚动盒子
        let tabsBoxChild = document.getElementsByClassName('tabsBoxChild')[0]
        // 移动端点击事件
        tabsBoxChild.addEventListener('touchstart', function (e) {
   
            tabsBoxChildStartPageX = 0
            tabsBoxChildMovePageX = 0
            tabsBoxChildStartPageX = e.touches[0].pageX
        })
        // 移动端移动事件
        tabsBoxChild.addEventListener('touchmove', function (e) {
   
            tabsBoxChildMovePageX = e.touches[0].pageX
            tabsBoxChildPageX = tabsBoxChildStartPageX - tabsBoxChildMovePageX
            tabsBoxChildStartPageX = tabsBoxChildMovePageX
            let tabsBoxChildLeft = Number(tabsBoxChild.style.left.split('px')[0])
            // 左临界
            if (tabsBoxChildLeft > 0) {
   
                tabsBoxChild.style.left = '0px'
                return
            }
            // 右临界   
            // tabsBoxChild.offsetWidth当前盒子的宽度  
            // document.body.clientWidth 视图宽度
            if (tabsBoxChildLeft <
                -(tabsBoxChild.offsetWidth - document.body.clientWidth)) {
   
                tabsBoxChild.style.left = -(tabsBoxChild.offsetWidth - document.body.clientWidth) + 'px'
                return
            }
            tabsBoxChild.style.left = Number(tabsBoxChildLeft) + (-tabsBoxChildPageX) + 'px'
        })

        function tab(i = '0') {
   
            let tabsBoxChild = document.getElementsByClassName('tabsBoxChild')[0]
            for (let i = 0; i < tabsBoxChild.children.length; i++) {
   
                tabsBoxChild.children[Number(i)].classList.remove("active");
            }
            tabsBoxChild.children[Number(i)].classList.add("active");
        }
        tab()
    </script>

点击的时候触发tab函数,将所有子元素遍历一遍去掉active类名,将当前点击的元素加上active类名。

目录
相关文章
|
JavaScript 前端开发 数据处理
vue2、vue3中使用$forceUpdate()
vue2、vue3中使用$forceUpdate()
3127 0
|
数据处理 计算机视觉 Python
【目标检测】指定划分COCO数据集训练(车类,行人类,狗类...)
【目标检测】指定划分COCO数据集训练(车类,行人类,狗类...)
6122 0
|
前端开发 JavaScript
Layui之组件的基本使用及案例演示1
Layui之组件的基本使用及案例演示1
489 0
|
IDE 开发工具 Python
PyCharm IDEA 安装【Chinese(Simplified)Language Pack/中文语言包】插件汉化出错
安装【Chinese(Simplified)Language Pack/中文语言包】插件时报【Plugin Installation】错误
9215 1
PyCharm IDEA 安装【Chinese(Simplified)Language Pack/中文语言包】插件汉化出错
|
11月前
|
Ubuntu Linux Windows
IP地址查看方法
本指南介绍了在不同操作系统中查看设备IP地址的方法。在Windows系统中,可通过命令提示符(输入`ipconfig`)或设置界面查找IPv4地址;Linux系统中,使用终端命令`ifconfig`或`ip addr show`获取网络接口的IP;Mac系统则可在“系统偏好设置”中的“网络”查看,或通过终端执行相同命令获取。这些方法简单易行,适用于各种常见场景。
5394 11
|
前端开发 JavaScript API
惊呆了!这些前端技术竟然能让你的网站实现无缝滚动效果!
【10月更文挑战第30天】本文介绍了几种实现网页无缝滚动的技术,包括CSS3的`scroll-snap`属性、JavaScript的Intersection Observer API以及现代前端框架如React和Vue的动画库。通过示例代码展示了如何使用这些技术,帮助开发者轻松实现流畅的滚动效果,提升用户体验。
1227 29
echarts如何设置滚动条(dataZoom),实现横向或纵向滚动
echarts如何设置滚动条(dataZoom),实现横向或纵向滚动
echarts如何设置滚动条(dataZoom),实现横向或纵向滚动
|
JavaScript 前端开发 API
vue3 v-md-editor markdown编辑器(VMdEditor)和预览组件(VMdPreview )的使用
本文介绍了如何在Vue 3项目中使用v-md-editor组件库来创建markdown编辑器和预览组件。文章提供了安装步骤、如何在main.js中进行全局配置、以及如何在页面中使用VMdEditor和VMdPreview组件的示例代码。此外,还提供了一个完整示例的链接,包括编辑器和预览组件的使用效果和代码。
vue3 v-md-editor markdown编辑器(VMdEditor)和预览组件(VMdPreview )的使用
|
微服务 应用服务中间件
微服务跨域(通过网关配置进行跨域)
在单体架构中,我们通常通过SpringMVC配置类实现CORS跨域支持,设置允许的来源、请求头、方法及凭证等。然而,在微服务架构下,因浏览器首先访问网关再进行服务路由,需在网关配置跨域。对于无SpringMVC环境的网关(如使用Gateway组件),我们可在YAML文件中配置`spring.cloud.gateway.globalcors`属性,以实现全局跨域支持。
1003 0
|
编解码 缓存 前端开发
【专栏:HTML与CSS移动端开发篇】移动端网页布局与适配
【4月更文挑战第30天】本文探讨了如何使用HTML和CSS优化移动端网页布局与适配,强调响应式设计、灵活布局和媒体查询的重要性。针对移动设备的屏幕尺寸、操作方式、网络速度和性能差异,提出了断点选择、触摸优化、图像和性能优化等最佳实践。测试和调试、框架工具的应用也是关键步骤,以确保在多设备上提供优秀用户体验。开发者需持续学习新趋势和工具,以适应移动端发展。
771 2