记录:微信小程序半圆滑块(基于react taro实现)

 

1、画两个半圆 一个旋转180°至不可见

render() {
  
    let orignAngle = this.orignAngle,
      Longtouch = this.state.Longtouch
    let Angle = `transform: rotate(${orignAngle}deg);transition: all ${Longtouch ? ' 0.2s' : ' 0.5s'}`
    return (
      <View className="slider" onTouchEnd={() => this.touchEnd()} onTouchMove={this.touchMove}  >
        <View className="activeSlider-set">
          <View className="activeSlider" style={Angle}>
          {/* <View className="bgcBorder borderLeft activeColor"></View>
          <View className="bgcBorder borderRight activeColor"></View> */}
          </View>
        </View>
        <View className="orign" id="orign">
          <View className="long" style={Angle}>
            <View className="circle shadow" onTouchStart={this.handleLongPress}></View>
          </View>
        </View>
        <View className="sliderBgc">
          {/* <View className="bgcBorder borderLeft BGCCOLOR"></View>
          <View className="bgcBorder borderRight BGCCOLOR"></View> */}
        </View>

        <View className="flexCenter btn_air_bar">
          <View className="btn_air " onClick={() => this.lose()}>-</View>
          <View className="val">
            <View className="val_text">{this.props.val}</View>
            <View className='monad'>℃</View>
          </View>
          <View className="btn_air " onClick={() => this.add()}>+</View>

        </View>
      </View>
    )
  }

css

$color-brand: #12A5FF;
$borderColor:#DCDCDC;                 
$sliderWidth:20px;
$long:168px;
.bgcBorder{
  width: $sliderWidth;
  height: $sliderWidth;
 
  position: absolute;
  border-radius: 50%;
}
.BGCCOLOR{
  background-color: $borderColor;
}
.activeColor{
  background-color: $color-brand;
}
.borderLeft{
  bottom: -($sliderWidth/2);
  left: -($sliderWidth);
}
.borderRight{
  bottom: -($sliderWidth/2);
  right: -($sliderWidth);
}
.slider {
  width: ($long+$sliderWidth)*2;
    height: $long+$sliderWidth;
  // overflow: hidden;
  margin: 134px auto;
  position: relative;
  padding-bottom: $sliderWidth/2;
  .sliderBgc {
    width: $long*2;
    height: $long;
    position: absolute;
    bottom: 0;
    left: 0;
    border: $sliderWidth solid;
    border-radius: 50% 50% 0 0/100% 100% 0 0;
    border-color: $borderColor;
    border-bottom: none;
   
  }


  .activeSlider-set{
    position: absolute;
    width: ($long+$sliderWidth)*2;
    height: $long+$sliderWidth;
    left: 0;
    bottom: 0;
    z-index: 2;
    
    overflow: hidden;
    .activeSlider {
      bottom: 0;
      left: 0;
      width: $long*2;
      height: $long;
      border: $sliderWidth solid;
      border-color: $color-brand !important;
      border-radius: 50% 50% 0 0/100% 100% 0 0;
      border-bottom: none;
      transform: rotate(-100deg);
      transform-origin: $long+$sliderWidth $long+$sliderWidth;
      
    }
  }
  .orign {
    width: 0;
    height: 0;
    position: absolute;
    background-color: rgba(0, 0, 0, 0.1);
    bottom: 0;
    left: 50%;
    z-index: 11;
    transform: translateX(50%);
  }

  .long {
    width: $long+$sliderWidth/2;
    height:0;
    z-index: 9999;
    position: absolute;
    top: 0;
    left: 0;
    transform-origin: 0 0;
    .circle {
      width: 56px;
      height: 56px;
      border-radius: 50%;
      position: absolute;
      top: 50%;
      right: 0;
      transform: translate(50%, -50%);
      background-color: #fff;
      z-index: 999;
    }
  }
}

.btn_air_bar{
  position: absolute;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  z-index: 99;
}

根据值 计算角度 

get orignAngle (){

   return this.getAngleByVal(this.props.val)

  }

 // 根据值设置角度

  getAngleByVal(item) {

    return (item - 16) * 180 / 16 - 180

  }

  // 根据当前角度设置值(16:因为做的是空调的温度滑块16-32 所以这里是直接写死的16)

  setValByAngle(orignAngle) {

    if (orignAngle >= 0) {

      let angle = Math.abs(orignAngle - 180)

      let val = Number(((180 - angle) / 180 * 16).toFixed()) + 16

      this.props.setVal(val)

    }

  }

// 初次触摸

  handleLongPress=(e)=> {

    var Longtouch = this.state.Longtouch

    if (!Longtouch) {

      let lng = e.changedTouches[0]

      let targetOrign = {

        x2: lng.pageX,

        y2: lng.pageY

      }

      this.setState({

        Longtouch: true,

        targetOrign: targetOrign,

      })

      Taro.createSelectorQuery().select('#orign').boundingClientRect((res) => {

        console.log(res)

        let orign = {

          x1: res.left,

          y1: res.top,

        }

        let d = { ...orign, ...targetOrign }

        let orignAngle = getAngle(d)

        this.setValByAngle(orignAngle)

        this.setState({

          orign: orign,

        })



      }).exec()

    }

  }



  touchMove=(e)=> {

    var Longtouch = this.state.Longtouch

    if (Longtouch) {

      if (timer) clearTimeout(timer)

      timer = setTimeout(() => {

        let lng = e.changedTouches[0]

        let targetOrign = {

          x2: lng.pageX,

          y2: lng.pageY

        }

        let orign = this.state.orign

        let d = { ...orign, ...targetOrign }

        let orignAngle = getAngle(d)

        this.setValByAngle(orignAngle)

      }, 5);

    }



  }

  touchEnd(e) {

    this.setState({

      Longtouch: false

    })

  }

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值