微信小程序仿淘寶熱搜詞在搜索框中輪播功能
摘要
逛淘寶的時(shí)候,發(fā)現(xiàn)淘寶搜索框中一直在垂直方向上輪播熱搜提示詞,覺(jué)得這是個(gè)不錯(cuò)的設(shè)計(jì),除了能讓空間更充分使用,也能讓頁(yè)面更有動(dòng)感,最重要的是能夠增加搜索框的使用頻率。就在小程序中試著實(shí)現(xiàn)實(shí)現(xiàn)。
效果

體驗(yàn)

實(shí)現(xiàn)思路
思路比較簡(jiǎn)單,主要是兩點(diǎn),
1:input處于熱搜提示詞上層,用z-index實(shí)現(xiàn)
2:熱搜詞輪播用swiper實(shí)現(xiàn),方向?yàn)関ertical
3:在input聚焦時(shí)獲取swiper當(dāng)前值,設(shè)置為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: []
}
},
/**
* 組件的初始數(shù)據(jù)
*/
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": {}
}
頁(yè)面代碼
js
Page({
data: {
msgList: [
{ title: "朋友圈" },
{ title: "文章" },
{ title: "公共號(hào)" },
{ title: "小程序" },
{ title: "音樂(lè)" },
{ title: "表情" },
{ title: "訂閱號(hào)" }]
},
search(e){
wx.showToast({
icon:"none",
title: "正在搜索"+e.detail.confirmContent,
})
}
})
wxss
<swiperSearch msgList="{{msgList}}" bind:search="search"></swiperSearch>
總結(jié)
以上所述是小編給大家介紹的微信小程序仿淘寶熱搜詞在搜索框中輪播功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
js實(shí)現(xiàn)Select列表各項(xiàng)上移和下移的方法
這篇文章主要介紹了js實(shí)現(xiàn)Select列表各項(xiàng)上移和下移的方法,涉及javascript動(dòng)態(tài)操作頁(yè)面元素屬性值的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
ES6中的Promise對(duì)象與async和await方法詳解
Promise是es6引入的異步編程薪解決方案,語(yǔ)法上promise就是一個(gè)構(gòu)造函數(shù),用來(lái)封裝異步操作病可以獲取其成功或失敗的結(jié)果,這篇文章主要介紹了ES6中的Promise對(duì)象與async和await方法,需要的朋友可以參考下2022-12-12
Json字符串轉(zhuǎn)換為JS對(duì)象的高效方法實(shí)例
一般JSON字符串轉(zhuǎn)換為JS對(duì)象,都使用var jsonStr="{a:1}";var jsonObj = eval("("+jsonStr+")");2013-05-05
Javascript保存網(wǎng)頁(yè)為圖片借助于html2canvas庫(kù)實(shí)現(xiàn)
借助于html2canvas庫(kù),把網(wǎng)頁(yè)保存為Canvas畫(huà)布,再把生成的canvas保存成圖片,下面的示例,大家可以看看2014-09-09
微信小程序textarea層級(jí)過(guò)高(蓋住其他元素)問(wèn)題的解決辦法
這篇文章主要給大家介紹了關(guān)于微信小程序textarea層級(jí)過(guò)高(蓋住其他元素)問(wèn)題的解決辦法,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

