轻源码

  • QingYuanMa.com
  • 全球最大的互联网技术和资源下载平台
搜索
一起源码网 门户 微信小程序 查看主题

微信小程序开发之选项卡,swiper实现块滑动

发布者: ldzhang | 发布时间: 2018-7-15 17:06| 查看数: 5958| 评论数: 1|帖子模式

一:微信小程序开发之选项卡

 
选项卡是web开发中经常使用到的一个模块,在小程序中竟然没有,这里参考别人的文章自己做了一个双选项卡

实现思路:

通过绑定swichNav事件来控制currentTab(当前选项卡)和isShow(是否显示),达到切换展示电影和游戏的目的

代码:

1.index.wxml

  1. <!--index.wxml-->
  2. <view class="swiper-tab">
  3. <view class="swiper-tab-list {{currentTab==0 ? 'on' : ''}}" data-current="0" bindtap="swichNav">电影</view>
  4. <view class="swiper-tab-list {{currentTab==1 ? 'on' : ''}}" data-current="1" bindtap="swichNav">游戏</view>
  5. </view>
  6. <view class="weui-cells__title" hidden="{{!isShow}}">电影列表</view>
  7. <view class="weui-cells__title" hidden="{{isShow}}">游戏列表</view>

2.index.wxss

  1. .swiper-tab {
  2. width: 100%;
  3. text-align: center;
  4. line-height: 80rpx;
  5. background-color:white;
  6. }
  7. .swiper-tab-list {
  8. font-size: 30rpx;
  9. display: inline-block;
  10. width: 50%;
  11. color: #777;
  12. border-bottom: 0rpx;
  13. }
  14. .on {
  15. color: #da7c0c;
  16. border-bottom: 2rpx solid #da7c0c;
  17. }
  18. .swiper-box {
  19. display: block;
  20. height: 100%;
  21. width: 100%;
  22. overflow: hidden;
  23. }
  24. .swiper-box view {
  25. text-align: center;
  26. }

3.index.js

  1. //index.js
  2. //获取应用实例
  3. var app = getApp()
  4. Page( {
  5. data: {
  6. isShow: true,
  7. currentTab: 0,
  8. },
  9. swichNav: function (e) {
  10. if (this.data.currentTab === e.target.dataset.current) {
  11. return false;
  12. } else {
  13. var showMode = e.target.dataset.current == 0;
  14. this.setData({
  15. currentTab: e.target.dataset.current,
  16. isShow: showMode
  17. })
  18. }
  19. },
  20. })

参考文章:

二:swiper实现块滑动

 

  1. <view class="container" style="height: {{ windowHeight }}px;">
  2. <view class="find-car-header">
  3. <view class="tab">
  4. <view bindtap="tabItemClick" id="tabItem_0" class="tab-item {{tabIndex == 0 ? 'active' : ''}}">页签1</view>
  5. <view bindtap="tabItemClick" id="tabItem_1" class="tab-item {{tabIndex == 1 ? 'active' : ''}}">页签2</view>
  6. </view>
  7. </view>
  8. <swiper bindchange="swiperChange" current="{{tabIndex}}" duration="600" class="content">
  9. <swiper-item class="swiper-item">
  10. <view class="letter-brand-list">
  11. <view class="letter-brand-item">
  12. <text>Artega</text>
  13. </view>
  14. <view class="letter-brand-item">
  15. <text>阿斯顿·马丁</text>
  16. </view>
  17. <view class="letter-brand-item">
  18. <text>奥迪</text>
  19. </view>
  20. <view class="letter-brand-item">
  21. <text>阿尔法·罗密欧</text>
  22. </view>
  23. </view>
  24. </swiper-item>
  25. <swiper-item class="swiper-item">
  26. <view class="letter-brand-list">
  27. <view class="letter-brand-item">
  28. <text>Artega</text>
  29. </view>
  30. <view class="letter-brand-item">
  31. <text>阿斯顿·马丁</text>
  32. </view>
  33. <view class="letter-brand-item">
  34. <text>奥迪</text>
  35. </view>
  36. <view class="letter-brand-item">
  37. <text>阿尔法·罗密欧</text>
  38. </view>
  39. </view>
  40. </swiper-item>
  41. </swiper>
  42. </view>

//index.js

  1. var app = getApp();
  2. Page({
  3. data: {},
  4. customData: {},
  5. onLoad: function (params) {
  6. //页面加载完成提示
  7. this.data.styles = {};
  8. this.data.styles.curtainViewWidth = 590;
  9. this.data.styles.curtainViewRight = -(this.data.styles.curtainViewWidth + 100);
  10. this.data.tabIndex = 0;
  11. this.data.list = new Array(3);
  12. this.changeTab(0);
  13. //获取系统信息
  14. var that = this;
  15. wx.getSystemInfo({
  16. success: function (info) {
  17. that.data.windowHeight = info.windowHeight;
  18. that.setData(that.data);
  19. console.log(that.data.windowHeight);
  20. }
  21. });
  22. },
  23. tabItemClick: function (event) {
  24. this.changeTab(parseInt(event.currentTarget.id.split("_")[1]));
  25. },
  26. swiperChange: function (event) {
  27. this.changeTab(event.detail.current);
  28. },
  29. changeTab: function (index) {
  30. this.data.tabIndex = index;
  31. this.setData(this.data);
  32. }
  33. });

//index.wxss

  1. /**index.wxss**/
  2. .find-car-header {
  3. position: fixed;
  4. top: 0;
  5. left: 0;
  6. z-index: 9999;
  7. width: 690rpx;
  8. height: 88rpx;
  9. padding: 0 30rpx;
  10. background-color: #eee;
  11. border-bottom: 2rpx solid #d2d2d2;
  12. }
  13. .tab {
  14. float: left;
  15. width: 330rpx;
  16. height: 88rpx;
  17. }
  18. .tab .tab-item {
  19. float: left;
  20. width: 140rpx;
  21. height: 88rpx;
  22. font-size: 30rpx;
  23. line-height: 88rpx;
  24. text-align: center;
  25. color: #333;
  26. }
  27. .tab .tab-item:first-child {
  28. margin-right: 50rpx;
  29. }
  30. .tab .active {
  31. position: relative;
  32. color: #0f69d7;
  33. }
  34. .tab .active:after {
  35. position: absolute;
  36. left: 0;
  37. bottom: 0;
  38. display: block;
  39. width: 100%;
  40. height: 4rpx;
  41. content: '';
  42. background: #0f69d7;
  43. }
  44. .content {
  45. z-index: 0;
  46. width: 750rpx;
  47. height: 100%;
  48. box-sizing: border-box;
  49. }
  50. .swiper-item {
  51. width: 100%;
  52. overflow-x: hidden;
  53. }
  54. .letter-brand-list {
  55. width: 720rpx;
  56. padding-left: 30rpx;
  57. background-color: #fff;
  58. }
  59. .letter-brand-item {
  60. width: 690rpx;
  61. height: 90rpx;
  62. padding: 10rpx 30rpx 10rpx 0;
  63. border-bottom: 1px solid #f0f0f0;
  64. font-size: 28rpx;
  65. line-height: 90rpx;
  66. color: #333;
  67. }
  68. .letter-brand-list .letter-brand-item:last-child {
  69. border: 0;
  70. }

最新评论

°5386 发表于 2022-7-6 14:52
纯音乐是怎么做出来的

轻源码让程序更轻更快

QingYuanMa.com

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表