fuzzySearch.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. },
  56. // 文本框输入事件
  57. onInputHandle() {
  58. this.$emit('input', this.text)
  59. },
  60. // 搜索按钮点击事件
  61. searchHandle(){
  62. this.$emit('search',this.text)
  63. },
  64. clearInput(){
  65. this.text = ''
  66. this.$emit('clear')
  67. }
  68. }
  69. }
  70. </script>
  71. <style lang="scss">
  72. // 自定义动画
  73. @keyframes fadeIn {
  74. from {
  75. opacity: 0;
  76. }
  77. to {
  78. opacity: 1;
  79. }
  80. }
  81. .mask {
  82. width: 100vh;
  83. height: 100vh;
  84. position: fixed;
  85. background-color: rgba(0, 0, 0, 0.4);
  86. top: 0;
  87. left: 0;
  88. z-index: 666;
  89. animation: fadeIn ease-in 0.2s;
  90. }
  91. .search {
  92. display: flex;
  93. justify-content: space-between;
  94. align-items: center;
  95. position: relative;
  96. top: 0;
  97. padding: 20rpx 24rpx;
  98. width: 100%;
  99. box-sizing: border-box;
  100. background-color: #fff;
  101. z-index: 999;
  102. .search-btn{
  103. width: 80rpx;
  104. font-size: 28rpx;
  105. margin-left: 10rpx;
  106. text-align: right;
  107. }
  108. .search-box {
  109. display: flex;
  110. flex: 1;
  111. justify-content: flex-start;
  112. align-items: center;
  113. height: 60rpx;
  114. padding-left: 10rpx;
  115. background-color: #f1f1f1;
  116. box-sizing: border-box;
  117. border-radius: 30rpx;
  118. position: relative;
  119. z-index: 999;
  120. .iconfont {
  121. width: 60rpx;
  122. height: 60rpx;
  123. margin-right: 8rpx;
  124. text-align: center;
  125. line-height: 60rpx;
  126. }
  127. .iconfont-guanbi{
  128. position: absolute;
  129. top: 0;
  130. right: 0;
  131. color: #eee;
  132. }
  133. input {
  134. width: 90%;
  135. font-size: 26rpx;
  136. }
  137. }
  138. .fuzzy {
  139. padding: 10rpx 0;
  140. padding-left: 76rpx;
  141. animation: fadeIn ease-in 0.2s;
  142. .fuzzy-item {
  143. font-size: 26rpx;
  144. color: #444;
  145. line-height: 60rpx;
  146. }
  147. }
  148. }
  149. </style>