在推薦頁會展現輪播圖,以及熱門歌單列表,這些數據都來自QQ音樂的真實數據。在項目過程當中發現一些接口失效,能夠從QQ音樂API koa2版本獲取最新的API。css
在本地進行請求:vue
在vue.config.js中添加:ios
devServer: { hot: true, proxy: { '/api': { target: 'http://localhost:3200', changeOrigin: true, ws: true, pathRewrite: { '^/api': '', } } } },
因爲提供的接口不是jsop接口,因此不須要轉成jsop接口。能夠直接使用 axios 來完成 Ajax 請求.git
在recommend.js中添加:json
import axios from 'axios'; // import jsonp from '../common/js/jsonp'; export function getRecommend() { // const url = '/api/getDigitalAlbumLists'; // return jsonp(url); return axios.get('/api/getDigitalAlbumLists'); }在recommend.vue中添加:axios
import Slider from '../../base/slider/slider'; import { getRecommend } from '../../api/recommend'; import { ERR_OK } from '../../api/config'; export default { components: { Slider, }, data() { return { recommends: [], }; }, created() { this._getRecommend(); }, methods: { _getRecommend() { getRecommend().then((res) => { res = res.data.response; if (res.code === ERR_OK) { this.recommends = res.data.banner; console.log(res.data.banner); } }); }, }, };
需安裝better-scroll,若better-scroll版本較高,需參考文章 新版bscroll輪播圖無縫切換 進行必要的代碼修改api
本項目版本 "better-scroll": "^0.1.15"
在slider.vue中添加:<template> <div class="slider" ref="slider"> <div class="slider-group" ref="sliderGroup"> <slot></slot> </div> <div class="dots"> <span class="dot" :class="{active:currentPageIndex === index}" v-for="(item,index) in dots" :key="index"> </span> </div> </div> </template> <script> import BScroll from 'better-scroll'; import { addClass } from '../../common/js/dom'; export default { name: 'slider', props: { loop: { type: Boolean, default: true, // 設置循環狀態 }, autoPlay: { // 自動播放 type: Boolean, default: true, }, interval: { // 輪播時間間隔 type: Number, default: 4000, }, }, data() { return { dots: [], currentPageIndex: 0, }; }, mounted() { // 瀏覽器一般17ms刷新一次 setTimeout(() => { this._setSliderWidth(); this._initDots(); this._initSlider(); if (this.autoPlay) { this._play(); } }, 20); window.addEventListener('resize', () => { if (!this.slider) { return; } this._setSliderWidth(true); this.slider.refresh(); }); }, destroyed() { clearTimeout(this.timer); }, methods: { // 設置slider的寬度 _setSliderWidth(isResize) { this.children = this.$refs.sliderGroup.children; let width = 0; let sliderWidth = this.$refs.slider.clientWidth; for (let i = 0; i < this.children.length; i++) { let child = this.children[i]; addClass(child, 'slider-item'); child.style.width = `${sliderWidth}px`; width += sliderWidth; } if (this.loop && !isResize) { width += 2 * sliderWidth; } this.$refs.sliderGroup.style.width = `${width}px`; }, _initSlider() { this.slider = new BScroll(this.$refs.slider, { // 容許橫向滾動 scrollX: true, // 不容許縱向滾動 scrollY: false, momentum: false, snap: true, snapLoop: this.loop, snapThreshold: 0.3, snapSpeed: 400, click: true, }); this.slider.on('scrollEnd', () => { let pageIndex = this.slider.getCurrentPage().pageX; if (this.loop) { pageIndex -= 1; } this.currentPageIndex = pageIndex; if (this.autoPlay) { clearTimeout(this.timer); this._play(); } }); }, _initDots() { this.dots = new Array(this.children.length); }, _play() { let pageIndex = this.currentPageIndex + 1; if (this.loop) { pageIndex += 1; } this.timer = setTimeout(() => { this.slider.goToPage(pageIndex, 0, 400); }, this.interval); }, }, }; </script> <style lang="scss" scoped> .slider { min-height: 1px; .slider-group { position: relative; overflow: hidden; // white-space 屬性設置處理元素內的空白 // 空白指Space(空格)、Enter(回車)、Tab(製表符) // nowrap 全部空格、回車、製表符都合併成一個空格,文本不換行 white-space: nowrap; .slider-item { float: left; /* 爲元素設定的寬度和高度決定了元素的邊框盒 */ box-sizing: border-box; overflow: hidden; text-align: center; a { display: block; width: 100%; overflow: hidden; text-decoration: none; } img { display: block; width: 100%; } } } .dots { position: absolute; right: 0; left: 0; bottom: 12px; text-align: center; font-size: 0; .dot { display: inline-block; margin: 0 4px; width: 8px; height: 8px; border-radius: 50%; background: $color-text-l; &.active { width: 20px; border-radius: 5px; background: $color-text-ll; } } } } </style>