index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. Component({
  2. externalClasses: ['i-class'],
  3. properties : {
  4. count : {
  5. type : Number,
  6. value : 5
  7. },
  8. value : {
  9. type : Number,
  10. value : 0
  11. },
  12. disabled : {
  13. type : Boolean,
  14. value : false
  15. },
  16. size : {
  17. type : Number,
  18. value : 20
  19. },
  20. name : {
  21. type : String,
  22. value : ''
  23. }
  24. },
  25. data : {
  26. touchesStart : {
  27. pageX : 0
  28. }
  29. },
  30. methods : {
  31. handleClick(e){
  32. const data = this.data;
  33. if( data.disabled ){
  34. return;
  35. }
  36. const index = e.currentTarget.dataset.index;
  37. this.triggerEvent('change',{
  38. index : index + 1
  39. })
  40. },
  41. handleTouchMove(e){
  42. const data = this.data;
  43. if( data.disabled ){
  44. return;
  45. }
  46. if( !e.changedTouches[0] ){
  47. return;
  48. }
  49. const movePageX = e.changedTouches[0].pageX;
  50. const space = movePageX - data.touchesStart.pageX;
  51. if( space <= 0 ){
  52. return;
  53. }
  54. let setIndex = Math.ceil( space/data.size );
  55. setIndex = setIndex > data.count ? data.count : setIndex ;
  56. this.triggerEvent('change',{
  57. index : setIndex
  58. })
  59. }
  60. },
  61. ready(){
  62. const className = '.i-rate';
  63. var query = wx.createSelectorQuery().in(this)
  64. query.select( className ).boundingClientRect((res)=>{
  65. this.data.touchesStart.pageX = res.left || 0;
  66. }).exec()
  67. }
  68. });