欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信小程序 slider的简单实例

程序员文章站 2022-07-06 12:28:42
微信小程序 slider的简单实例 实现效果图: 微信小程序slider应用,可加减的slider控制

微信小程序 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
  })
 
 }
})

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