微信小程序 slider的简单实例

微信小程序 slider的简单实例

实现效果图:

微信小程序slider应用,可加减的slider控制

<view class="control-w ">
  <block wx:for="{{controls}}" wx:key="id" wx:for-item="v">
   <view class="slide-item">
    <view class="itemName">{{v.name}}</view>
    <view class="slidewrap">
     <text class="s-con" data-id="{{v.id}}" bindtap="minusCount">-</text>
     <slider class="slide" bindchange="sliderchange" step="10" value="{{v.value}}" data-id="{{v.id}}" max="{{v.max}}" min="0" wx:if="{{index<3}}" />
     <slider class="slide" bindchange="sliderchange" step="10" value="{{v.value}}" data-id="{{v.id}}" max="{{v.max}}" min="0" wx:if="{{index==3}}" />
     <text class="s-con" data-id="{{v.id}}" data-max="{{v.max}}" bindtap="addCount">+</text>
    </view>
   </view>
  </block>
 </view>

页面结构

Page({
 data: {
  controls: [
   {
    id: 1,
    name: '功能一',
    value: 30,
    max: 60
   },
   {
    id: 2,
    name: '功能二',
    value: 30,
    max: 60
   },
   {
    id: 3,
    name: '功能三',
    value: 30,
    max: 60
   },
   {
    id: 4,
    name: '功能四',
    value: 50,
    max: 100
   }
  ]
 },
 
 // 控制加
 addCount: function (event) {
  let data = event.currentTarget.dataset
  let controls = this.data.controls
  let control = controls.find(function (v) {
   return v.id == data.id
  })
  let control1 = controls.find(function (v) {
   return v.max == data.max
  })
  
  if (control.value > control1.max)
   return
  control.value += 10;
  this.setData({
   'controls': controls
  })
 },
 // 控制减
 minusCount: function (event) {
  let data = event.currentTarget.dataset
  let controls = this.data.controls
  let control = controls.find(function (v) {
   return v.id == data.id
  })
  if (control.value <= 0)
   return
  control.value -= 10;
  this.setData({
   'controls': controls
  })
 },
 //拖动
 sliderchange: function (e) {
  let data = e.currentTarget.dataset
  let controls = this.data.controls
  let control = controls.find(function (v) {
   return v.id == data.id
  })
  this.setData({
   'controls': controls
  })
 
 }
})

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。