mehaotian-search.vue 4.1 KB

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