温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何使用Vue代码实现一个上下滚动加载组件

发布时间:2022-10-27 11:29:21 阅读:251 作者:iii 栏目:开发技术
Vue开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

本篇内容主要讲解“如何使用Vue代码实现一个上下滚动加载组件”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用Vue代码实现一个上下滚动加载组件”吧!

组件代码

// scrollLoader.vue
// 滚动加载组件

<style scoped>
  .container-main {margin0 auto; overflow: auto; overflow-x: hidden; padding0;}
  .loadingwidth100%height40pxposition: relative; overflow: hidden; text-align: center; margin5px 0 ; color#999font-size13px;}
  .loading-icon{color#707070;};
  .loader {
    font-size10px;
    margin8px auto;
    text-indent: -9999em;
    width24px;
    height24px;
    border-radius50%;
    background#999;
    background-moz-linear-gradient(left, #999 10%rgba(255255255042%);
    background-webkit-linear-gradient(left, #999 10%rgba(255255255042%);
    background-o-linear-gradient(left, #999 10%rgba(255255255042%);
    background-ms-linear-gradient(left, #999 10%rgba(255255255042%);
    backgroundlinear-gradient(to right, #999 10%rgba(255255255042%);
    position: relative;
    -webkit-animation: load3 1s infinite linear;
    animation: load3 1s infinite linear;
  }
  .loader:before {
    width50%;
    height50%;
    background#999;
    border-radius100% 0 0 0;
    position: absolute;
    top0;
    left0;
    content'';
  }
  .loader:after {
    background#f5f5f5;
    width72%;
    height75%;
    border-radius68%;
    content'';
    margin: auto;
    position: absolute;
    top0;
    left0;
    bottom0;
    right0;
  }
  @-webkit-keyframes load3 {
  0% {
    -webkit-transformrotate(0deg);
    transformrotate(0deg);
  }
  100% {
    -webkit-transformrotate(360deg);
    transformrotate(360deg);
  }
  }
  @keyframes load3 {
  0% {
    -webkit-transformrotate(0deg);
    transformrotate(0deg);
  }
  100% {
    -webkit-transformrotate(360deg);
    transformrotate(360deg);
  }
  }

</style>

<template>
  <div id="scrollLoader-container" class="container-main">
    <div class="loading" v-if="topLoading">
      <div class="loader">加载中...</div>
    </div>

    <div :>
      <slot></slot>
    </div>

    <div class="loading" v-if="bottonLoading">
      <div class="loader">加载中...</div>
    </div>
  </div>
</template>

<script>
  export default {
    name"scroll-loader",

    props: {
      //给slot传一个最小值,保证一开始能出现滚动条
      'minHeight': {
        typeNumber,
        default800
      }, 

    },

    created(){
    },
    computed: {
      realMinHeight(){
        return this.minHeight + 30
      }
    },
    data() {
      return {
        topLoadingfalse,
        bottonLoadingfalse,

        stopTopLoadingfalse//是否停止传播滚动到顶部事件
        stopBottonLoadingfalse//是否停止传播滚动到底部事件
      }
    },
    mounted(){
      this.listenScroll();
    },

    methods: {
      listenScroll(){
        var me = this;
        var topDone = (stopTopLoading) => {
          me.topLoading = false;
          if(stopTopLoading) me.stopTopLoading = true;
        };

        var bottonDone = (stopBottonLoading) => {
          me.bottonLoading = false;
          if(stopBottonLoading) me.stopBottonLoading = true;
        };
        setTimeout(function(){
          var scrollContainer = document.getElementById('scrollLoader-container');

          scrollContainer.onscroll = function(){

            if(scrollContainer.scrollTop<=0 && !me.stopTopLoading){
              if(me.topLoadingreturn;

              me.topLoading = true;
              me.$emit('scroll-to-top', topDone);
            }
            if((scrollContainer.offsetHeight + scrollContainer.scrollTop+1 >= scrollContainer.scrollHeight) && !me.stopBottonLoading){
              if(me.bottonLoadingreturn;

              me.bottonLoading = true;
              scrollContainer.scrollTop += 40;
              me.$emit('scroll-to-botton', bottonDone);
            }
          }
        }, 50)
      },

    }
  }
</script>

vue是什么

Vue是一套用于构建用户界面的渐进式JavaScript框架,Vue与其它大型框架的区别是,使用Vue可以自底向上逐层应用,其核心库只关注视图层,方便与第三方库和项目整合,且使用Vue可以采用单文件组件和Vue生态系统支持的库开发复杂的单页应用。

到此,相信大家对“如何使用Vue代码实现一个上下滚动加载组件”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

vue
AI

开发者交流群×