mehaotian-search.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <view class="search" :style="{ backgroundColor: backgroundColor }">
  3. <view class="content" :style="{ 'border-radius': radius + 'px', border: border }">
  4. <view class="content-box" :class="{ center: mode === 2 }">
  5. <text class="icon icon-search">&#xe61c;</text>
  6. <input class="input" :class="{ center: !active && mode === 2 }" @input="search" :focus="isFocus" :placeholder="placeholder" v-model="inputVal" @focus="focus" @blur="blur" />
  7. <!-- <view v-if="!active && mode === 2" class="input sub" @click="getFocus">请输入搜索内容</view> -->
  8. <!--<text v-if="isDelShow" class="icon icon-del" @click="clear">&#xe644;</text>-->
  9. </view>
  10. <view v-show="(active && show && button === 'inside') || (isDelShow && button === 'inside')" class="searchBtn" @click="search">搜索</view>
  11. </view>
  12. <!--<view v-if="button === 'outside'" class="button" :class="{ active: show || active }" @click="search">-->
  13. <!--<view class="button-item">{{ !show ? searchName : '搜索' }}</view>-->
  14. <!--</view>-->
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. props: {
  20. mode: {
  21. type: Number,
  22. default: 1
  23. },
  24. button: {
  25. type: String,
  26. default: 'outside'
  27. },
  28. show: {
  29. type: Boolean,
  30. default: true
  31. },
  32. radius: {
  33. type: String,
  34. default: '60'
  35. },
  36. placeholder: {
  37. type: String,
  38. default: '请输入搜索内容'
  39. },
  40. backgroundColor: {
  41. type: String,
  42. default: '#fff'
  43. },
  44. border: { type: String, default: '1px #f5f5f5 solid' }
  45. },
  46. data() {
  47. return {
  48. active: false,
  49. inputVal: '',
  50. searchName: '取消',
  51. isDelShow: false,
  52. isFocus: false
  53. };
  54. },
  55. methods: {
  56. focus() {
  57. this.active = true;
  58. },
  59. blur() {
  60. this.isFocus = false;
  61. if (!this.inputVal) {
  62. this.active = false;
  63. }
  64. },
  65. clear() {
  66. this.inputVal = '';
  67. this.active = false;
  68. this.$emit('search', '');
  69. },
  70. getFocus() {
  71. this.isFocus = true;
  72. },
  73. search() {
  74. this.$emit('search', this.inputVal);
  75. }
  76. },
  77. watch: {
  78. inputVal(newVal) {
  79. if (newVal) {
  80. this.searchName = '搜索';
  81. this.isDelShow = true;
  82. } else {
  83. this.searchName = '取消';
  84. this.isDelShow = false;
  85. }
  86. }
  87. }
  88. };
  89. </script>
  90. <style lang="scss" scoped>
  91. .search {
  92. display: flex;
  93. width: 100%;
  94. // border-bottom: 1px #f5f5f5 solid;
  95. box-sizing: border-box;
  96. padding: 15upx;
  97. font-size: $uni-font-size-base;
  98. background: #fff;
  99. .content {
  100. display: flex;
  101. align-items: center;
  102. width: 100%;
  103. height: 80upx;
  104. border: 1px #ccc solid;
  105. background: #fff;
  106. overflow: hidden;
  107. transition: all 0.2s linear;
  108. border-radius: 30px;
  109. background-color: #f0f0f0;
  110. .content-box {
  111. width: 100%;
  112. display: flex;
  113. align-items: center;
  114. &.center {
  115. justify-content: center;
  116. }
  117. .icon {
  118. padding: 0 15upx;
  119. &.icon-del {
  120. font-size: 38upx;
  121. }
  122. }
  123. .input {
  124. width: 100%;
  125. max-width: 100%;
  126. line-height: 60upx;
  127. height: 60upx;
  128. transition: all 0.2s linear;
  129. &.center {
  130. width: 200upx;
  131. }
  132. &.sub {
  133. // position: absolute;
  134. width: auto;
  135. color: grey;
  136. }
  137. }
  138. }
  139. .searchBtn {
  140. height: 100%;
  141. flex-shrink: 0;
  142. padding: 0 30upx;
  143. background: $uni-color-success;
  144. line-height: 60upx;
  145. color: #fff;
  146. border-left: 1px #ccc solid;
  147. transition: all 0.3s;
  148. }
  149. }
  150. .button {
  151. display: flex;
  152. align-items: center;
  153. justify-content: center;
  154. position: relative;
  155. flex-shrink: 0;
  156. width: 0;
  157. transition: all 0.2s linear;
  158. white-space: nowrap;
  159. overflow: hidden;
  160. &.active {
  161. padding-left: 15upx;
  162. width: 100upx;
  163. }
  164. }
  165. }
  166. @font-face {
  167. font-family: 'iconfont';
  168. src: url('https://at.alicdn.com/t/font_989023_efq0mtli526.ttf') format('truetype');
  169. }
  170. .icon {
  171. font-family: iconfont;
  172. font-size: 32upx;
  173. font-style: normal;
  174. color: #999;
  175. }
  176. </style>