原創:微信小程序之MaterialDesign--input組件

做者:jeffercss

來自:原文地址html

主要經過input輸入事件配合css的transform動態改變實現這種效果。ios

實際調試過程當中,input組件bindinput事件觸發後回調的detail對象,在模擬器中含有cursor屬性,在真機中(測過安卓,ios沒測過)卻沒有該屬性,最後選擇detail對象中的value屬性的值的長度來同步輸入的位數。git

bindfocus事件最好不要添加改變css的代碼 。 github


預覽圖片:app

 

 

JS:this

 

//index.js
//獲取應用實例
var app = getApp()
Page({
  data: {
    v_username_border:'', //用戶輸入框底部border樣式
    v_pwd_border:'',  // 密碼輸入框底部border樣式
    v_float_username:'', // 浮動文字字transform 樣式
    v_float_pwd:'',
    num_current_un:0,  // 當前輸入的文本位數
    sp_num_current_un:'', // 當前輸入文本位數超過限制時的樣式
    isPwdError:false  // 提交時 密碼輸入錯誤時的文本提示
  },
  onLoad: function () {
    console.log('onLoad')
  },
  // 用戶名輸入框獲取焦點時事件回調
  usernameFocus:function(e){
    var that = this;
    console.log(e.detail)
  },
  // 用戶名輸入框輸入時事件回調
  usernameInput:function(e){
    console.log(e.detail)
     this.setData({
      v_username_border:'border-bottom:1px solid red',
     num_current_un:e.detail.value.length
    })
    if(e.detail.value.length!=0){
       this.setData({
        v_float_username:'color:red ;transform: translateY(-18.5px)',
        sp_num_current_un:'color:lightseagreen;'
      })
      if(e.detail.value.length>20){
        this.setData({
          sp_num_current_un:'color:orangered;'
        })
      }
    }else{
      this.setData({
        v_float_username:'transform: translateY(0px)',
      })
    }
  },
  // // 用戶名輸入框失去焦點時回調
  usernameBlur:function(e){
    console.log("onBlur")
     this.setData({
      v_username_border:'border-bottom:1px solid grey'
    })
  },
  pwdFocus:function(e){
    console.log('onFocus')
  },
  pwdInput:function(e){
    this.setData({
      v_pwd_border:'border-bottom:1px solid red',
      isPwdError:false
    })
    console.log(e.detail)
    if(e.detail.value.length!=0){
      this.setData({
        v_float_pwd:'color:red ; transform: translateY(-18.5px)',
      })
    }else{
      this.setData({
        v_float_pwd:'transform: translateY(0px)',
      })
    }
  },
   pwdBlur:function(e){
    console.log("onBlur")
     this.setData({
      v_pwd_border:'border-bottom:1px solid grey; '
    })
  },
// 登陸按鈕模擬表單提交  可用form組件代替
  onLogin:function(e){
    this.setData({
      isPwdError:true
    })
  }
})

源碼地址https://github.com/jeffer0323/We-MaterialDesignspa

相關文章
相關標籤/搜索