微信小程序 用swiper实现一个滑动刻度尺(体重选择器)

本文介绍了如何使用微信小程序的swiper组件创建一个具有缓冲和选择功能的刻度尺组件。通过设置数组并动态更新数据,实现在滑动时改变刻度线的值,同时确保最小值和最大值可以滑动到中间位置。组件设计包括了样式、事件监听和数据传递,可以作为可复用组件使用,并在页面中通过事件获取当前滑动的值。
摘要由CSDN通过智能技术生成

效果图

在这里插入图片描述

思路:

利用微信swiper组件实现滑动效果,创建一个数组arr,先存启始数据进去,然后分别在前面存放起始数–的数据,后面添加起始数据++的数据,循环数组arr创建swiper-item,每一个swiper-item都是一个小刻度线,达到缓冲和选择数据的效果,滑动的时候开始监听不停改变起始值,思路成立开始实践。

实践:

打算直接做成一个复用的组件

wxml:

swiper属性:
display-multiple-items:当前显示的swiper-item数
current:当前滑动swiper-item个数
bindchange:滑动时触发的函数

<view style="height:165rpx;positive:relative;">
  <view style="margin-bottom:20rpx;font-size:24rpx;position:absolute;width:570rpx;text-align: center;">
    <text style="color: #4377E9 " ><text style="font-size: 44rpx;font-family: DIN-Bold, DIN;font-weight: bold;color: #4377E9 ;line-height: 54rpx;">{{currentNumber}}</text> {{ unit }}</text>
    <text class="line"></text>
  </view>
  <swiper duration="10" class="swiperss" bindchange="getCurrent" display-multiple-items="{{multipleItems}}" easing-function = "linear" current="{{ current }}">
    <swiper-item class="swiperItems" wx:for="{{multipleItems + offset * 2}}" wx:key="index">
      <view wx:if="{{arr[item]!=''}}"   class="{{arr[item]%5==0?'linestwo':'lines'}}" ></view>
      <view style="color: #BBBEC3;">{{arr[item]%5==0?arr[item]:''}}</view>
    </swiper-item>
  </swiper>
</view>

CSS


/* components/heightAndWeight/index.wxss */
.swiperss{
  height: 100%;
  width: 100%;
  margin: 0 auto;
  overflow: visible;
}
.swiperItems{

  font-size:24rpx;
  position:relative;
  margin-top: 74rpx;
  border-top:1px solid #F5F7F9; 
  height: 120rpx !important;
  width: calc(570rpx / 19) !important;
  overflow: visible;
}
.swiperItems > .lines{
  background-color:#D2D6DF  ;
  margin-bottom: 10rpx;
  width:1px;height:35rpx;
  margin-left: 15rpx;
}
.linestwo{
  margin: 0 auto;
  width:1px;height:63rpx;
  background-color:#D2D6DF ;
  margin-left: 16rpx;
}
.lines + view{
  font-size: 24rpx;
  font-family: DIN-Regular, DIN;
  font-weight: 400;
  color: #D9D9D9;
  line-height: 30rpx;
  width: 100%;
  text-align: center;
}
.line{
  position: absolute;
  left:50.4%;
  top: 64rpx;
  transform: translateX(-50%);
  width: 5rpx;
  height: 40rpx;
  background: #43A3FF;
  box-shadow: 0px 0px 2rpx 0px #43A3FF;
  z-index: 6;
  margin-right: 10rpx;

}

js

字传父数值:
min:刻度尺最小值
max:刻度尺最大值
unit:中间的固定线的单位
currentNumber:刻度线的起始默认值
multipleItems:swiper可视区域的swiper-item有多少
offset:我们的选择指针在中间,在滑动到最小值或者最大值时最小的值反而在两边,我们需要在前后补上空白的刻度尺,让最小值或者最大值滑动到中间来
一个前面说的arr数组个数等于 multipleItems的个数+偏差值*2 然后循环就可以了

// components/heightAndWeight/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    min: {
      type: Number
    },
    max: {
      type: Number
    },
    unit: {
      type: String
    },
    currentNumber: {
      type: Number
    },
    multipleItems: {
      type: Number,
      value: 9
    },
    offset: {
      type: Number,
      value: 4
    }
  },
  observers: {
    "current": function (current) {
      console.log('current-----currentNumber---', current, this.data.currentNumber)
      if (current < this.data.offset) {
        this.setData({
          currentNumber: Math.max(this.data.currentNumber + current - this.data.offset, this.data.minNumber)
        })
      } else if (current > this.data.offset) {
        this.setData({
          currentNumber: Math.min(this.data.currentNumber + current - this.data.offset, this.data.maxNumber)
        })
      }
    },
    "currentNumber": function (currentNumber) {
      console.log('----currentNumber', currentNumber)
      let arr = []

      for (let l = parseInt(this.data.multipleItems / 2) + this.data.offset; l > 0; l--) {
        arr.push(currentNumber - l >= this.data.minNumber ? currentNumber - l : '')
      }

      arr.push(currentNumber)

      for (let l = 1; l <=  parseInt(this.data.multipleItems / 2) + this.data.offset; l++) {
        arr.push(currentNumber + l <= this.data.maxNumber ? currentNumber + l : '')
      }
      console.log('-----arr', arr)

      this.setData({
        arr,
        current: this.data.offset
      })

    }
  },
  attached() {
    console.log('this.dddddddddddd', this.data.currentNumber)
    this.setData({
      minNumber: this.data.min,
      maxNumber: this.data.max,
      current: 0,
     
      arr: [],
    })
    
  },
  /**
   * 组件的初始数据
   */
  data: {
    minNumber: null,
    maxNumber: null,
    current: 0,
  
  
    arr: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getCurrent(e) {
      this.setData({
        current: e.detail.current
      })
      console.log('eeeeeeeeeeeeee', e.detail.current, this.data.currentNumber)
      this.triggerEvent('currentNumber', {
        current: this.data.currentNumber
      })
    }
  }
})

监听currentNumber的值根据根据这个值创造出循环arr的数组,这样就可以不用创建一大堆数据,直接动态创建数据,再监听刻度尺的滑动,根据他的往左往右 改变currentNumber的值,到最小或者最大时停止。

最后抛出getCurrent函数 把currentNumber值抛出,在页面上就可以拿到当前滑动的值随心所欲啦

组件使用页面wxml:

<view class="flex" style="flex-wrap:wrap;margin-top: 20rpx">
    <height-weight3    offset="10"    min="1" max="150" unit=""  multipleItems="19"    currentNumber="{{ ageCurrentNumber }}"  style="width:570rpx;margin: 0 auto;" bind:currentNumber="getCurrentNumberAge"></height-weight3>
  </view>

js

  getCurrentNumberAge(e){  //年龄
    console.log('获取当前current值年龄',e,e.detail.current)

    let result = e.detail.current;
    this.setData({
      age:result 
    })
  },

完美收工 最后感谢王同志的指导

  • 4
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
当然可以!下面是一个简单的微信小程序BMI计算器的代码示例: wxml文件(index.wxml): ```html <view class=""> <view class="title">BMI计算器</view> <view class="input-container"> <text>身高(cm):</text> <input bindinput="onHeightChange" type="number" placeholder="请输入身高" /> </view> <view class="input-container"> <text>体重(kg):</text> <input bindinput="onWeightChange" type="number" placeholder="请输入体重" /> </view> <view class="result">您的BMI指数为:{{bmi}}</view> </view> ``` wxss文件(index.wxss): ```css .container { display: flex; flex-direction: column; align-items: center; margin-top: 100rpx; } .title { font-size: 32rpx; margin-bottom: 30rpx; } .input-container { display: flex; align-items: center; margin-bottom: 20rpx; } .result { font-size: 28rpx; margin-top: 30rpx; } ``` js文件(index.js): ```javascript Page({ data: { height: '', weight: '', bmi: '' }, onHeightChange(event) { this.setData({ height: event.detail.value }); this.calculateBMI(); }, onWeightChange(event) { this.setData({ weight: event.detail.value }); this.calculateBMI(); }, calculateBMI() { const height = parseFloat(this.data.height); const weight = parseFloat(this.data.weight); if (height && weight) { const bmi = (weight / ((height / 100) ** 2)).toFixed(2); this.setData({ bmi: bmi }); } else { this.setData({ bmi: '' }); } } }) ``` 这个示例中,我们使用了一个输入框来获取用户输入的身高和体重,然后根据用户输入的数值计算BMI指数,并将结果显示在页面上。
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值