【黄啊码】微信小程序swipe切换及自适应高度问题解决

108 阅读3分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

我用的是医疗小程序挂号的例子,示例如图所示

image.png

image.png

<view>
    <view class="swiper-tab-list {{currentTab==0 ? 'on' : ''}}" data-current="0" bindtap="swichNav">挂号预约</view>
    <view class="swiper-tab-list {{currentTab==1 ? 'on' : ''}}" data-current="1" bindtap="swichNav">医生资料</view>
  </view>
  <swiper current="{{currentTab}}" class="swiper-box" duration="300" style="height:{{winHeight}}px" bindchange="bindChange">
    <swiper-item class="swiper0">
        <view class="zjtd_line"></view>
        <view style="padding: 20rpx;">
          <view class="yuyue_left"></view>
          <text>{{doctor_time}}{{week}}</text>
          <view class="yuyue_right"></view>
        </view>
        <view class="zjtd_line"></view>
        <view class="gh_list">
          <view class="item" wx:for="{{get_doctor_time}}" key="index">
            <view class="gh_left">
              <view>{{item.start_time}}</view><view>{{item.end_time}}</view>
            </view>
            <view class="gh_money">¥{{item.doctor_price}}</view>
            <view class="gh_num">{{item.see_num}}号</view>
            <button class="zjtd_btn" style="width:110rpx;">预约</button>
            <view class="zjtd_line" style="margin-bottom:10rpx;"></view>
          </view>
        </view>
    </swiper-item>
    <swiper-item class="swiper1">
        <view class="doctor_content" style="text-align:left;">
          简介:{{doctor_info.doctor_note}}
        </view>
    </swiper-item>
</swiper>

js代码:

data: {
    currentTab: 0,
}
 
  //切换代码
  swichNav: function( e ) {
      var that = this;
      if( this.data.currentTab === e.target.dataset.current ) {
          return false;
      } else {
          that.setData( {
              currentTab: e.target.dataset.current
          })
          this.getElementHeight(e.target.dataset.current)
      }
  },
  //动态获取高度
  getElementHeight(element) {
      //创建节点选择器
      var query = wx.createSelectorQuery();
      //选择id
      var that = this;
      if(element==0){
        query.select(".gh_list").boundingClientRect(function (rect) {
        that.setData({
          winHeight: rect.height+50//这里加的50是日期位置的高度,没有这个位置的可忽略
        })
        }).exec();
      }else{
        query.select(".doctor_content").boundingClientRect(function (rect) {
        that.setData({
          winHeight: rect.height
        })
        }).exec();
      }
  },
 
  bindChange: function( e ) {
      var that = this;
      that.setData( { currentTab: e.detail.current });
  
  },
  onLoad: function (options) {
    //这里利用setdata的回调函数获取已经渲染的当前切换栏目的高度
    this.setData({get_doctor_time:res.data},function(){
        this.getElementHeight(this.data.currentTab);
    });
  }

对于css,一个一个挑,太麻烦了,所以这里我直接全部晒出来

/* pages/doctor/index.wxss */

  1. .doctor{
  2. padding: 30rpx;
  3. overflow: hidden;
  4. background-color: #fdfdfd;
  5. }
  6. .doctor_img{ width: 120rpx;height: 120rpx; margin: 10rpx 4rpx 0rpx; border-radius: 5rpx; overflow: hidden;}
  7. .doctor_img img{width:100%;height:100%;object-fit:cover;}
  8. .doctor_word{width:75%;color:#267377;margin-left:10rpx;}
  9. .doctor_p{line-height: 48rpx; float: left;}
  10. .b_gray{background:#ccc;}
  11. .doctor_dir {
  12. margin: 15rpx 0rpx 0rpx 0rpx;
  13. line-height: 43rpx;
  14. text-indent: 20rpx;
  15. float: left;
  16. word-break: break-all;
  17. }
  18. .dir_height{line-height:43rpx;}
  19. .doctor_left{width:94%;float:left;}
  20. /* tab样式开始 */
  21. .main{
  22. padding: 20rpx;
  23. }
  24. .swiper-tab{
  25. display: flex;
  26. margin-bottom: 10rpx;
  27. }
  28. .swiper-tab-list{
  29. display: inline-block;
  30. width: 49.5%;
  31. color: #777777;
  32. text-align: center;
  33. margin-bottom: 10px;
  34. color: #ffffff;
  35. border-bottom: 1px solid #395f64;
  36. padding: 20rpx;
  37. background-color: #395f64;
  38. border-radius: 10rpx;
  39. margin-right: 3rpx;
  40. }
  41. .on{
  42. color: #ffffff;
  43. border-bottom: 1px solid #c70000;
  44. padding: 20rpx;
  45. background-color: #c70000;
  46. }
  47. .swiper-box{
  48. width: 100%;
  49. height: auto;
  50. }
  51. .swiper-box view{
  52. text-align: center;
  53. }
  54. /* tab样式结束 */
  55. .yuyue_left{
  56. float:left;
  57. }
  58. .yuyue_right{
  59. float:right;
  60. }
  61. .gh_list{
  62. margin-top:20rpx;
  63. }
  64. .gh_left{
  65. float: left;
  66. width:25%;
  67. }
  68. .gh_left view {
  69. background-color: #fbfbfb;
  70. color: #000;
  71. padding: 8rpx;
  72. margin-bottom: 8rpx;
  73. border: 1px solid #e6e6e6;
  74. border-radius: 10rpx;
  75. }
  76. .gh_money{
  77. float:left;
  78. width:25%;
  79. text-align:center;
  80. margin-top: 35rpx;
  81. }
  82. .gh_num{
  83. float:left;
  84. width:25%;
  85. text-align:center;
  86. margin-top: 35rpx;
  87. }
  88. .zjtd_btn {
  89. font-size: 28rpx;
  90. color: white;
  91. padding: 2rpx;
  92. font-weight: unset;
  93. background-color: #d20303;
  94. top: 25rpx;
  95. }
  96. .spfl{
  97. padding: 25rpx;
  98. font-size: 28rpx;
  99. color:#ffffff;
  100. background-color: #395f64;
  101. border-bottom:1rpx solid #d4d4d4;
  102. overflow: hidden;
  103. }
  104. .zkfl_lj{
  105. float: right;
  106. }
  107. .doctor_list{
  108. text-align: center;
  109. overflow: hidden;
  110. }
  111. .doc_one{
  112. width:25%;
  113. float: left;
  114. }
  115. .doctor_imgs{
  116. width:100%;
  117. height: 120rpx;
  118. margin: 30rpx 0rpx 13rpx;
  119. border-radius: 5rpx;
  120. overflow: hidden;
  121. }
  122. .doctor_txt{
  123. text-align:center;
  124. }
  125. .doctor_content{
  126. color:#333333;
  127. text-align: left;
  128. line-height: 40rpx;
  129. float: left;
  130. padding: 15rpx;
  131. } 啊码整理了一部分小程序的案例,比较适合入门的朋友入手,如果需要可以从该链接【1】链接【2】进入

image.png

image.png

image.png