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

所属分类: 网络编程 / JavaScript 阅读数: 904
收藏 0 赞 0 分享

摘要

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

效果

在这里插入图片描述

体验

在这里插入图片描述

实现思路

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

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>

以上所述是小编给大家介绍的微信小程序仿淘宝热搜词在搜索框中轮播功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

更多精彩内容其他人还在看

jquery实现可拖拽弹出层特效

这篇文章主要介绍了jquery实现可拖拽弹出层特效,代码非常精简,效果非常棒,这里推荐给有需要的小伙伴
收藏 0 赞 0 分享

浅谈 javascript 事件处理

本文向大家简单介绍了javascript的事件处理机制,从事件源,事件操作到事件处理程序都做了简单介绍,并给出了部分示例,这里推荐给大家。
收藏 0 赞 0 分享

jQuery中:reset选择器用法实例

这篇文章主要介绍了jQuery中:reset选择器用法,实例分析了:reset选择器的功能、定义及匹配重置按钮的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

原生javascript实现隔行换色

这篇文章主要介绍了原生javascript实现隔行换色,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery中:button选择器用法实例

这篇文章主要介绍了jQuery中:button选择器用法,实例分析了:button选择器的功能、定义及选取按钮的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery中:file选择器用法实例

这篇文章主要介绍了jQuery中:file选择器用法,实例分析了:file选择器的功能、定义及选取类型为file的<input>元素的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery中:enabled选择器用法实例

这篇文章主要介绍了jQuery中:enabled选择器用法,实例分析了:enabled选择器的功能、定义及选取所有可用元素的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

AngularJS中取消对HTML片段转义的方法例子

这篇文章主要介绍了AngularJS中取消对HTML片段转义的方法例子,在一些需要显示HTML的地方,就要取消AngularJS的转义,本文就介绍了这种方法,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery中:disabled选择器用法实例

这篇文章主要介绍了jQuery中:disabled选择器用法,实例分析了:disabled选择器功能、定义及选取所有禁用的表单元素的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

jQuery中:checked选择器用法实例

这篇文章主要介绍了jQuery中:checked选择器用法,实例分析了:checked选择器的功能、定义及选取选中的复选框或单选按钮时的使用技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多