【VUE中實現錨點的平滑滾動 + VUE 全屏滾動】


需求:點擊TAB,可以實現板塊的快速定位,平滑滾動,

起初思路把買個TAB對應的頁面當成單獨的頁面,所以方向錯了,成了不同頁面間的跳轉,其實應該是一個頁面中有不同的組件,每個組件占一屏,點擊TAB錨點定位

1.錨點的平滑滾動--------------------------------

1.效果

圖片質量差,湊活看吧,不知道怎么只播放一次

 

 

2.最外層:layout組件

header為頭部導航TAB

transition包裹了router-view,用於給不同頁面之間的跳轉加動效

 1 <template>
 2     <div class="layout">
 3         <Header
 4             @routerEvent="layoutEvent"
 5             :changeActive="changeActive"
 6         ></Header>
 7         <transition
 8             mode="out-in"
 9             :enter-active-class="enterClass" 
10             :leave-active-class="leaveClass">
11             <router-view></router-view>
12         </transition>
13         <!-- <Footer></Footer> -->
14     </div>
15 </template>

3.頭部導航:Header組件

span為Tab,添加點擊事件,向父組件layout中emit出去

 1 <template>
 2     <div class="header" :class="{'teshu': changeActive===0}">
 3         <div class="header_tabBox">
 4             <img src="../../assets/img/logo.png" alt="">
 5             <span
 6              v-for="(item, idx) in tabList" :key="idx" 
 7              :class="{'active': changeActive === idx}"
 8              @click="routerEvent(item, idx)">{{item.label}}
 9              </span>
10         </div>
11     </div>
12 </template>
1     methods: {
2         routerEvent(item, idx) {
3             const vm = this;
4             vm.$emit('routerEvent',item, idx)
5         }
6     }

 

4.關鍵部分:

在具體頁面部分,將所有組件,包括home, aboutus,service,case,concat,分別對應首頁,關於我們,服務對象,案例,聯系五個部分,引入在一起

因為我們實現的需求看似是不同的頁面跳轉並且可以順滑滾動,實際是一個頁面中錨點的跳轉

在每一個組件的最外層需要加入對應id,比如.scroll-item

 1 <template>
 2   <div class="outer">
 3     <div id="home" class="recommendPage scroll-item">
 4       <swiper :options="swiperOption" ref="mySwiper">
 5         <swiper-slide v-for="(item, idx) in bannerList" :key="idx">
 6           <img :src="item.url" alt="">
 7         </swiper-slide>
 8         <div class="swiper-pagination" slot="pagination"></div>
 9         <div class="swiper-button-prev" slot="button-prev"></div>
10         <div class="swiper-button-next" slot="button-next"></div>
11       </swiper>
12     </div>
13     <AboutUs></AboutUs>
14     <Service></Service>
15     <Case></Case>
16     <Concat></Concat>
17   </div>
18 
19 </template>

 

 1         layoutEvent(item, idx) {
 2             const vm = this;
 3             let items  = document.querySelectorAll(".scroll-item");
 4             for (var i = 0; i < items.length; i++) {
 5                 if (idx === i) {
 6                         // location.hash = item.name
 7                     items[i].scrollIntoView({
 8                       block: "start",//默認跳轉到頂部
 9                       behavior: "smooth"//滾動的速度
10                     });
11                 }
12             }
13         }

 2. 全屏滾動

上邊是第一種思路用到錨點,其實還可以用fullpage實現全屏滾動

1.安裝

1 npm install --save vue-fullpage.js

2.main.js中引入

1 import Vue from 'vue'
2 
3 import 'fullpage.js/vendors/scrolloverflow' // Optional. When using scrollOverflow:true
4 
5 import './fullpage.scrollHorizontally.min' // Optional. When using fullpage extensions
6 
7 import VueFullPage from 'vue-fullpage.js'   (注意page的P是大寫)
8 
9 Vue.use(VueFullPage); 

3.HTML結構

ref  id  section都是必須的固定結構

 1 <div>
 2     <full-page ref="fullpage" :options="options" id="fullpage">
 3     <div class="section">
 4       First section ...
 5     </div>
 6     <div class="section">
 7       Second section ...
 8     </div>
 9   </full-page>
10 </div>

 

1       options: {
2         licenseKey: 'OPEN-SOURCE-GPLV3-LICENSE',
3         scrollOverflow: true,
4         scrollBar: false,
5         menu: '#menu',
6         // anchors: ['homeL', 'aboutUsL', 'serviceL', 'caseL', 'concatL'],
7         anchors: ['首頁', '關於我們', '服務對象', '案例展示', '聯系我們'],
8       },

 

  • anchors:定義錨鏈接,默認值為[],有了錨鏈接,就可以快速的打開定位到某一頁面,也是我們實現導航欄的關鍵;

比如這個例子中,我本來用的是第一種思路:錨點跳轉+history模式,改用fullpage之后,我的路由中會出現對應的hash

  • lockAnchors:是否鎖定錨鏈接,默認為false,如果設定為true,定義的錨鏈接,也就是anchors屬性就沒有效果了;

  • menu:綁定菜單,設定相關屬性與anchors的值相對應后,菜單可以控制滾動,默認為false;

  • navigation:是否顯示導航,默認為false,如果設置為true,會顯示小圓點,作為導航;

  • navigationPostion:導航小圓點的位置,可以設置為left或right;

  • navigationTooltips:導航小圓點的tooltips的設置,默認為[],注意按照順序設置。


免責聲明!

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



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