vue中使用swiper做輪播頁面,標題樣式隨着輪播頁面改變而改變


vue中使用swiper做輪播頁面,標題樣式隨着輪播頁面改變而改變

知識點: 標題組件與輪播組件之間交互(vue兄弟組件之間傳值)

兄弟之間傳遞數據需要借助於事件車,通過事件車的方式傳遞數據

1、創建一個Vue的實例,讓各個兄弟共用同一個事件機制。

2、傳遞數據方,通過一個事件觸發bus.$emit(方法名,傳遞的數據)。

3、接收數據方,通過mounted(){}觸發bus.$on(方法名,function(接收數據的參數){用該組件的數據接收傳遞過來的數據}),此時函數中的this已經發生了改變,可以使用箭頭函數。

實例如下:

我們可以創建一個單獨的js文件eventVue.js,內容如下

import Vue from 'vue';
 
export default new Vue();

  

整個文件這兩句就夠啦!!!!

標題組件

子組件1

  • 文件名: nav.vue
 1 <template>
 2   <div class="nav-container">
 3     <ul style="padding: 0">
 4       <li v-for="(item, index) in newBreadcrumb" 
 5       :key="index"
 6       :class="nowIndex===index ? 'selected-nav nav-title':'unselected-nav nav-title'">
 7       {{item.name}}
 8       <span class="line">/</span>
 9       </li>
10     </ul>
11   </div>
12 </template>
13 <script>
14 import eventHub from '../../../../commonJS/eventVue.js'
15 export default {
16   props:['breadcrumb'],
17   data(){
18     return {
19       newBreadcrumb: [],
20       nowIndex: 0,
21     }
22   },
23   created(){
24     console.log("breadcrumb", this.breadcrumb) //父子組件傳值,傳過來一個標題數組
25     //對數組進行處理,轉為數組對象,給原數組成員加了key,為name
26     for(let i in this.breadcrumb){
27       let breadcrumbObject = {}
28       this.$set(breadcrumbObject, 'name', this.breadcrumb[i] )
29       this.newBreadcrumb.push(breadcrumbObject)
30     }
31     
32   },
33   mounted(){
34     //nav組件接收swiper組件滑動時傳過來的index
35     eventHub.$on('slideTab', (index)=>{
36       this.nowIndex = index
37     });
38   },
39 }
40 </script>
41 <style>
42 .nav-container {
43   font-size: 0;
44   padding: .2rem 0 0 .2rem;
45 }
46 .nav-title {
47   font-family: "DINPro-Medium";
48   font-size: 0.18rem;
49 }
50 li {
51   display:inline-block;
52 }
53 .nav-title:nth-child(3) .line{
54   display: none;
55 }
56 .selected-nav {
57   color: #554344;
58 }
59 .unselected-nav {
60   color: #B8B8B8;
61 }
62 </style>

 

子組件2
  • 文件名: swiper.vue
 1 <template>
 2   <div router>
 3     <div v-for="(route, index) in $router.options.routes" :key="index">
 4           <!-- {{route.name}} -->
 5       <div v-if="route.name === 'CompetitionObjective'">
 6         <template v-for="(items, index) in route.children">
 7             <swiper
 8             :key="index"
 9             v-if="items.path === 'MRtableAssociation/'"
10             :options="swiperOption"
11             ref="mySwiper"
12             >
13             <swiper-slide
14                 v-for="(item, index) in items.children"
15                 :key="index"
16                 class="mdrt-item"
17             >
18                 <keep-alive>
19                 <component :is="item.component" v-bind:MDRTcompetitionCd="MDRTcompetitionCd" v-if="MDRTcompetitionCd.length>0"></component>
20                 </keep-alive>
21             </swiper-slide>
22             <div class="swiper-pagination" slot="pagination"></div>
23             </swiper>
24         </template>
25       </div>
26     </div>
27   </div>
28 </template>
29 <script>
30 import eventHub from '../../../../commonJS/eventVue.js'
31 export default {
32   props:{
33     MDRTcompetitionCd: {
34       type: String,
35       default: ""
36     }
37   },
38   data(){
39     return {
40       swiperOption: {
41         pagination: {
42           el: ".swiper-pagination"
43         },
44       }
45     }
46   },
47   mounted() {
48       var mySwiper = this.$refs['mySwiper'][0].swiper
49       mySwiper.on('slideChange', () => { // 監控滑動后當前頁面的索引,將索引發射到導航組件
50       // 左右滑動時將當前slide的索引發送到nav組件,eventHub是兄弟組件之間傳值的橋梁
51       eventHub.$emit('slideTab', mySwiper.activeIndex);
52     });
53   }
54 }
55 </script>
56 <style scoped>
57 .swiper-table {
58   height: 3.65rem;
59   border-top: 0.01rem solid #d8d8d8;
60 }
61 
62 .swiper-table >>> .swiper-container-horizontal > .swiper-pagination-bullets {
63   bottom: -0.2rem;
64   left: 0;
65   width: 100%;
66   z-index: 1000;
67 }
68 .swiper-table >>> .swiper-pagination-bullet-active {
69   background: #d31145 !important;
70   /* width: .34rem !important; */
71 }
72 .mdrt-item {
73   font-family: 'DINPro-Medium';
74   font-size: .18rem;
75   color: #554344 !important;
76 } 
77 </style>

 

父組件:

關鍵代碼

<v-nav :breadcrumb='breadcrumb' v-if="breadcrumb.length > 0"></v-nav>
<v-swiper :MDRTcompetitionCd='MDRTcompetitionCd'></v-swiper>

  




如有問題歡迎指正:qq: 1534147975


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM