鳕鱼天空

This is Mr Wang's Tech Blog.

微信小程序— slider(滑动选择器)

今天来介绍一下slider(滑动选择器):

官方文档:https://mp.weixin.qq.com/debug/wxadoc/dev/component/slider.html

1.效果图如下,

2.index.js中:

//index.js
//获取应用实例
const app = getApp()
var that;
var isDisabled = true;
Page({
  data: {
    //默认最小值20;
    min: 20,
    //默认最大值20;
    max: 100,
    //设置不禁用
    disabled: false,
    //设置选中默认颜色
    colorSelect: '#00ff00',
    //设置背景颜色
    backgroundColor: "#ccc",
    //设置滑块的大小
    blockSize: 28,
    //设置滑块的颜色
    blockColor: "#eeeeee",
    //是否显示当前 value
    showValue: true,
    //步长
    step: 5,
  },

  onLoad: function () {
    that = this;
  },
  //设置最小值
  minInput: function (e) {
    that.setData({
      min: e.detail.value,
    })
  },
  //设置最大值
  maxInput: function (e) {
    that.setData({
      max: e.detail.value,
    })
  },
  //设置是否禁用滑动
  btnDisabled: function (e) {
    if (isDisabled == true) {
      isDisabled = false;
      that.setData({
        disabled: true,
      })
    } else {
      isDisabled = true;
      that.setData({
        disabled: false,
      })
    }
  },
  //已选择的颜色
  colorSelectInput: function (e) {
    that.setData({
      colorSelect: e.detail.value,
    })
  },
  //设置背景条的颜色
  bgColorInput: function (e) {
    that.setData({
      backgroundColor: e.detail.value,
    })
  },
  //设置滑块的大小
  blockSizeInput: function (e) {
    that.setData({
      blockSize: e.detail.value,
    })
  },
  //设置步长,取值必须大于 0,并且可被(max - min)整除
  stepInput: function (e) {
    that.setData({
      step: e.detail.value,
    })
  },
  //设置滑块的颜色
  blockColorInput: function (e) {
    that.setData({
      blockColor: e.detail.value,
    })
  },
  //设置是否显示当前value
  btnshowValue: function () {
    that.setData({
      showValue: false,
    })
  },
  //完成一次拖动后触发的事件
  sliderchange: function (e) {
    wx.showToast({
      title: "拖动后触发" + e.detail.value,
      duration: 1000
    })
  },
  //拖动过程中的触发的事件
  bindchanging: function (e) {
    wx.showToast({
      title: "拖动中触发" + e.detail.value,
      duration: 1000
    })
  },
})

3.index.wxss中:

/*输入框样式  */

.section {
  width: 93%;
  margin: 20rpx;
  background-color: #ccc;
  padding: 10rpx;
  font-size: 30rpx;
  border-radius: 5px;
}

/*button样式  */

.button {
  width: 95%;
  margin: 20rpx;
}

4.index.wxml中:

<view class="section_gap">
  <text class="section__title">使用示例:</text>
  <view class="body-view">
    <slider bindchange="sliderchange" bindchanging="bindchanging" min="{{min}}" max="{{max}}" disabled="{{disabled}}" activeColor='{{colorSelect}}' backgroundColor='{{backgroundColor}}' block-size='{{blockSize}}' block-color='{{blockColor}}' step="{{step}}" show-value='{{showValue}}'/>
  </view>
</view>

<view class="section">
  <input placeholder="设置最小值,默认为20" type="number" bindinput="minInput" confirm-type="done" focus/>
</view>

<view class="section">
  <input placeholder="设置最大值,默认为100" type="number" bindinput="maxInput" confirm-type="done" focus/>
</view>

<view>
  <view class='text'>设置已选择的颜色(请使用十六进制颜色值,例如:#ff00ff)</view>
  <input class="section" placeholder="" value="#" bindinput="colorSelectInput" confirm-type="done" focus/>
</view>

<view>
  <view class='text'>设置背景条的颜色(请使用十六进制颜色值,例如:#ff00ff)</view>
  <input class="section" placeholder="" value="#" bindinput="bgColorInput" confirm-type="done" focus/>
</view>

<view>
  <view class='text'>设置滑块的颜色(请使用十六进制颜色值,例如:#ff00ff)</view>
  <input class="section" placeholder="" value="#" bindinput="blockColorInput" confirm-type="done" focus/>
</view>

<view class="section">
  <input placeholder="设置滑块的大小,取值范围为12 - 28" type="number" bindinput="blockSizeInput" confirm-type="done" focus/>
</view>

<view class="section">
  <input placeholder="步长,取值必须大于 0,并且可被(max - min)整除" type="number" bindinput="stepInput" confirm-type="done" focus/>
</view>

<button class='button' catchtap='btnDisabled'>设置/取消禁用</button>
<button class='button' catchtap='btnshowValue'>隐藏当前value</button>
  •  

--------------------- 本文来自 Afanbaby 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/afanbaby/article/details/79206852?utm_source=copy

添加评论

Loading