微信小程序仿淘宝热搜词在搜索框中轮播功能

2022-10-09,,,,

摘要

淘宝的时候,发现淘宝搜索框中一直在垂直方向上轮播热搜提示词,觉得这是个不错的设计,除了能让空间更充分使用,也能让页面更有动感,最重要的是能够增加搜索框的使用频率。就在小程序中试着实现实现。

效果

体验

实现思路

思路比较简单,主要是两点,

1:input处于热搜提示词上层,用z-index实现
2:热搜词轮播用swiper实现,方向为vertical
3:在input聚焦时获取swiper当前值,设置为placeholder
4:将swiper隐藏

代码

已封装成组件

组件代码:

wxss

<view class="swiper-view">
 <swiper class="swiper_container" vertical="true" autoplay="true" circular="true" interval="2000">
  <block wx:for="{{msglist}}">
   <swiper-item>
    <view class="swiper_item">{{item.title}}</view>
   </swiper-item>
  </block>
 </swiper>
</view>

wxss

.container {
 width: 100%;
 height: 80rpx;
 display: flex;
 flex-direction: row;
 justify-content: center;
 align-items: center;
 background: #ededed;
}

.search-container {
 width: 690rpx;
 height: 60rpx;
 display: flex;
 flex-direction: row;
 justify-content: flex-start;
 align-items: center;
 background: #fff;
 border-radius: 5rpx;
}

.swiper_container {
 margin-left: 15rpx;
 height: 60rpx;
 width: 100%;
 display: flex;
 flex-direction: row;
 justify-content: flex-start;
 align-items: center;
 position:absolute;
 z-index:1;
}

.swiper_item {
 height: 60rpx;
 font-size: 26rpx;
 color: #999;
 overflow: hidden;
 text-overflow: ellipsis;
 white-space: nowrap;
 display: flex;
 flex-direction: row;
 justify-content: flex-start;
 align-items: center;
}

js

component({
 /**
  * 组件的属性列表
  */
 properties: {
  msglist:{
   type:json,
   value: []
  }
 },

 /**
  * 组件的初始数据
  */
 data: {
  placeholder:'',
  currentindex:0,
  index:0,
  isfocus:false,
  msglist: [],
  content:'',
  confirmcontent:''
 },

 ready(){
  this.setdata({
   msglist:this.properties.msglist
  })
 },
 /**
  * 组件的方法列表
  */
 methods: {
  changeindex(e){
   this.setdata({
    index:e.detail.current
   })
  },
  focusinput(){
   this.setdata({
    isfocus:true,
    placeholder:this.data.msglist[this.data.index].title
   })
  },
  blurinput(){
   if (this.data.content == ""){
    this.setdata({
     isfocus: false,
     currentindex: this.data.index,
     placeholder: ''
    })
   }
  },
  confirm(e){
   var confirmcontent = ''
   if(e.detail.value==''){
    confirmcontent = this.data.placeholder
   }else{
    confirmcontent = e.detail.value
   }

   this.triggerevent('search', {confirmcontent})
  },
  inputcontent(e){
   this.setdata({
    content: e.detail.value
   })
  }
 }
})

json

{
 "component": true,
 "usingcomponents": {}
}

页面代码

js

page({
 data: {
  msglist: [
   { title: "朋友圈" },
   { title: "文章" },
   { title: "公共号" },
   { title: "小程序" },
   { title: "音乐" },
   { title: "表情" },
   { title: "订阅号" }]
 },
 search(e){
  wx.showtoast({
   icon:"none",
   title: "正在搜索"+e.detail.confirmcontent,
  })
 }
})

wxss

<swipersearch msglist="{{msglist}}" bind:search="search"></swipersearch>

总结

以上所述是小编给大家介绍的微信小程序仿淘宝热搜词在搜索框中轮播功能,希望对大家有所帮助

《微信小程序仿淘宝热搜词在搜索框中轮播功能.doc》

下载本文的Word格式文档,以方便收藏与打印。