修改动画 完善搜索

This commit is contained in:
alger
2021-09-28 17:31:08 +08:00
parent 33be8880ff
commit 3c1318fe20
+77 -56
View File
@@ -1,32 +1,33 @@
<template> <template>
<div class="search-page"> <div class="search-page">
<div <div class="hot-search" :class="setAnimationClass('animate__fadeInDown')">
class="hot-search" <template v-for="(item, index) in hotSearchData?.data">
style="height:750px" <div
:class="setAnimationClass('animate__fadeInDown')" :class="setAnimationClass('animate__bounceInLeft')"
> :style="setAnimationDelay(index, 100)"
<n-layout style="height:750px" :native-scrollbar="false"> class="hot-search-item"
<template v-for="(item,index) in hotSearchData?.data"> @click.stop="clickHotKeyword(item.searchWord)"
<div class="hot-search-item" @click.stop="clickHotKeyword(item.searchWord)"> >
<span <span
class="hot-search-item-count" class="hot-search-item-count"
:class="{ 'hot-search-item-count-3': index < 3 }" :class="{ 'hot-search-item-count-3': index < 3 }"
>{{ index + 1 }}</span> >{{ index + 1 }}</span>
{{ item.searchWord }} {{ item.searchWord }}
</div> </div>
</template> </template>
</n-layout>
</div> </div>
<div> <!-- 搜索到的歌曲列表 -->
<!-- 搜索到的歌曲列表 --> <div class="search-list">
<div class="search-song-list"> <template v-if="searchDetail">
<template v-for="(item,index) in searchDetail?.result.song.songs"> <div
<div class="search-song-item"> v-for="(item, index) in searchDetail?.result.song.songs"
<img :src="item.al.picUrl + '?param=100y100'" alt /> :key="item.id"
<div>{{ item.name }}</div> :class="setAnimationClass('animate__bounceInRight')"
</div> :style="setAnimationDelay(index, 100)"
</template> >
</div> <SongItem :item="item" />
</div>
</template>
</div> </div>
</div> </div>
</template> </template>
@@ -34,63 +35,79 @@
<script lang="ts" setup> <script lang="ts" setup>
import { getSearch } from "@/api/search"; import { getSearch } from "@/api/search";
import type { IHotSearch } from "@/type/search"; import type { IHotSearch } from "@/type/search";
import { getHotSearch } from "@/api/home";
import { useRoute, useRouter } from "vue-router";
import { setAnimationClass, setAnimationDelay } from "@/utils";
import type { ISearchDetail } from "@/type/search";
import { onMounted, ref, watch } from "vue";
import SongItem from "@/components/common/SongItem.vue";
import { getHotSearch } from '@/api/home';
import { onBeforeRouteUpdate, useRoute, useRouter } from "vue-router";
import { setAnimationClass } from "@/utils";
import type { ISearchDetail } from "@/type/search"
import { onBeforeMount, onMounted, ref } from "vue";
const route = useRoute(); const route = useRoute();
const router = useRouter() const router = useRouter();
const searchDetail = ref<ISearchDetail>() const searchDetail = ref<ISearchDetail>();
const loadSearch = async (keyword: any) => {
if (!keyword) return;
const { data } = await getSearch(keyword);
searchDetail.value = data;
}
// 热搜列表 // 热搜列表
const hotSearchData = ref<IHotSearch>() const hotSearchData = ref<IHotSearch>();
const loadHotSearch = async () => { const loadHotSearch = async () => {
const { data } = await getHotSearch(); const { data } = await getHotSearch();
hotSearchData.value = data; hotSearchData.value = data;
} };
onBeforeMount(() => {
loadSearch(route.params.keyword) onMounted(() => {
loadHotSearch(); loadHotSearch();
}) });
const clickHotKeyword = (keyword: string) => { const clickHotKeyword = (keyword: string) => {
router.push({ router.push({
path: "/search", path: "/search",
query: { query: {
keyword: keyword keyword: keyword,
} },
}) });
// isHotSearchList.value = false; // isHotSearchList.value = false;
} };
// 监听路由参数变化
onBeforeRouteUpdate(to => {
let value = to.query.keyword?.toString()
loadSearch(value);
})
const loadSearch = async (keyword: any) => {
searchDetail.value = undefined;
if (!keyword) return;
const { data } = await getSearch(keyword);
const songs = data.result.song.songs;
// songs map 替换属性
songs.map((item: any) => {
item.picUrl = item.al.picUrl;
item.song = item;
item.artists = item.ar;
});
data.result.song.songs = songs;
searchDetail.value = data;
};
loadSearch(route.query.keyword);
watch(
() => route.query,
async newParams => {
console.log(newParams);
loadSearch(newParams.keyword);
}
)
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.search {
height: 750px;
background-color: #333;
}
.search-page { .search-page {
@apply flex; @apply flex;
} }
.hot-search { .hot-search {
@apply mt-3 mr-4 border rounded-xl border-2 flex-1 overflow-hidden; @apply mt-3 mr-4 rounded-xl flex-1 overflow-hidden;
background: #1a1a1a;
border-color: #63e2b7;
animation-duration: 0.2s; animation-duration: 0.2s;
min-width: 400px; min-width: 400px;
&-item { &-item {
@@ -103,4 +120,8 @@ onBeforeRouteUpdate(to => {
} }
} }
} }
.search-list {
@apply mt-3 flex-1;
}
</style> </style>