大概步骤:
目标效果
十三、优化
- 要点:
- 搜索历史和热门搜索添加滚动
- 适配小播放组件
- 搜索输入框节流设置
- 滚动搜索结果列表,收起手机端输入键盘
(一)、搜索历史和热门搜索添加滚动
在search.vue中为2个组件外包一个div再添加滚动组件
<scroll ref="scroll" :data="shortcut">
<div class="search-scroll">
<!--热门搜索关键词组件-->
<search-hotkey></search-hotkey>
<!--搜索历史列表组件-->
<search-history></search-history>
</div>
</scroll>
滚动组件传入数据,来实时刷新dom滚动
但是这里的数据,包括了热门搜索和本地缓存了,热门搜索是异步的,因此数据,在计算属性中得
computed:{
shortcut(){
//因为要监听热门关键词数据和搜索结果数据,而且是异步的
return this.hotKey.concat(this.searchHistory)
},
(二)、适配小播放组件
使用mixin方法
import {playlistMixin} from '@/common/js/mixin' //引入mixin重复方法
mixins:[playlistMixin],
/******mixin方法引入************/
handlePlaylist(playlist){
const bottom = playlist.length>0 ? '60px':''
this.$refs.scroll.$el.style.bottom = bottom
this.$refs.scroll.refresh()
},
(三)、搜索输入框节流设置
见往期文章
(四)、滚动搜索结果列表,收起手机端输入键盘
在输入框组件中,使用input的dom的原生方法blur即可,这里注意事件派发,
在滚动前收起
/*********设置方法给父组件调用***********************/
blur(){
this.$refs.queryinput.blur()
}
在滚动前收起,使用scroll组件的滚动前事件
scroll.vue
props:{
//开启滚动前事件,用于收起键盘
beforScoll:{
type:Boolean,
default:false
},
}
//开启滚动前事件收起键盘
if(this.beforScoll){
this.scroll.on('beforScollStart',()=>{
this.$emit('beforScoll')
})
}
项目来源:慕课网
如有建议和疑问可联系
QQ:1017386624
邮箱:1017386624@qq.com