程序顶部搜索框的实现方式与技术要点

why 801 2024-07-19

这篇文章主要为大家详细介绍了小程序实现搜索框功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

小程序顶部搜索框怎么实现

小程序顶部搜索框怎么实现

实现效果如下:

2019712163542622.gif

具体代码

1、WXML文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

<!--搜索框 -->

<view class="weui-search-bar">

 <view class="weui-search-bar__form">

 <!--点击之后,出现input框 -->

 <view class="weui-search-bar__box">

  <icon class="weui-icon-search_in-box" type="search" size="14"></icon>

  <input type="text" class="weui-search-bar__input" placeholder="搜索" value="{{inputVal}}" focus="{{inputShowed}}" bindinput="inputTyping" />

  <!--输入款字数大于0,则显示清除按钮 -->

  <view class="weui-icon-clear" wx:if="{{inputVal.length > 0}}" bindtap="clearInput">

  <icon type="clear" size="14"></icon>

  </view>

 </view>

 <!--没点击之前,只是一些文字和图标 -->

 <label class="weui-search-bar__label" hidden="{{inputShowed}}" bindtap="showInput">

  <icon class="weui-icon-search" type="search" size="14"></icon>

  <view class="weui-search-bar__text">搜索</view>

 </label>

 </view>

 <!--动态出现的“取消”键 -->

 <view class="weui-search-bar__cancel-btn" hidden="{{!inputShowed}}" bindtap="hideInput">取消</view>

</view>

2、WXSS文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

.weui-search-bar {

 position: relative;

 padding: 8px 10px;

 display: -webkit-box;

 display: -webkit-flex;

 display: flex;

 box-sizing: border-box;

 background-color: #EFEFF4;

 border-top: 1rpx solid #D7D6DC;

 border-bottom: 1rpx solid #D7D6DC;

   

}

.weui-icon-search {

 margin-right: 8px;

 font-size: inherit;

}

.weui-icon-search_in-box {

 position: absolute;

 left: 10px;

 top: 7px;

}

.weui-search-bar__text {

 display: inline-block;

 font-size: 14px;

 vertical-align: middle;

}

.weui-search-bar__form {

 position: relative;

 -webkit-box-flex: 1;

 -webkit-flex: auto;

   flex: auto;

 border-radius: 5px;

 background: #FFFFFF;

 border: 1rpx solid #E6E6EA;

}

.weui-search-bar__box {

 position: relative;

 padding-left: 30px;

 padding-right: 30px;

 width: 100%;

 box-sizing: border-box;

 z-index: 1;

}

.weui-search-bar__input {

 height: 28px;

 line-height: 28px;

 font-size: 14px;

}

.weui-icon-clear {

 position: absolute;

 top: 0;

 right: 0;

 padding: 7px 8px;

 font-size: 0;

}

.weui-search-bar__label {

 position: absolute;

 top: 0;

 right: 0;

 bottom: 0;

 left: 0;

 z-index: 2;

 border-radius: 3px;

 text-align: center;

 color: #9B9B9B;

 background: #FFFFFF;

 line-height: 28px;

}

.weui-search-bar__cancel-btn {

 margin-left: 10px;

 line-height: 28px;

 color: #09BB07;

 white-space: nowrap;

 font-size: 30rpx;

}

3、js文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

showInput: function () {

this.setData({

 inputShowed: true

});

},

hideInput: function () {

this.setData({

 inputVal: "",

 inputShowed: false

});

// getList(this);

},

clearInput: function () {

this.setData({

 inputVal: ""

});

// getList(this);

},

inputTyping: function (e) {

//搜索数据

// getList(this, e.detail.value);

this.setData({

 inputVal: e.detail.value

});

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序发布之后能否进行修改的全面解读
下一篇:小程序能否在不添加开发者的情况下运行
相关文章

 发表评论

暂时没有评论,来抢沙发吧~