phone-alphabet.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <view class="alphabet">
  3. <view class="alphabet-item"
  4. v-for="(item, key) of phones"
  5. :key="key"
  6. :data-key="key"
  7. :class="activeClass == key ? 'active' : ''"
  8. @touchstart="handleTouchStart"
  9. @touchmove = "handleTouchMove"
  10. @touchend="handleTouchEnd"
  11. @touchcancel="handleTouchCancel"
  12. >
  13. {{key}}
  14. </view>
  15. <view class="alphabet-alert" v-if="touchmove">
  16. {{activeClass}}
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. name:"phone-alphabet",
  23. props:{
  24. phones:Object,
  25. phoneListIndex:String
  26. },
  27. data() {
  28. return {
  29. touchStatus:false,
  30. timer:null,
  31. activeClass:'A',
  32. phonesArr:[],
  33. touchmove:false
  34. };
  35. },
  36. mounted () {
  37. let phonesArr = Object.keys(this.phones)
  38. this.phonesArr = phonesArr
  39. },
  40. watch:{
  41. phoneListIndex (index) {
  42. this.activeClass = index
  43. }
  44. },
  45. methods:{
  46. handleTouchStart (e) {
  47. this.$emit('reset',false)
  48. let key = e.target.dataset.key
  49. this.activeClass = key
  50. this.$emit('change',e.target.dataset.key)
  51. this.touchStatus = true
  52. },
  53. handleTouchMove (e) {
  54. this.$emit('scrollAnimationOFF',false)
  55. if(this.touchStatus){
  56. this.touchmove = true
  57. if(this.timer){
  58. clearTimeout(this.timer)
  59. }
  60. this.timer = setTimeout(()=>{
  61. const touchY = e.touches[0].clientY - 54
  62. const index = Math.floor(touchY / 20)
  63. if(index >= 0 && index < this.phonesArr.length){
  64. this.activeClass = this.phonesArr[index]
  65. this.$emit('change',this.phonesArr[index])
  66. }
  67. },16)
  68. }
  69. },
  70. handleTouchEnd (e) {
  71. this.$emit('scrollAnimationOFF',true)
  72. this.touchStatus = false
  73. this.touchmove = false
  74. }
  75. }
  76. }
  77. </script>
  78. <style>
  79. .alphabet>.active,.hover{
  80. color: #fff;
  81. background-color: #0190a0;
  82. border-radius: 40upx;
  83. }
  84. .alphabet{
  85. display: flex;
  86. flex-direction: column;
  87. text-align: center;
  88. z-index: 10;
  89. padding: 10upx 10upx 0 10upx;
  90. }
  91. .alphabet-item{
  92. width: 40upx;
  93. font-size:24upx;
  94. height: 40upx;
  95. line-height: 40upx;
  96. }
  97. .alphabet-alert{
  98. position: absolute;
  99. z-index: 20;
  100. width: 160upx;
  101. height: 160upx;
  102. left: 50%;
  103. top: 50%;
  104. margin-left: -80upx;
  105. margin-top: -80upx;
  106. border-radius: 80upx;
  107. text-align: center;
  108. line-height: 160upx;
  109. font-size: 70upx;
  110. color: #fff;
  111. background-color: rgba(0, 0, 0, 0.5);
  112. }
  113. </style>