搜索框轮播功能
效果
- 不断轮播数据,直到用户点击搜索或完成输入
- 用户搜索时若无输入数据,则以当前轮播数据作为输入值,若有输入数据,则以输入数据作为搜索值
思路
- 文字轮播同样采用轮播图的形式(通过绝对定位浮现在输入框的位置,且输入框的层级要比轮播图高,否则会出现输入数据,输入框不显示)
- 用一个变量保存轮播图当前数据,使用轮播图中@change函数,每次轮播的时候报存当前轮播数据,且注意变量初始值为轮播数据首位
- 监听输入框的值,若有值,轮播图隐藏,
- 搜索时判断当前输入框有无值,有值则以它为搜索条件,无值则以当前轮播值为搜索条件
代码实现
页面结构
<view class="input">
<input style="z-index: 1000;" confirm-type="search" maxlength="15" type="text" auto-blur
@confirm="searchWare()" v-model="inputValue" :placeholder="inputPlaceholder" placeholder-style="padding-left: 20rpx;color: '#666666'" />
<view class="close" v-if="inputValue" @click="clearInput()">
<view class="iconfont icon-guanbi"></view>
</view>
<view class="iconfont icon-sousuo1" @click="searchWare()"></view>
<swiper class="place_swiper" autoplay interval=2000 circular vertical @change="placeChange" v-show="!focus">
<swiper-item v-for="(item,index) in classifyList">
<view class="">{{item.type}}</view>
</swiper-item>
</swiper>
</view>
逻辑
export default {
data() {
return {
inputValue: '',
focus: false,
currentSwip: '零食',
}
},
watch: {
inputValue: {
handler(newValue,oldValue) {
console.log(newValue,'新的值');
if(newValue) {
this.inputPlaceholder = ''
this.focus = true;
} else {
this.inputPlaceholder = ''
this.focus = false;
}
}
}
},
methods: {
searchWare() {
uni.showLoading({
title: '加载中..',
mask: true
});
this.currentPage = 1;
this.list = [];
if (this.inputValue) {
} else {
this.inputValue = this.currentSwip
this.focus = true;
}
},
clearInput() {
this.inputValue = '';
this.inputPlaceholder = '';
this.focus = false
},
placeChange(event) {
this.currentSwip = this.classifyList[event.detail.current].type
},
}
}