效果圖

src/components/scroll/index.vue
<template>
<!-- 通過ref可以獲取到dom物件 -->
<swiper :options="swiperOption" ref="swiper">
<div >
<me-loading :text="pullDownText" inline ref="pullDownLoading" />
</div>
<swiper-slide>
<!-- 所有內容放在插槽里 -->
<slot></slot>
</swiper-slide>
<div >
<me-loading :text="pullUpText" inline ref="pullUpLoading" />
</div>
<div slot="scrollbar"></div>
</swiper>
</template>
<script>
import { Swiper, SwiperSlide } from 'vue-awesome-swiper';
import 'swiper/css/swiper.css';
import MeLoading from 'components/loading';
export default {
name: 'Scrollbar',
title: 'Scrollbar',
components: {
Swiper,
SwiperSlide,
MeLoading
},
data() {
return {
pulling:false,//是否正在下拉中
pullDownText:'向下拉動會重新加載幻燈片哦',
pullUpText:'向上拉動會加載更多哦',
swiperOption: {
scrollbar: {
el: '.swiper-scrollbar',
hide: true
},
direction:'vertical',
slidesPerView:'auto',
freeMode:true,
setWrapperSize:true,
on:{//下拉重繪時觸發的事件
sliderMove:this.sliderMove,//一開始使用sliderMove,有bug
touchEnd:this.touchEnd,
transitionEnd:this.scrollEnd//滾動結束
}
},
}
},
props:{
recommends:{
type:[Array,Object],
default(){
return [];
}
}
},
watch:{//當recommends值發生改變時
recommends(){
this.$refs.swiper && this.$refs.swiper.$swiper.update();//更新滾動條長度
}
},
methods:{
sliderMove(){
if(this.pulling) return;//正在下拉中,則不重復下拉
const swiper=this.$refs.swiper.$swiper;
this.$emit("scrolling",swiper.translate,swiper);
if(swiper.translate>0){//向下拉
if(swiper.translate>100){//超出規定的高度
this.$refs.pullDownLoading.setText("開始下拉...");
}else{
this.$refs.pullDownLoading.setText("向下拉動會重新加載幻燈片哦");
}
}else if(swiper.isEnd){//上拉
//是否達到上拉的觸發條件
//swiper的位移加上swiper的高度(617px)-50px的值如果大于當前內容高度
//swiper.translate這個屬性可以獲取到wrapper的位移,其實可以理解為滾動條滾動的距離
//swiper.height這個屬性獲取swiper容器的高度, 也就是顯示區域的高度
//50px是我們設定的一個值,為了讓頁面不是到達最低部的時候,可以提前加載內容
//parseInt(swiper.$wrapperEl.css('height'))是wrapper的HTML元素的height屬性, 也就是所有內容的高度
const isPullUp=Math.abs(swiper.translate)+swiper.height-50 > parseInt(swiper.$wrapperEl.css('height'));
if(isPullUp){//開始上拉
this.$refs.pullUpLoading.setText("開始上拉");
}else{//保持初始化
this.$refs.pullUpLoading.setText('向上拉動會加載更多哦');
}
}
},
touchEnd(){
if(this.pulling) return;//正在下拉中,則不重復下拉
const swiper=this.$refs.swiper.$swiper;
if(swiper.translate>100){
this.pulling=true;//正在下拉中
swiper.allowTouchMove=false;//禁止觸摸
swiper.setTransition(swiper.params.speed);//設定初始速度
swiper.setTranslate(100);//移動到設定的位置(拖動過度時回到設定的位置)
swiper.params.virtualTranslate=true;//定住不給回彈
this.$refs.pullDownLoading.setText("正在下拉中...");//設定正在重繪中的文字
this.$emit("pull-down",this.pullDownEnd);//觸發訊息,傳遞結束下拉的函式
}else if(swiper.isEnd){//上拉
//是否達到上拉的觸發條件
const isPullUp=Math.abs(swiper.translate)+swiper.height-30>parseInt(swiper.$wrapperEl.css('height'));
if(isPullUp){//開始上拉
this.pulling=true;
swiper.allowTouchMove=false;//禁止觸摸
swiper.setTransition(swiper.params.speed);//設定初始速度
swiper.setTranslate(-(parseInt(swiper.$wrapperEl.css('height'))+50-swiper.height));//超過拉動距離時回彈
swiper.params.virtualTranslate=true;//定住不給回彈
this.$refs.pullUpLoading.setText("正在上拉中...");//設定正在重繪中的文字
this.$emit("pull-up",this.pullUpEnd);//觸發訊息,傳遞結束下拉的函式
}
}
},
pullDownEnd(){
const swiper=this.$refs.swiper.$swiper;
this.pulling=false;//下拉結束
this.$refs.pullDownLoading.setText("下拉結束");//設定加載結束后的文字
swiper.allowTouchMove=true;//可以觸摸
swiper.setTransition(swiper.params.speed);//設定初始速度
swiper.params.virtualTranslate=false;//可以回彈
swiper.setTranslate(0);//移動到最初的位置
//影片完成后再觸發結束事件
setTimeout(()=>{
this.$emit("pulldown-end");
},swiper.params.speed);
},
pullUpEnd(){
const swiper=this.$refs.swiper.$swiper;
this.pulling=false;
this.$refs.pullUpLoading.setText("上拉結束");//設定加載結束后的文字
swiper.allowTouchMove=true;//可以觸摸
swiper.params.virtualTranslate=false;//可以回彈
},
scrollTop(){
this.$refs.swiper.$swiper.slideTo(0);//回到頂部
},
scrollEnd(){
this.$emit("scroll-end",this.$refs.swiper.$swiper.translate,this.$refs.swiper.$swiper,this.pulling);
}
}
}
</script>
<style lang="scss" scoped>
.swiper-container{
width:100%;
height:100%;
overflow:hidden;
}
.swiper-slide{
height:auto;
}
.mine-scroll-pull-down{
position:absolute;
left:0;
bottom:100%;
width:100%;
height:80px;
}
.mine-scroll-pull-up{
position:absolute;
left:0;
top:100%;
width:100%;
height:30px;
}
</style>
src/pages/home/index.vue
<template>
<div >
<div >
<home-header : ref="header"/>
</div>
<scrollbar :data="https://www.cnblogs.com/chenyingying0/p/recommends" @pull-down="pullRefresh" @pull-up="loadMore" @scroll-end="scrollEnd" ref="scroll" @scrolling="changeHeader">
<slider ref="mySwiper" />
<home-nav />
<!-- 熱門推薦加載完成后更新滾動條 -->
<recommend @loaded="updateScroll" ref="recommend" />
</scrollbar>
<div >
<me-backtop :visible="backtopVisible" @backtop="backtop" />
</div>
<!-- 該頁面自己的子路由 -->
<router-view></router-view>
</div>
</template>
<script>
import Slider from 'components/slider';
import Scrollbar from 'components/scroll';
import HomeNav from './nav';
import Recommend from './recommend';
import MeBacktop from 'components/backtop';
import HomeHeader from './header';
export default {
name:"Home",
components:{
Slider,
Scrollbar,
HomeNav,
Recommend,
MeBacktop,
HomeHeader
},
data(){
return{
recommends:[],
backtopVisible:false,
headerTransition:false
}
},
methods:{
updateScroll(recommends){
this.recommends=recommends;
},
pullRefresh(end){//重繪輪播圖
this.$refs.mySwiper.getSliders().then(end);
},
loadMore(end){//加載更多
this.$refs.recommend.getRecommends().then(end).catch(err=>{
if(err){//沒有更多圖片了
console.log(err);
}
end();
});
},
scrollEnd(translate,swiper,pulling){//下拉重繪結束
//顯示回到頂部按鈕
this.backtopVisible=translate<0 && -translate>swiper.height;//向下拉并且距離大于一屏
if(!pulling){
this.changeHeader(translate,swiper);
}
},
backtop(){
this.$refs.scroll && this.$refs.scroll.scrollTop();//回到頂部
},
changeHeader(translate,swiper){//修改頭部導航背景色
if(translate>0){//下拉中,隱藏導航
this.$refs.header.hide();
return;
}else{
this.$refs.header.show();
this.headerTransition=-translate>200;//大于200時回傳true,改變導航背景色
}
},
}
}
</script>
<style lang="scss" scoped>
.home{
width:100%;
height:100%;
}
.g-header-container{
position:absolute;
left:0;
top:0;
width:100%;
z-index:999;
height:50px;
}
.g-backtop-container{
position: absolute;
z-index:1100;
right:10px;
bottom:60px;
}
</style>
src/pages/home/header.vue
<template>
<div v-show="headerVisible">
<i ></i>
<div >搜索框</div>
<i ></i>
</div>
</template>
<script>
export default {
name:"HomeHeader",
data(){
return{
headerVisible:true
}
},
methods:{
show(){
this.headerVisible=true;
},
hide(){
this.headerVisible=false;
}
}
}
</script>
<style lang="scss" scoped>
.header{
background:transparent;
transition:background-color 0.5s;
display: flex;
justify-content: space-between;
align-items: center;
padding:5px 20px;
.iconfont{
font-size:24px;
color:#fff;
}
//下拉到一定位置,添加.header-transition,使導航背景色變紅
&.header-transition{
background-color:rgba(222, 24, 27, 0.9);
}
.headerr-center{
flex:1;
}
}
</style>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/131631.html
標籤:JavaScript
上一篇:JavaScript中的作用域
