美文网首页
如何写一个滑块区间选择控件

如何写一个滑块区间选择控件

作者: 文子产品笔记 | 来源:发表于2018-09-18 09:59 被阅读0次
最近要写一个滑块人数区间选择的控件,其实本来不算难,知识由于刚接触小程序,很多都语法不太熟所以花了点时间琢磨了下,具体效果看下面图: WechatIMG29.jpeg

整个滑块样式分为三部分,上面刻度,中间线条,下边滑块,所以只要对css样式熟练点就能写出来,另外需要监听滑块的move事件,代码如下:

wcss代码:

.content{
  display: flex;
  flex-direction: column;
  margin-left: 30px;
  margin-right: 30px;
}

.filter_content{
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.barview{
  display: flex;
  height: 30px;
  justify-content: center;
}

.bar{
  position: relative;
  width: 100%;
  height: 30px;
}

.btn1{
  position: absolute;
}

.person{
  display: flex;
  height: 30px;
  justify-content: center;
}

.line-img{
  background: gray;
  width: 100%;
  height: 1px;
}

.line{
  display: flex;
  justify-content: center;
}

.kedus{
  width: 24px;
  line-height: 50rpx;
  text-align: center;
  background-size: 24px 30px;
  padding-top: 5rpx;
}

.no_sel_text{
  color: gray;
  font-size: 12px;
}

.sel_text{
  color: #fff;
  font-size: 9px;
}

.kedu{
  display: flex;
  width: 100%;
  justify-content: space-between;
  position: relative;
}

.kedu_img{
  width: 24px;
  height: 50rpx;
}

.showview{
  display: flex;
  justify-content: center;
}

.showtext{
  color: #ff0000;
}

wxml代码:

<view class='filter_content'>
      <!-- 刻度 -->
      <view class='person'>
        <view class='kedu'>
          <block wx:for="{{kedu}}" wx:key="{{index}}">
            <view class="kedus {{(index==start_index||index == end_index)?'sel_text':'no_sel_text'}}'"   style="background-image: url({{(index == start_index || index == end_index)?'http://p157c6vbr.bkt.clouddn.com/person_sel_point.svg':''}});">{{item}}</view>
          </block>
        </view>
      </view>

      <!-- 线条 -->
      <view class='line'>
        <image class='line-img'></image>
      </view>

      <!-- 滑动条 -->
      <view class='barview'>
        <view class='bar'>
          <image class='btn1' src="../../images/screen_number.svg" style="width:24px;height:30px; margin-left:{{ballLeft}}px;" bindtouchmove='movestart' bindtouchend='moveend'></image>
          <image class='btn1' src="../../images/screen_number.svg" style="width:24px;height:30px; right:{{ballLeft1}}px;" bindtouchmove='movestart1' bindtouchend='moveend1'></image>
        </view>
      </view>
    </view>

js代码:

  data: {
    screenHeight: null,
    screenWidth: null,
    ballLeft: 0,
    ballLeft1: 0,
    kedu: [1, 4, 6, 10, 12, 20, 50, 200, 300],
    kedutext1: 1,
    kedutext2: 300,
}
  onLoad: function (options) {
    var _this = this;
    wx.getSystemInfo({
      success: function (res) {
        _this.setData({
          screenHeight: res.windowHeight,
          screenWidth: res.windowWidth,
        });
      }
    });
  },


  movestart: function (e) {
    var touchs = e.touches[0];
    var pageX = touchs.pageX;
    var pageY = touchs.pageY;

    // //防止坐标越界,view宽高的一般  
    if (pageX < 0) return;
    if (pageX > this.data.screenWidth) return;

    //这里用right和bottom.所以需要将pageX pageY转换  
    var x = pageX;
    var y = pageY;
    var a = x / this.data.screenWidth;
    var index = parseInt(this.data.kedu.length * a);

    var ke = ((this.data.screenWidth - 60) - (this.data.kedu.length*24)) / (this.data.kedu.length - 1);
    var x1 = index * (ke+24);
    if (this.data.kedu[index] >= this.data.kedutext2 - 1){
    }else{
      this.setData({
        ballLeft: x1,
        kedutext1: this.data.kedu[index], 
        start_index: index
      });
    }
  },

  
  movestart1: function (e) {
    var touchs = e.touches[0];
    var pageX = touchs.pageX;
    var pageY = touchs.pageY;

    // //防止坐标越界,view宽高的一般  
    if (pageX < 0) return;
    if (pageX > this.data.screenWidth) return;
    //这里用right和bottom.所以需要将pageX pageY转换  
    var x = this.data.screenWidth - pageX;
    var y = this.data.screenHeight - pageY;
    var a = x / this.data.screenWidth;
    var index = parseInt(this.data.kedu.length * a);

    var ke = ((this.data.screenWidth - 60) - (this.data.kedu.length * 24)) / (this.data.kedu.length - 1);
    var x1 = index * (ke + 24);
    if (this.data.kedu[this.data.kedu.length - index - 1] <= this.data.kedutext1 + 1) {
    } else {
      this.setData({
        ballLeft1: x1,
        kedutext2: this.data.kedu[this.data.kedu.length - index - 1],
        end_index: this.data.kedu.length - index - 1
      });
    }
  },
  
  //块1滑动结束
  moveend:function(e){
    this.loadUIData();
  },

  //块2滑动结束
  moveend1: function (e) {
    this.loadUIData();
  },

因为没有写独立demo,所以没有把js代码整部分贴上来,不过核心代码就是movestart计算这块了。

相关文章

  • 如何写一个滑块区间选择控件

    整个滑块样式分为三部分,上面刻度,中间线条,下边滑块,所以只要对css样式熟练点就能写出来,另外需要监听滑块的mo...

  • UISlider控件

    UISlider控件 UISlider控件,滑块控件(调节声音,屏幕亮度,播放进度,字体大小 滑块当前位置代表的数...

  • IOS UISlider,UIStepper

    UISlider滑块控件 //滑块控件 事件驱动型 应用场景:(调节音量、亮度等),高度34,固定不变 //设置最...

  • Android开始-结束时间选择控件

    控件说明:区间时间选择控件(仅支持时间的选择,日期默认为当天的系统日期),可以选择一个开始时间与结束时间。可以设置...

  • 滑块区间插件

    https://github.com/IonDen/ion.rangeSlider

  • 滑块控制的4个创意概念

    滑块已经存在很长一段时间了,并且事实上已经成为了标准UI控件,用于选择一个值或者一系列值。滑块很有用,因为它允许用...

  • IOS开关控件,分段控件和滑块控件的使用方法

    那么我们将如何使用代码来实现开关控件,分段控件和滑块控件,将是我们今天的主要学习内容。 开关控件UISwitch ...

  • UIslider滑块控件

    UIslider滑块控件在IOS开发中会常用到,可用于调节音量,字体大小等UI方面的交互,用法总结如下: 初始化一...

  • Flat风格的Qml范围滑块

    基于Qml的RangeSlider控件修改而成。 范围滑块代码 范围滑块样式代码 更多精彩内容请关注公众号Qt君。

  • jRange使用心得(动态修改滑块的值)

    最近的项目中需要使用到滑块去选择范围区间,手写的话,项目时间来不及,所以就在网上查了下,发现基于jQuery的插件...

网友评论

      本文标题:如何写一个滑块区间选择控件

      本文链接:https://www.haomeiwen.com/subject/qdgjnftx.html