fuzzySearch.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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
  7. type="text"
  8. :placeholder="placeholder"
  9. v-model="text"
  10. @input="onInputHandle"
  11. @focus="inputFoucsHandle"
  12. @blur="inputBlurHandle"
  13. />
  14. <view v-show="showClosable" class="iconfont icon-guanbi" @click="clearInput"></view>
  15. </view>
  16. <view class="search-btn" @click="searchHandle"> 搜索 </view>
  17. </view>
  18. <view class="mask" v-show="showMask"></view>
  19. </view>
  20. </template>
  21. <script>
  22. export default {
  23. props: {
  24. placeholder: {
  25. type: String,
  26. default: '请输入搜索内容'
  27. },
  28. fixed: {
  29. type: Boolean,
  30. default: false
  31. }
  32. },
  33. data() {
  34. return {
  35. showMask: false,
  36. text:''
  37. }
  38. },
  39. computed:{
  40. showClosable(){
  41. return this.text.trim().length>0
  42. }
  43. },
  44. methods: {
  45. chickHandle(e) {
  46. this.$emit('fuzzyClick', e)
  47. },
  48. // 文本框获取焦点
  49. inputFoucsHandle() {
  50. this.showMask = true
  51. },
  52. // 文本框失去焦点
  53. inputBlurHandle() {
  54. this.showMask = false
  55. this.$emit('blur')
  56. },
  57. // 文本框输入事件
  58. onInputHandle() {
  59. this.$emit('input', this.text)
  60. },
  61. // 搜索按钮点击事件
  62. searchHandle(){
  63. this.$emit('search',this.text)
  64. },
  65. clearInput(){
  66. this.text = ''
  67. this.$emit('clear')
  68. }
  69. }
  70. }
  71. </script>
  72. <style lang="scss">
  73. // 自定义动画
  74. @keyframes fadeIn {
  75. from {
  76. opacity: 0;
  77. }
  78. to {
  79. opacity: 1;
  80. }
  81. }
  82. .mask {
  83. width: 100vh;
  84. height: 100vh;
  85. position: fixed;
  86. background-color: rgba(0, 0, 0, 0.4);
  87. top: 0;
  88. left: 0;
  89. z-index: 666;
  90. animation: fadeIn ease-in 0.2s;
  91. }
  92. .search {
  93. display: flex;
  94. justify-content: space-between;
  95. align-items: center;
  96. position: relative;
  97. top: 0;
  98. padding: 20rpx 24rpx;
  99. width: 100%;
  100. box-sizing: border-box;
  101. background-color: #fff;
  102. z-index: 999;
  103. .search-btn{
  104. width: 80rpx;
  105. font-size: 28rpx;
  106. margin-left: 10rpx;
  107. text-align: right;
  108. }
  109. .search-box {
  110. display: flex;
  111. flex: 1;
  112. justify-content: flex-start;
  113. align-items: center;
  114. height: 60rpx;
  115. padding-left: 10rpx;
  116. background-color: #f1f1f1;
  117. box-sizing: border-box;
  118. border-radius: 30rpx;
  119. position: relative;
  120. z-index: 999;
  121. .iconfont {
  122. width: 60rpx;
  123. height: 60rpx;
  124. margin-right: 8rpx;
  125. text-align: center;
  126. line-height: 60rpx;
  127. }
  128. .iconfont-guanbi{
  129. position: absolute;
  130. top: 0;
  131. right: 0;
  132. color: #eee;
  133. }
  134. input {
  135. width: 480rpx;
  136. font-size: 26rpx;
  137. }
  138. }
  139. .fuzzy {
  140. padding: 10rpx 0;
  141. padding-left: 76rpx;
  142. animation: fadeIn ease-in 0.2s;
  143. .fuzzy-item {
  144. font-size: 26rpx;
  145. color: #444;
  146. line-height: 60rpx;
  147. }
  148. }
  149. }
  150. </style>