搜索框的位移问题
<template>
<div class="search-modal" v-if="showModal" ref="main_search" >
<div class="mask"></div>
<transition name="searchModalAni">
<div class="search-container" @click="blur">
<div class="arrow" @click="closeModal"><img src="~IMAGES/common/small_down_arrow.png" alt=""></div>
<div class="search-body" ref="search_body">
<div class="search-title">
<img src="~IMAGES/common/small_log.png" alt="" class="small-logo">
<span class="logo-title">秒可技能搜索</span>
</div>
<div class="search-input">
<input type="text" class="input-show" v-model="keyword" @input="hiddenSearch(true)" ref="centerInput" @focus="getHistory" id="centerInput">
<a href="javascript:;" class="search-btn">秒可一下</a>
<ul v-if="showHistoryList" class="history-list">
<li v-for="(item, index) of searchHistory" :key="index" class="history-list-item" @click="handleClickCenterItem(item, true)">
{{item}}
</li>
</ul>
</div>
</div>
</div>
</transition>
<div class="lf-search-container" ref="lfSearchDom">
<div class="lf-search-body">
<img src="~IMAGES/common/small_log.png" alt="" class="lf-small-logo">
<input type="text" class="lf-input" v-model="keyword" ref="lf_Input" @input="searchPractice" @keyup.enter="toSearch">
<a href="javascript:;" class="lf-btn" @click="toSearch">秒可一下</a>
<ul class="lf-history-list" v-show="lfShowHistoryList">
<li v-for="(item, index) of inputSearchList" :key="index" class="lf-history-list-item" @click.stop="handleClickCenterItem(item.name, true)">
{{item.name}}
</li>
</ul>
</div>
</div>
<div class="search-list-container" v-if="showResult">
<div class="course-list">
<div class="totalNum">{{totalNum > 0 ? '秒可为你找到' + totalNum + '条相关结果' : '秒可未找到相关结果'}}</div>
<div class="search-result" v-if="totalNum">
<div v-for="(item, index) in courseList" :key="index" class="search-list-item" @click="toSkillPage(item.id)">
<el-container>
<el-header class="course-title"><a href="javascript:;">{{item.name}}</a></el-header>
<el-container>
<el-aside class="course-img" v-show="item.cover_picture"><img :src="item.cover_picture" alt=""></el-aside>
<el-main class="course-introduction" v-show="item.introduction">
<div class="introduction-content">
{{item.introduction}}
</div>
</el-main>
</el-container>
</el-container>
</div>
</div>
<div class="empty" v-else>
<img src="~IMAGES/common/search_empty.png" alt="">
</div>
</div>
</div>
<div class="paging-block" v-show="showResult && totalNum">
<el-pagination
background="#8fe9de"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:page-size="5"
:current-page="currentPage"
:pager-count="7"
layout="prev, pager, next, jumper"
:total="totalNum">
</el-pagination>
</div>
</div>
</template>
<script>
import lodash from 'lodash'
export default {
name: 'SearchModal',
data () {
return {
keyword: '',
page: 1,
offset: 0,
practice_list: [],
searchHistory: [],
inputSearchList: [],
showHistoryList: false,
lfShowHistoryList: false,
lfSearchBody: false,
showSearchList: false,
showResult: this.showSearchResult,
currentPage: 1,
showPageBlock: false
}
},
props: {
showModal: Boolean,
showSearchResult: Boolean,
courseList: {
type: Array,
default: []
},
totalNum: {
type: Number,
default: 0
}
},
mounted () {
},
methods: {
closeModal () {
this.keyword = ''
this.$emit('close', this.showModal)
this.showHistoryList = false
this.$refs.lf_Input.value = ''
this.showModal = false
this.$refs.lfSearchDom.style.visibility = 'hidden'
this.showResult = false
this.showPageBlock = false
},
hiddenSearch (flag = false) {
this.$refs.search_body.style.display = 'none'
this.$refs.lfSearchDom.style.visibility = 'visible'
this.$refs.lf_Input.focus()
if (flag) {
this.searchPractice()
}
this.lfSearchBody = true
},
toSearch (searchString, flag = false) {
console.log(searchString, flag)
let search = flag ? searchString : this.keyword
console.log(search)
var judegeNotEmpty = false
if (search !== '') {
judegeNotEmpty = true
console.log(judegeNotEmpty)
this.judegeHiddenDropList(judegeNotEmpty)
} else {
judegeNotEmpty = false
this.judegeHiddenDropList(judegeNotEmpty)
}
this.setStorage(search)
this.$emit('getCourseList', search)
this.keyword = search
this.showResult = true
},
getHistory () {
const localSearchList = JSON.parse(window.localStorage.getItem('search_history')) || []
console.log(localSearchList)
this.searchHistory = localSearchList
console.log(this.searchHistory)
if (localSearchList.length !== 0) {
this.showHistoryList = true
this.$refs.centerInput.style.borderColor = '#1FAE9D'
this.$refs.centerInput.style.borderBottom = 'none'
this.$refs.centerInput.style.borderBottomLeftRadius = 0 + 'px'
}
},
setStorage (search) {
let localSearchList = JSON.parse(window.localStorage.getItem('search_history')) || []
console.log(localSearchList)
var reg = /^\s+$/g
if (!(reg.test(search)) && typeof search === 'string') {
localSearchList.unshift(search)
}
let uniqueArray = Array.from(new Set(localSearchList))
console.log(uniqueArray)
let aheadFour = uniqueArray.splice(0, 5)
window.localStorage.setItem('search_history', JSON.stringify(aheadFour))
},
judegeHiddenDropList (notEmpty) {
if (notEmpty) {
console.log(this.$refs.lf_Input)
this.$refs.lf_Input.style.borderBottom = '1px solid #1FAE9D'
this.$refs.lf_Input.style.borderBottomLeftRadius = 6 + 'px'
this.lfShowHistoryList = false
}
},
handleClickCenterItem (searchString, flag = false) {
this.toSearch(searchString, flag)
this.hiddenSearch(false)
},
blur () {
console.log('失去焦点')
this.judegeHiddenDropList(true)
},
handleSizeChange (val) {
console.log(`每页 ${val} 条`)
},
handleCurrentChange (val) {
console.log(`当前页: ${val}`)
console.log(this.keyword, val)
this.$emit('getCurrentData', this.keyword, val)
},
toSkillPage (id) {
console.log(id)
this.$emit('toSillPractice', id)
},
searchPractice: lodash.debounce(function () {
console.log(this.keyword)
let keyword = this.keyword
this.$api.getSearchPractice({search: keyword})
.then(res => {
console.log(res)
let allInputSearchList = res.practice_list
this.inputSearchList = allInputSearchList.splice(0, 5)
if (res.practice_list.length !== 0) {
this.lfShowHistoryList = true
this.$refs.lf_Input.style.borderBottom = 'none'
this.$refs.lf_Input.style.borderBottomLeftRadius = 0 + 'px'
}
if (res.practice_list.length === 0) {
this.lfShowHistoryList = false
this.$refs.lf_Input.style.borderBottom = '1px solid #1FAE9D'
this.$refs.lf_Input.style.borderBottomLeftRadius = 6 + 'px'
}
})
}, 100)
}
}
</script>
<style>
.el-pagination.is-background .el-pager li:not(.disabled).active {
background-color: #8fe9de;
color: #fff;
}
.el-input.is-active .el-input__inner, .el-input__inner:focus {
border-color: #8fe9de;
outline: none;
}
</style>
<style scoped>
.searchModalAni-enter-active, .searchModalAni-enter-active{
transition: all 1s ease;
}
.searchModalAni-leave-active, .search-enter {
height: 0px !important;
}
.searchModalAni-enter-active, .searchModalAni-leave {
height: 800px;
}
.search-modal {
position: fixed;
top: 0;
left: 0;
height: 80%;
width: 100%;
}
.arrow {
position: absolute;
right: 30px;
top: 20px;
font-size: 30px;
color: #cccccc;
cursor: pointer;
}
.mask {
position: fixed;
top: 0;
left: 0;
height: 80%;
width: 100%;
background-color: #000000;
opacity: 0.5;
}
.search-container {
position: fixed;
top: 100px;
left: 0;
right: 0;
bottom: 0;
width: 100%;
background: #ffffff;
border-top-left-radius: 50px;
border-top-right-radius: 50px;
}
.search-body {
position: absolute;
left: 50%;
top: 30%;
transform: translate(-50%);
height: 80px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.lf-search-container {
position: absolute;
top: 146px;
left: 50px;
visibility: hidden;
}
.lf-search-body {
display: flex;
height: 62px;
width: 686px;
align-items: center;
}
.lf-small-logo {
height: 29px;
width: 37px;
vertical-align: middle;
}
.lf-input {
margin-left: 16px;
width: 500px;
height: 48px;
background: #ffffff;
outline: none;
border: none;
padding-left: 15px;
border: 1px solid #1FAE9D;
border-top-left-radius: 6px;
border-bottom-left-radius: 6px;
border-right: none;
font-size: 18px;
color: #0E0E0E;
box-sizing: border-box;
}
.lf-btn {
width: 88px;
height: 48px;
line-height: 52px;
text-decoration: none;
text-align: center;
background: #1FAE9D;
color: #ffffff;
border-top-right-radius: 6px;
border-bottom-right-radius: 6px;
border-right: none;
border: 1px solid #1FAE9D;
border-left: none;
box-sizing: border-box;
}
.search-title {
font-size: 29px;
color:#080808;
}
.logo-title {
margin: 12px;
letter-spacing: 2px;
}
.small-logo {
vertical-align: middle;
}
.search-input {
margin-top: 40px;
display: flex;
position: relative;
}
.input-show {
height: 46px;
width: 440px;
outline: none;
border: none;
border: 1px solid #D7D7D7;
border-top-left-radius: 6px;
border-bottom-left-radius: 6px;
box-sizing: border-box;
}
.search-btn {
height: 46px;
width: 80px;
text-decoration: none;
color: #ffffff;
background:#1FAE9D;
text-align: center;
line-height: 46px;
box-sizing: border-box;
border: 1px solid #1FAE9D;
border-top-right-radius: 6px;
border-bottom-right-radius: 6px;
font-size: 16px;
}
.left-search {
position: absolute;
left: 0;
top: 800;
width: 300px;
height: 500px;
}
.history-list {
position: absolute;
top: 26px;
left: 0px;
width: 440px;
list-style: none;
border: 1px solid #1FAE9D;
border-top: none;
padding: 0;
box-sizing: border-box;
}
.history-list-item {
height: 38px;
width: 100%;
line-height: 38px;
color: #74777B;
font-size: 16px;
cursor: pointer;
padding-left: 10px;
box-sizing: border-box;
}
.history-list-item:hover, .lf-history-list-item:hover {
color: #1FAE9D;
}
.lf-history-list {
position: absolute;
top: 39px;
left: 53px;
width: 501px;
list-style: none;
padding: 0;
box-sizing: border-box;
border: 1px solid #1FAE9D;
border-top: none !important;
background: #ffffff;
z-index: 8;
}
.lf-history-list-item {
height: 38px;
width: 100%;
line-height: 38px;
color: #74777B;
font-size: 16px;
cursor: pointer;
padding-left: 10px;
box-sizing: border-box;
}
.search-list-container {
position: fixed;
top: 216px;
left: 100px;
height: 80%;
width: 590px;
}
.totalNum {
font-size: 12px;
color: #838383;
}
.course-title {
padding-left: 0;
padding-right: 0;
font-size: 18px;
height: 30px !important;
width: 100px !important;
}
.course-title a {
color: #73c9bf;
}
.el-header {
height: 20px;
}
.course-img {
width: 70px !important;
height: 70px !important;
margin-top: 4px;
margin-right: 12px;
}
.course-img img {
width: 70px;
height: 70px;
vertical-align: middle;
}
.course-introduction {
width: 533px;
height: 60px;
}
.el-aside {
overflow: hidden;
height: 70px;
width: 70px;
}
.el-main {
padding: 0;
margin-top: 10px;
overflow: hidden;
word-spacing: 2px;
text-align: justify;
font-size: 12px;
color: #0F0F0F;
}
.introduction-content {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
}
.search-list-item {
margin-top: 20px;
}
.paging-block {
position: absolute;
left: 90px;
height: 60px;
line-height: 60px;
bottom: -160px;
}
::v-deep .el-pagination.is-background .el-pager li:not(.disabled).active {
background-color:#009688 !important;
color: #fff;
}
.el-pager li.active {
color: #1FAE9D !important;
}
.el-pager li:hover {
color: #1FAE9D !important;
}
</style>