当前位置:Gxlcms > JavaScript > 小程序实现悬浮搜索框

小程序实现悬浮搜索框

时间:2021-07-01 10:21:17 帮助过:1人阅读

本文实例为大家分享了小程序实现悬浮搜索框的具体代码,供大家参考,具体内容如下

悬浮搜索框是当数据界面不断滚动时,搜索框始终悬浮在最上方。来看一下效果图

UI代码

  1. <view class="search-wrapper">
  2. <view class="search-panel">
  3. <view class="search-section">
  4. <view class="search-button-wrapper">
  5. <image class="search-button" src="/images/scan.png" bindtap="scan"></image>
  6. </view>
  7. <view class="search-input-wrapper ">
  8. <input bindinput="bindBarcodeInput" bindconfirm="query" bindfocus="bindBarcodeFocus" bindblur="bindBarcodeBlur" class="search-input" placeholder="扫描或者手动输入条码" value="{{barcode}}" confirm-type="search" />
  9. </view>
  10. <view class="search-button-wrapper">
  11. <image class="search-button" src="/images/search.png" bindtap="query"></image>
  12. </view>
  13. </view>
  14. </view>
  15. <view class="search-demo" hidden="{{hiddenDropdown}}">
  16. <button size="mini" bindtap="setDemoData">示例</button>
  17. <button size="mini" bindtap="clear" style="margin-left:10px;">清空</button>
  18. </view>
  19. </view>

样式

  1. .search-wrapper {
  2. position: fixed;/*悬停搜索框的关键样式*/
  3. top: 0px;
  4. left: 0;
  5. width: 100%;
  6. z-index: 999;
  7. }
  8. .search-panel {
  9. background-color: #f50;
  10. }
  11. .search-section {
  12. padding: 5px 0px;
  13. display: flex;
  14. flex-direction: row;
  15. }
  16. .search-demo {
  17. padding: 5px;
  18. flex-direction: row;
  19. background-color: #eee;
  20. padding-left:42px;
  21. align-items: flex-start;
  22. }
  23. .search-input-wrapper {
  24. flex: 8;
  25. padding: 5px;
  26. background-color: #eee;
  27. border-radius: 3px;
  28. }
  29. .search-input {
  30. padding-top: 5px;
  31. }
  32. .search-clear {
  33. float: right;
  34. width: 32px;
  35. height: 32px;
  36. z-index: 998;
  37. }
  38. .search-button-wrapper {
  39. padding-left: 5px;
  40. padding-right: 5px;
  41. padding-top:5px;
  42. }
  43. .search-button {
  44. flex: 1;
  45. border: none !important;
  46. color: white !important;
  47. width: 32px;
  48. height: 32px;
  49. }

JS代码

  1. //获取应用实例
  2. var app = getApp()
  3. Page({
  4. data: {
  5. barcode: "",
  6. hiddenLoading: true,
  7. hiddenData: true,
  8. hiddenDropdown: true,
  9. hiddenClear:true,
  10. demoData: 'XXXX',
  11. Product: {},
  12. },
  13. bindBarcodeInput: function (e) {
  14. this.setData({
  15. barcode: e.detail.value
  16. })
  17. },
  18. bindBarcodeFocus: function (e) {
  19. this.setData({
  20. hiddenDropdown: false,
  21. hiddenClear:false
  22. })
  23. },
  24. bindBarcodeBlur: function (e) {
  25. this.setData({
  26. hiddenDropdown: true,
  27. hiddenClear:true
  28. })
  29. },
  30. scan: function (e) {
  31. var that = this;
  32. wx.scanCode({
  33. success: function (res) {
  34. that.setData({
  35. barcode: res.result
  36. });
  37. that.query(e);
  38. },
  39. fail: function () {
  40. that.setData({
  41. barcode: "",
  42. hiddenData: true
  43. });
  44. },
  45. complete: function () {
  46. // complete
  47. }
  48. })
  49. },
  50. setDemoData: function (e) {
  51. this.setData({
  52. barcode: this.data.demoData
  53. });
  54. },
  55. clear: function (e) {
  56. this.setData({
  57. barcode: "",
  58. hiddenData: true
  59. });
  60. },
  61. query: function (e) {
  62. var url = "https://www.xxx.com/query";//查询数据的URL
  63. var that = this;
  64. if (that.data.barcode == undefined
  65. || that.data.barcode == null
  66. || that.data.barcode.length <= 0) {
  67. that.setData({ hiddenData: true });
  68. wx.showToast({
  69. title: '请输入条码',
  70. image: '/images/fail.png',
  71. duration: 2000
  72. });
  73. return;
  74. }
  75. wx.request({
  76. url: url,
  77. data: { barcode: that.data.barcode },
  78. method: 'GET',
  79. success: function (res) {
  80. var result = res.data;
  81. if (result.Status != 0) {
  82. that.setData({ hiddenData: true });
  83. wx.showToast({
  84. title: result.Message,
  85. image: '/images/fail.png',
  86. duration: 2000
  87. })
  88. return;
  89. }
  90. that.setData({ Product: result.Data, hiddenData: false });
  91. wx.showToast({
  92. title: "获取数据成功",
  93. image: '/images/ok.png',
  94. duration: 2000
  95. })
  96. },
  97. fail: function (e) {
  98. var toastText = '获取数据失败' + JSON.stringify(e);
  99. that.setData({
  100. hiddenLoading: !that.data.hiddenLoading,
  101. hiddenData: true
  102. });
  103. wx.showToast({
  104. title: toastText,
  105. icon: '',
  106. duration: 2000
  107. })
  108. },
  109. complete: function () {
  110. // complete
  111. }
  112. })
  113. }
  114. })

用到的几个图片

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

人气教程排行