tnblog
首页
视频
资源
登录

uni-app左侧菜单,左右联动

9820人阅读 2022/1/25 16:42 总访问:295343 评论:0 收藏:0 手机
分类: 随笔

效果如下:

代码:

  1. <template>
  2. <view>
  3. <scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
  4. <view v-for="(item,index) in tabbar" :key="index" :class="[current==index ? 'u-tab-item-active' : '']"
  5. :data-current="index" @tap.stop="swichMenu(index)">
  6. <text>{{item.name}}</text>
  7. </view>
  8. </scroll-view>
  9. <block v-for="(item,index) in tabbar" :key="index">
  10. <scroll-view scroll-y v-if="current==index">
  11. <view>
  12. <view>
  13. <view>
  14. <text>{{item.name}}</text>
  15. </view>
  16. <view>
  17. <view v-for="(item1, index1) in item.foods" :key="index1">
  18. <image :src="item1.icon" mode=""></image>
  19. <view>{{item1.name}}</view>
  20. </view>
  21. </view>
  22. </view>
  23. </view>
  24. </scroll-view>
  25. </block>
  26. </view>
  27. </template>
  28. <script>
  29. export default {
  30. data() {
  31. return {
  32. tabbar: [
  33. {
  34. name:"学习笔记",
  35. foods:[
  36. {
  37. name:".net",
  38. icon:"https://img.tnblog.net/bigclassimg/1net.png"
  39. },
  40. {
  41. name:"java",
  42. icon:"https://img.tnblog.net/bigclassimg/2CORE.png"
  43. },
  44. {
  45. name:"python",
  46. icon:"https://img.tnblog.net/bigclassimg/2CORE.png"
  47. }
  48. ]
  49. },
  50. {
  51. name:"读书笔记",
  52. foods:[
  53. {
  54. name:".net",
  55. icon:"https://img.tnblog.net/bigclassimg/1net.png"
  56. },
  57. {
  58. name:"java",
  59. icon:"https://img.tnblog.net/bigclassimg/python.png"
  60. }
  61. ]
  62. },
  63. {
  64. name:"体育笔记",
  65. foods:[
  66. {
  67. name:".net",
  68. icon:"https://img.tnblog.net/bigclassimg/1net.png"
  69. },
  70. {
  71. name:"java",
  72. icon:"https://img.tnblog.net/bigclassimg/mobile.png"
  73. }
  74. ]
  75. },
  76. {
  77. name:"学习笔记",
  78. foods:[
  79. {
  80. name:".net",
  81. icon:"https://img.tnblog.net/bigclassimg/1net.png"
  82. },
  83. {
  84. name:"java",
  85. icon:"https://img.tnblog.net/bigclassimg/2CORE.png"
  86. }
  87. ]
  88. },
  89. {
  90. name:"读书笔记",
  91. foods:[
  92. {
  93. name:".net",
  94. icon:"https://img.tnblog.net/bigclassimg/1net.png"
  95. },
  96. {
  97. name:"java",
  98. icon:"https://img.tnblog.net/bigclassimg/python.png"
  99. }
  100. ]
  101. },
  102. {
  103. name:"体育笔记",
  104. foods:[
  105. {
  106. name:".net",
  107. icon:"https://img.tnblog.net/bigclassimg/1net.png"
  108. },
  109. {
  110. name:"java",
  111. icon:"https://img.tnblog.net/bigclassimg/mobile.png"
  112. }
  113. ]
  114. },
  115. {
  116. name:"学习笔记",
  117. foods:[
  118. {
  119. name:".net",
  120. icon:"https://img.tnblog.net/bigclassimg/1net.png"
  121. },
  122. {
  123. name:"java",
  124. icon:"https://img.tnblog.net/bigclassimg/2CORE.png"
  125. }
  126. ]
  127. },
  128. {
  129. name:"读书笔记",
  130. foods:[
  131. {
  132. name:".net",
  133. icon:"https://img.tnblog.net/bigclassimg/1net.png"
  134. },
  135. {
  136. name:"java",
  137. icon:"https://img.tnblog.net/bigclassimg/python.png"
  138. }
  139. ]
  140. },
  141. {
  142. name:"体育笔记",
  143. foods:[
  144. {
  145. name:".net",
  146. icon:"https://img.tnblog.net/bigclassimg/1net.png"
  147. },
  148. {
  149. name:"java",
  150. icon:"https://img.tnblog.net/bigclassimg/mobile.png"
  151. }
  152. ]
  153. },
  154. {
  155. name:"学习笔记",
  156. foods:[
  157. {
  158. name:".net",
  159. icon:"https://img.tnblog.net/bigclassimg/1net.png"
  160. },
  161. {
  162. name:"java",
  163. icon:"https://img.tnblog.net/bigclassimg/2CORE.png"
  164. }
  165. ]
  166. },
  167. {
  168. name:"读书笔记",
  169. foods:[
  170. {
  171. name:"zz",
  172. icon:"https://img.tnblog.net/bigclassimg/1net.png"
  173. },
  174. {
  175. name:"aj",
  176. icon:"https://img.tnblog.net/bigclassimg/python.png"
  177. }
  178. ]
  179. },
  180. {
  181. name:"测试笔记",
  182. foods:[
  183. {
  184. name:".net",
  185. icon:"https://img.tnblog.net/bigclassimg/1net.png"
  186. },
  187. {
  188. name:"java",
  189. icon:"https://img.tnblog.net/bigclassimg/mobile.png"
  190. }
  191. ]
  192. }
  193. ],
  194. scrollTop0//tab标题的滚动条位置
  195. current: 0// 预设当前项的值
  196. menuHeight: 0// 左边菜单的高度
  197. menuItemHeight: 0// 左边菜单item的高度
  198. }
  199. },
  200. methods: {
  201. // 点击左边的栏目切换
  202. async swichMenu(index) {
  203. if(index == this.current) return ;
  204. this.current = index;
  205. // 如果为0,意味着尚未初始化
  206. if(this.menuHeight == 0 || this.menuItemHeight == 0) {
  207. await this.getElRect('menu-scroll-view''menuHeight');
  208. await this.getElRect('u-tab-item''menuItemHeight');
  209. }
  210. // 将菜单菜单活动item垂直居中
  211. this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
  212. },
  213. // 获取一个目标元素的高度
  214. getElRect(elClass, dataVal) {
  215. new Promise((resolve, reject) => {
  216. const query = uni.createSelectorQuery().in(this);
  217. query.select('.' + elClass).fields({sizetrue},res => {
  218. // 如果节点尚未生成,res值为null,循环调用执行
  219. if(!res) {
  220. setTimeout(() => {
  221. this.getElRect(elClass);
  222. }, 10);
  223. return ;
  224. }
  225. this[dataVal] = res.height;
  226. }).exec();
  227. })
  228. }
  229. }
  230. }
  231. </script>
  232. <style scoped>
  233. .u-wrap {
  234. height: calc(100vh);
  235. /* #ifdef H5 */
  236. height: calc(100vh - var(--window-top));
  237. /* #endif */
  238. display: flex;
  239. flex-direction: column;
  240. }
  241. .u-search-box {
  242. padding: 18rpx 30rpx;
  243. }
  244. .u-menu-wrap {
  245. flex: 1;
  246. display: flex;
  247. overflow: hidden;
  248. }
  249. .u-search-inner {
  250. background-color: rgb(234, 234, 234);
  251. border-radius: 100rpx;
  252. display: flex;
  253. align-items: center;
  254. padding: 10rpx 16rpx;
  255. }
  256. .u-search-text {
  257. font-size: 26rpx;
  258. // color: $u-tips-color;
  259. color: #909399;
  260. margin-left: 10rpx;
  261. }
  262. .u-tab-view {
  263. width: 200rpx;
  264. height: 100%;
  265. }
  266. .u-tab-item {
  267. height: 110rpx;
  268. background: #f6f6f6;
  269. box-sizing: border-box;
  270. display: flex;
  271. align-items: center;
  272. justify-content: center;
  273. font-size: 26rpx;
  274. color: #444;
  275. font-weight: 400;
  276. line-height: 1;
  277. }
  278. .u-tab-item-active {
  279. position: relative;
  280. color: #000;
  281. font-size: 30rpx;
  282. font-weight: 600;
  283. background: #fff;
  284. }
  285. .u-tab-item-active::before {
  286. content: "";
  287. position: absolute;
  288. // border-left: 4px solid $u-type-primary;
  289. border-left: 4px solid #ff7410;
  290. height: 32rpx;
  291. left: 0;
  292. top: 39rpx;
  293. }
  294. .u-tab-view {
  295. height: 100%;
  296. }
  297. .right-box {
  298. background-color: rgb(250, 250, 250);
  299. }
  300. .page-view {
  301. padding: 16rpx;
  302. }
  303. .class-item {
  304. margin-bottom: 30rpx;
  305. background-color: #fff;
  306. padding: 16rpx;
  307. border-radius: 8rpx;
  308. }
  309. .item-title {
  310. font-size: 26rpx;
  311. // color: $u-main-color;
  312. color:#303133;
  313. font-weight: bold;
  314. }
  315. .item-menu-name {
  316. font-weight: normal;
  317. font-size: 24rpx;
  318. color:#303133;
  319. // color: $u-main-color;
  320. }
  321. .item-container {
  322. display: flex;
  323. flex-wrap: wrap;
  324. }
  325. .thumb-box {
  326. width: 33.333333%;
  327. display: flex;
  328. align-items: center;
  329. justify-content: center;
  330. flex-direction: column;
  331. margin-top: 20rpx;
  332. }
  333. .item-menu-image {
  334. width: 120rpx;
  335. height: 120rpx;
  336. }
  337. </style>


评价

uni-app开始时间与结束时间(结束时间大于开始时间)picker编写

利用uni-app官网里面picker插件代码当点击开始时间后结束时间要大于选择后的开始时间,点击结束时间后开始时间要小于开始时...

uni-app用法与html标签的变化

以前是html标签,比如,现在是小程序组件,比如。那么标签和组件有什么区别,不都是用尖括号包围起来一段英文吗?其实标签...

uni-app无法导入插件

点击网页中右上角的插件导入,可以打开项目,但是就是无法导入插件。是因为,要登录!hbuilder里边也要登录才行,他这个应...

使用uni-app的云端一体插件

初次使用uni-app的云端一体插件步骤还是有点多,也有一些坑,在这里把使用过程记录一下。 一:先下载需要的插件在插件右上...

uni-app中list插件图标问题

如图:他使用uni-icons实现的如果只有text就不会有图标有时候贴的文档不是太完善,还是要下一个完整的插件来对比一下

uni-app发起请求

代码如下:uni.request({ url:&#39;接口地址&#39;, success:(result)=&gt;{ my.lists=result.data; } })注意你的...

uni-app官方文档

https://uniapp.dcloud.io/

uni-app运行与调试

上边有菜单,可以运行到浏览器,也可以运行到内置浏览器运行到内置浏览器的效果:

uni-app引入外部资源引入外网样式

很简单代码如下:&lt;style&gt; @import&#39;http://image.tnblog.net/amazeui.min.css&#39;; &lt;/style&gt; &lt;st...

uni-app设置起始页

很简单,在pages.json里边配置即可。

uni-app页面无法跳转问题

首先检查一下有没有在pages里边配置没有配置不行的哦或者看看你的跳转方式对不对,如果跳转的是底部菜单,但是你用的是uni....

uni-app 删除失败 setting denied access to....

删除的时候如果文件夹下面还有页面,这样直接删除文件夹是会报错的:删除失败 setting denied access to....这种情况就直接...

uni-app中引入外部js

可以使用代码如下:varscript=document.createElement(&#39;script&#39;); script.src=&quot;http://image.tnblog.net/jqu...

uni-app dom操作

比如引入外部js需要用到的dom操作:varscript=document.createElement(&#39;script&#39;); script.src=&quot;http://image....

uni-app使用web-view引入页面

代码如下:&lt;template&gt; &lt;viewclass=&quot;h5-html&quot;&gt; &lt;web-view:webview-styles=&quot;webviewStyl...

uni-app真机调试

很简单只要手机开启了usb调试,然后hbuilderx会自动获取到手机的然后运行到手机即可华为手机开启use调试:一直点击版本号进...
旧年素颜,君记否
排名
15
文章
52
粉丝
1
评论
2
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术