
Vue实现移动端APP的方格布局横向滑动翻页带滚动条
发布日期:2021-05-10 06:04:31
浏览次数:25
分类:原创文章
本文共 4264 字,大约阅读时间需要 14 分钟。
在移动端,横向滚动的时候,使用一个小小滚动条,说明是可以左右滑动的。
在这里不是直接左右滑动切换到下个页面,而是div左右滑动。
效果如哈罗单车这种金刚区的交互。下面蓝色的是滚动条
我自己实现效果如下:
代码如下:
<template> <div class="selfHelp"> <div class="scrollContent" ref="scrollCon" @scroll="scrollCon"> <div class="con-item"> <div class="item-top"> <img class="img" src="../../assets/img/class/qq.png" alt="" /> <div class="item-label">我的订单</div> </div> <div class="item-bot"> <img class="img" src="../../assets/img/class/wx.png" alt="" /> <div class="item-label">个人信息</div> </div> </div> <div class="con-item"> <div class="item-top"> <img class="img" src="../../assets/img/class/qq.png" alt="" /> <div class="item-label">我的订单</div> </div> <div class="item-bot"> <img class="img" src="../../assets/img/class/wx.png" alt="" /> <div class="item-label">个人信息</div> </div> </div> <div class="con-item"> <div class="item-top"> <img class="img" src="../../assets/img/class/qq.png" alt="" /> <div class="item-label">我的订单</div> </div> <div class="item-bot"> <img class="img" src="../../assets/img/class/wx.png" alt="" /> <div class="item-label">个人信息</div> </div> </div> <div class="con-item"> <div class="item-top"> <img class="img" src="../../assets/img/class/qq.png" alt="" /> <div class="item-label">我的订单</div> </div> <div class="item-bot"> <img class="img" src="../../assets/img/class/wx.png" alt="" /> <div class="item-label">个人信息</div> </div> </div> <div class="con-item"> <div class="item-top"> <img class="img" src="../../assets/img/class/qq.png" alt="" /> <div class="item-label">我的订单</div> </div> <div class="item-bot"> <img class="img" src="../../assets/img/class/wx.png" alt="" /> <div class="item-label">个人信息</div> </div> </div> </div> <div class="scrollSlider" ref="scrollBox"> <div class="contentSlider" ref="scrollBar"></div> </div> </div></template><script>import apis from '@/http/custom.js'export default { data () { return { conScrollWidth: '', // 滚动内容的宽度 conClientWidth: '', // box的宽度 conScrollLeft: '', // 滚动了的距离 } }, methods: { // 滚动事件 scrollCon (e) { this.conScrollWidth = e.target.scrollWidth this.conClientWidth = e.target.clientWidth this.conScrollLeft = e.target.scrollLeft var scale = (this.conScrollWidth - this.conClientWidth) / (this.$refs.scrollBox.clientWidth - this.$refs.scrollBar.clientWidth) this.$refs.scrollBar.style.marginLeft = (this.conScrollLeft / scale) + 'px' } }}</script><style lang="less">.selfHelp { .scrollContent { width: 100%; height: 120px; overflow-y: hidden; overflow-x: auto; white-space: nowrap; border: 1px solid red; .con-item { text-align: center; display: inline-block; background-color: cyan; width: 25%; height: 120px; .item-top { height: 60px; } .item-bot { height: 60px; } .img { width: 30px; height: 30px; } .item-label { font-size: 12px; } } } .scrollContent::-webkit-scrollbar { /*滚动条整体样式*/ width: 0; /*高宽分别对应横竖滚动条的尺寸*/ height: 0; } .scrollSlider { margin: 0 auto; margin-top: 20px; height: 5px; width: 50px; background-color: chocolate; .contentSlider { height: 5px; width: 20px; background-color: cornflowerblue; } }}</style>
主要实现逻辑如下:
// 滚动事件 scrollCon (e) { //使用比例来实现同步滚动 //this.conScrollWidth 滚动区域的长度 //this.conClientWidth div区域长度 //this.conScrollLeft 滚动了的距离 this.conScrollWidth = e.target.scrollWidth this.conClientWidth = e.target.clientWidth this.conScrollLeft = e.target.scrollLeft // 计算滑动的比例 var scale = (this.conScrollWidth - this.conClientWidth) / (this.$refs.scrollBox.clientWidth - this.$refs.scrollBar.clientWidth) //滚动条滑动的距离 this.$refs.scrollBar.style.marginLeft = (this.conScrollLeft / scale) + 'px' }
以上就是整个滚动区域的实现过程,如果对你有帮助,动动小手点个赞,支持一下作者。🤗🤗🤗
发表评论
最新留言
表示我来过!
[***.240.166.169]2025年05月09日 09时09分59秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
解决:angularjs radio默认选中失效问题
2019-03-06
windows环境下安装zookeeper(仅本地使用)
2019-03-06
缓冲区溢出实例(一)--Windows
2019-03-06
Hadoop学习笔记—Yarn
2019-03-06
Jenkins - 部署在Tomcat容器里的Jenkins,提示“反向代理设置有误”
2019-03-06
wxWidgets源码分析(3) - 消息映射表
2019-03-06
wxWidgets源码分析(5) - 窗口管理
2019-03-06
wxWidgets源码分析(8) - MVC架构
2019-03-06
wxWidgets源码分析(9) - wxString
2019-03-06
[梁山好汉说IT] 梁山好汉和抢劫银行
2019-03-06
[源码解析] 消息队列 Kombu 之 基本架构
2019-03-06
[源码分析] 消息队列 Kombu 之 启动过程
2019-03-06
wx.NET CLI wrapper for wxWidgets
2019-03-06
ASP.NET MVC Action Filters
2019-03-06
Powershell中禁止执行脚本解决办法
2019-03-06
OO_Unit2 多线程电梯总结
2019-03-06
04_Mysql配置文件(重要参数)
2019-03-06