這篇文章將為大家詳細講解有關vue如何實現整屏滾動切換效果,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
1、下載vue-awesome-swiper
npm i vue-awesome-swiper -S
2、在main.js引入
import vueAwesomeSwiper from 'vue-awesome-swiper' Vue.use(vueAwesomeSwiper);
3、直接上案例,新建一個路由頁面
<template>
<div class="hello-world">
<swiper id="swiperBox" v-bind:options="swiperOption" ref="mySwiper">
<swiper-slide class="swiper-slide" v-for="(item, index) in list" :key="index">
<div class="page">
<h4>第{{item}}頁</h4>
</div>
</swiper-slide>
</swiper>
</div>
</template>
<script>
import { swiper, swiperSlide } from "vue-awesome-swiper";
export default {
name: "HelloWorld",
data() {
return {
list: [], //輪換列表
swiperOption: {
notNextTick: true, //notNextTick是一個組件自有屬性,如果notNextTick設置為true,組件則不會通過NextTick來實例化swiper,也就意味著你可以在第一時間獲取到swiper對象,假如你需要剛加載遍使用獲取swiper對象來做什么事,那么這個屬性一定要是true
direction: "vertical", //水平方向移動
grabCursor: true, //鼠標覆蓋Swiper時指針會變成手掌形狀,拖動時指針會變成抓手形狀
setWrapperSize: true, //Swiper使用flexbox布局(display: flex),開啟這個設定會在Wrapper上添加等于slides相加的寬或高,在對flexbox布局的支持不是很好的瀏覽器中可能需要用到。
autoHeight: true, //自動高度。設置為true時,wrapper和container會隨著當前slide的高度而發生變化
slidesPerView: 1, //設置slider容器能夠同時顯示的slides數量(carousel模式)??梢栽O置為數字(可為小數,小數不可loop),或者 'auto'則自動根據slides的寬度來設定數量。loop模式下如果設置為'auto'還需要設置另外一個參數loopedSlides。
mousewheel: true, //開啟鼠標滾輪控制Swiper切換??稍O置鼠標選項,默認值false
mousewheelControl: true, //同上
height: window.innerHeight, // 高度設置,占滿設備高度
resistanceRatio: 0, //抵抗率。邊緣抵抗力的大小比例。值越小抵抗越大越難將slide拖離邊緣,0時完全無法拖離。本業務需要
observeParents: true, //將observe應用于Swiper的父元素。當Swiper的父元素變化時,例如window.resize,Swiper更新
// 如果自行設計了插件,那么插件的一些配置相關參數,也應該出現在這個對象中,如下debugger
//debugger: true,
// swiper的各種回調函數也可以出現在這個對象中,和swiper官方一樣
on: {
//監聽滑動切換事件,返回swiper對象
slideChange: () => {
let swiper = this.$refs.mySwiper.swiper;
console.log(swiper.activeIndex); //滑動打印當前索引
if (swiper.activeIndex === this.list.length - 1) {
//到最后一個加載更多數據
let newList = [];
let listLength = this.list.length;
for (let i = 0; i < 10; i++) {
newList.push(listLength + i);
}
this.list = this.list.concat(newList);
}
}
}
}
};
},
created() {
//從后臺獲取數據
this.list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
},
// 如果你需要得到當前的swiper對象來做一些事情,你可以像下面這樣定義一個方法屬性來獲取當前的swiper對象,同時notNextTick必須為true
computed: {
swiper() {
return this.$refs.mySwiper.swiper;
}
},
mounted() {
// this.swiper.slideTo(3, 1000, false); //手動跳到指定頁
},
components: {
swiper,
swiperSlide
}
};
</script>
<style scoped>
.swiper-slide {
font-size: 24px;
text-align: center;
line-height: 100px;
}
.swiper-slide:nth-child(2n) {
background: skyblue;
}
.swiper-slide:nth-child(2n-1) {
background: seashell;
}
</style>4、電腦瀏覽器可能有問題,請使用真機測試
關于vue如何實現整屏滾動切換效果就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。