fuzzySearch.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <view>
  3. <view class="search" :style="{ position: fixed ? 'fixed' : 'relative' }">
  4. <view class="search-box">
  5. <text class="iconfont icon-sousuo"></text>
  6. <input type="text" :placeholder="placeholder" v-model="text" @input="onInputHandle" @focus="inputFoucsHandle" @blur="inputBlurHandle"/>
  7. </view>
  8. <view class="fuzzy" v-if="showMask">
  9. <view
  10. class="fuzzy-item"
  11. v-for="(item, index) in list"
  12. :key="index"
  13. @click="chickHandle(item)"
  14. >
  15. {{ item }}
  16. </view>
  17. </view>
  18. </view>
  19. <view class="mask" v-show="showMask"></view>
  20. </view>
  21. </template>
  22. <script>
  23. export default {
  24. props: {
  25. placeholder: {
  26. type: String,
  27. default: '请输入搜索内容'
  28. },
  29. list: {
  30. type: Array
  31. },
  32. fixed: {
  33. type: Boolean,
  34. default: false
  35. }
  36. },
  37. data() {
  38. return {
  39. showMask:false,
  40. text:''
  41. }
  42. },
  43. methods: {
  44. chickHandle(e) {
  45. this.$emit('fuzzyClick', e)
  46. },
  47. // 文本框获取焦点
  48. inputFoucsHandle(){
  49. this.showMask = true
  50. },
  51. // 文本框失去焦点
  52. inputBlurHandle(){
  53. this.showMask = false
  54. },
  55. // 文本框输入事件
  56. onInputHandle(){
  57. this.$emit('input',this.text)
  58. }
  59. }
  60. }
  61. </script>
  62. <style lang="scss">
  63. // 自定义动画
  64. @keyframes fadeIn {
  65. from{
  66. opacity: 0;
  67. }
  68. to{
  69. opacity: 1;
  70. }
  71. }
  72. .mask{
  73. width: 100vh;
  74. height: 100vh;
  75. position: fixed;
  76. background-color: rgba(0,0,0,.4);
  77. top: 0;
  78. left: 0;
  79. z-index: 666;
  80. animation: fadeIn ease-in .2s;
  81. }
  82. .search {
  83. position: relative;
  84. top: 0;
  85. padding: 20rpx 24rpx;
  86. width: 100%;
  87. box-sizing: border-box;
  88. background-color: #fff;
  89. z-index: 999;
  90. .search-box {
  91. display: flex;
  92. justify-content: flex-start;
  93. align-items: center;
  94. height: 60rpx;
  95. padding-left: 10rpx;
  96. background-color: #f1f1f1;
  97. box-sizing: border-box;
  98. border-radius: 30rpx;
  99. position: relative;
  100. z-index: 999;
  101. .iconfont {
  102. width: 60rpx;
  103. height: 60rpx;
  104. margin-right: 8rpx;
  105. text-align: center;
  106. line-height: 60rpx;
  107. }
  108. input {
  109. font-size: 26rpx;
  110. }
  111. }
  112. .fuzzy {
  113. padding: 10rpx 0;
  114. padding-left: 76rpx;
  115. animation: fadeIn ease-in .2s;
  116. .fuzzy-item {
  117. font-size: 26rpx;
  118. color: #444;
  119. line-height: 60rpx;
  120. }
  121. }
  122. }
  123. </style>