这篇文章将为大家详细讲解有关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模式)。可以设置为数字(可为小数,小数不可loop),或者 'auto'则自动根据slides的宽度来设定数量。loop模式下如果设置为'auto'还需要设置另外一个参数loopedSlides。
mousewheel: true, //开启鼠标滚轮控制Swiper切换。可设置鼠标选项,默认值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如何实现整屏滚动切换效果就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。