Search

搜索栏

search这部分是利用小程序input组件实现,在下面的示例中主要使用了focusbindinput两个属性,由于mpvue从底层支持 Vue.js 语法和构建工具体系,因此可以用vuev-model进行双向数据绑定,示例如下:

  1. <template>
  2. <div class="page">
  3. <div class="page__hd">
  4. <div class="page__title">SearchBar</div>
  5. <div class="page__desc">搜索栏</div>
  6. </div>
  7. <div class="page__bd">
  8. <div class="weui-search-bar">
  9. <div class="weui-search-bar__form">
  10. <div class="weui-search-bar__box">
  11. <icon class="weui-icon-search_in-box" type="search" size="14"></icon>
  12. <input type="text" class="weui-search-bar__input" placeholder="搜索" v-model="inputVal" :focus="inputShowed" @input="inputTyping" />
  13. <div class="weui-icon-clear" v-if="inputVal.length > 0" @click="clearInput">
  14. <icon type="clear" size="14"></icon>
  15. </div>
  16. </div>
  17. <label class="weui-search-bar__label" :hidden="inputShowed" @click="showInput">
  18. <icon class="weui-icon-search" type="search" size="14"></icon>
  19. <div class="weui-search-bar__text">搜索</div>
  20. </label>
  21. </div>
  22. <div class="weui-search-bar__cancel-btn" :hidden="!inputShowed" @click="hideInput">取消</div>
  23. </div>
  24. <div class="weui-cells searchbar-result" v-if="inputVal.length > 0">
  25. <navigator url="" class="weui-cell" hover-class="weui-cell_active">
  26. <div class="weui-cell__bd">
  27. <div>实时搜索文本</div>
  28. </div>
  29. </navigator>
  30. <navigator url="" class="weui-cell" hover-class="weui-cell_active">
  31. <div class="weui-cell__bd">
  32. <div>实时搜索文本</div>
  33. </div>
  34. </navigator>
  35. <navigator url="" class="weui-cell" hover-class="weui-cell_active">
  36. <div class="weui-cell__bd">
  37. <div>实时搜索文本</div>
  38. </div>
  39. </navigator>
  40. <navigator url="" class="weui-cell" hover-class="weui-cell_active">
  41. <div class="weui-cell__bd">
  42. <div>实时搜索文本</div>
  43. </div>
  44. </navigator>
  45. </div>
  46. </div>
  47. </div>
  48. </template>
  49. <script>
  50. export default {
  51. data() {
  52. return {
  53. inputShowed: false,
  54. inputVal: ""
  55. }
  56. },
  57. methods: {
  58. showInput() {
  59. this.inputShowed = true;
  60. },
  61. hideInput() {
  62. this.inputVal = '';
  63. this.inputShowed = false
  64. },
  65. clearInput() {
  66. this.inputVal = '';
  67. },
  68. inputTyping(e) {
  69. console.log(e);
  70. this.inputVal = e.mp.detail.value
  71. }
  72. }
  73. }
  74. </script>
  75. <style scoped>
  76. .searchbar-result {
  77. margin-top: 0;
  78. font-size: 14px;
  79. }
  80. .searchbar-result:before {
  81. display: none;
  82. }
  83. .weui-cell {
  84. padding: 12px 15px 12px 35px;
  85. }
  86. </style>

效果

search01