最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue的滾動條插件實(shí)現(xiàn)代碼

 更新時(shí)間:2019年09月07日 10:49:15   作者:muamaker  
這篇文章主要介紹了vue的滾動條插件實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了vue的滾動條插件實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

效果如下

代碼如下

<template>
  <div class="vue-scroll" ref="vueScrollW">
    <div class="vue-scroll-w" ref="vueScroll" >
      <div class="vue-scroll-c" :style="{width:cWidth}">
        <slot></slot>
      </div>
    </div>
    <div class="vue-scrollbar" v-if="rate < 1">
      <div class="vue-scrollbar-thumb"
      :style="{height:thumbH,top:thumbTop}"
      @mousedown="onmousedown"
      @mouseup="onmouseup"
      ></div>
    </div>
  </div>
</template>
 
<script>
 
 
 
export default {
  name:"vue-scroll",
  data(){
    return {
      thumb:0,
      top:0,
      rate:2,
      moveTop:null,
      isDrag:false,
      cw:10,
      observer:null
    }
  },
  computed:{
    thumbH(){
      return this.thumb + "px";
    },
    thumbTop(){
      return this.top + "px";
    },
    cWidth(){
      return this.cw + "%";
    }
    
  },
  updated(){
    if(!window.MutationObserver){
      this.refresh();
    }
  },
  mounted(){
    var me = this;
    me.$refs.vueScroll.addEventListener("scroll",me.onscroll.bind(me));
    window.addEventListener("mouseup",me.onmouseup.bind(me));
    window.addEventListener("mousemove",me.onmousemove.bind(me));
 
    if(window.MutationObserver){
      //MutationObserver 最低只兼容 ie11
      me.observer = new window.MutationObserver(me.mutationCallback.bind(me));
      me.observer.observe(me.$refs.vueScroll, {
        attributes: true,
        childList: true,
        subtree: true
      });
    }
    
    me.refresh();
  },
  methods:{
    mutationCallback(mutationsList){
      this.refresh();
    },
    onscroll(){
      this.top = this.$refs.vueScroll.scrollTop * this.rate; //計(jì)算滾動條所在的高度
      if(this.rate < 1){
        this.eventTrigger(this.top);
      }
    },
    refresh(){
      var me = this;
      var vueScroll = me.$refs.vueScroll;
      var rate = vueScroll.clientHeight / vueScroll.scrollHeight; //滾動條高度的比例,也是滾動條top位置的比例
      me.rate = rate;
      if(rate < 1){
        //需要出現(xiàn)滾動條,并計(jì)算滾動條的高度
        me.thumb = rate * vueScroll.clientHeight; //滾動條的 bar 的高度
        //計(jì)算出原生的滾動條的寬度
        var w = me.$refs.vueScrollW.clientWidth;
        //根據(jù)比例,轉(zhuǎn)換為內(nèi)容的百分比
        me.cw = w/vueScroll.clientWidth *100;
      }else{
        //不需要出現(xiàn)滾動條
         me.thumb = 0;
         me.cw = 10;
      }
    },
  
    onmousedown(){
      this.isDrag = true;
      this.moveTop = null;
    },
    onmouseup(){
      this.isDrag = false;
    },
    onmousemove(e){
      if(this.isDrag){
        if(this.moveTop !== null){
          var speed = e.screenY - this.moveTop;
          var top = this.top + speed;
          this.scrollThumb(top);
        }
        this.moveTop = e.screenY;
        e.preventDefault();
      }
       
    },
    scrollThumb(top){
      if(top < 0 ){
        top = 0;
         
      }
      if(top > this.$refs.vueScroll.clientHeight-this.thumb){
        top = this.$refs.vueScroll.clientHeight-this.thumb;
         
      }
       
      this.$refs.vueScroll.scrollTop = top/this.rate;
      this.top = top;
    },
    eventTrigger(top){
      if(top === 0){
        this.$emit("reachTop"); //到達(dá)頂部
      }
      if(top === this.$refs.vueScroll.clientHeight-this.thumb){
        this.$emit("reachBottom"); //到達(dá)底部與
      }
      this.$emit("vuescroll",this.$refs.vueScroll.scrollTop,this.top);//返回內(nèi)容滾動的高度 和 滾動條所在的高度
    },
    scrollTo(scrollTop){
      //對外的api,滾動的內(nèi)容的哪里
       this.$refs.vueScroll.scrollTop = scrollTop;
       this.$nextTick(()=>{
         this.onscroll();
       })
    }
  },
  destroyed(){
    var me = this;
    me.$refs.vueScroll && me.$refs.vueScroll.removeEventListener("scroll",me.onscroll.bind(me));
    window.removeEventListener("mouseup",me.onmouseup.bind(me));
    window.removeEventListener("mousemove",me.onmousemove.bind(me));
    me.observer&&me.observer.disconnect();
  }
}
</script>
 
<style lang="scss" scoped>
.vue-scroll{
  height: 100%;
  width: 100%;
  overflow: hidden;
  position: relative;
  .vue-scroll-w{
    width: 1000%;
    height: 100%;
    overflow: auto;
    .vue-scroll-c{
      position: relative;
      width: 10%;
    }
  }
  .vue-scrollbar{
    position: absolute;
    z-index: 1;
    right: 0;
    top: 0;
    width: 4px;
    height: 100%;
    background: #EEEEEE;
    opacity: 0.6;
    .vue-scrollbar-thumb{
      position: absolute;
      top: 0;
      right: 0;
      width: 4px;
      border-radius: 4px;
      background: #D3D3D3;
      &:hover{
        background: #bbb;
      }
      &:active{
        background: #aaa;
      }
    }
  }
}
</style>

使用

<template>
  <div class="scroll">
    <vueScroll>
      <ul>
        <li v-for="item in 60" :key="item">{{item}}</li>
      </ul>
    </vueScroll>
  </div>
</template>
 
<script>
import vueScroll from "@/components/vue-scroll.vue"
export default {
  data(){
    return {
      count:60
    }
  },
  components:{
    vueScroll
  },
  mounted(){
   
  }
}
</script>
<style lang="less" scoped>
.scroll{
  width: 400px;
  height: 600px;
  margin: 0 auto;
  border: 1px solid red;
  ul{
    li{
      line-height: 30px;
      border-bottom: 1px solid #ddd;
    }
  }
}
</style>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue如何讀取本地文件

    vue如何讀取本地文件

    這篇文章主要介紹了vue如何讀取本地文件問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue項(xiàng)目中使用rem替換px的實(shí)現(xiàn)示例

    vue項(xiàng)目中使用rem替換px的實(shí)現(xiàn)示例

    移動端頁面適配,rem和vw適配方案,本文主要介紹了vue項(xiàng)目中使用rem替換px的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Vue2.x配置路由導(dǎo)航守衛(wèi)實(shí)現(xiàn)用戶登錄和退出

    Vue2.x配置路由導(dǎo)航守衛(wèi)實(shí)現(xiàn)用戶登錄和退出

    之前在Vue的學(xué)習(xí)中通過路由導(dǎo)航守衛(wèi)控制實(shí)現(xiàn)了用戶登錄模塊的功能,本文基于Vue2.x進(jìn)行實(shí)現(xiàn),在此將實(shí)現(xiàn)過程進(jìn)行記錄與總結(jié),感興趣的可以了解一下
    2021-08-08
  • vue.js使用Element-ui實(shí)現(xiàn)導(dǎo)航菜單

    vue.js使用Element-ui實(shí)現(xiàn)導(dǎo)航菜單

    這篇文章主要為大家詳細(xì)介紹了vue.js使用Element-ui中實(shí)現(xiàn)導(dǎo)航菜單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • vue使用原生swiper代碼實(shí)例

    vue使用原生swiper代碼實(shí)例

    這篇文章主要介紹了vue使用原生swiper代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • vue啟動報(bào)錯(cuò)‘vue-cli-service serve‘問題及解決

    vue啟動報(bào)錯(cuò)‘vue-cli-service serve‘問題及解決

    這篇文章主要介紹了vue啟動報(bào)錯(cuò)‘vue-cli-service serve‘問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue項(xiàng)目中Toast字體過小,沒有邊距的解決方案

    vue項(xiàng)目中Toast字體過小,沒有邊距的解決方案

    這篇文章主要介紹了vue項(xiàng)目中Toast字體過小,沒有邊距的解決方案。具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue 粒子特效的示例代碼

    vue 粒子特效的示例代碼

    本篇文章主要介紹了vue 粒子特效的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • vue打包terser壓縮去除控制臺打印和斷點(diǎn)過程

    vue打包terser壓縮去除控制臺打印和斷點(diǎn)過程

    這篇文章主要介紹了vue打包terser壓縮去除控制臺打印和斷點(diǎn)過程,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • vue組件中添加@click失效問題及解決

    vue組件中添加@click失效問題及解決

    這篇文章主要介紹了vue組件中添加@click失效問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08

最新評論

临沂市| 岳西县| 天柱县| 绥化市| 长沙县| 桂林市| 铜川市| 鹤庆县| 武隆县| 洪湖市| 从江县| 鄂州市| 大邑县| 永定县| 沧源| 久治县| 南阳市| 轮台县| 江安县| 会昌县| SHOW| 栾川县| 西峡县| 合水县| 安乡县| 新闻| 那坡县| 许昌县| 五台县| 贵港市| 山西省| 尤溪县| 田东县| 盈江县| 吉木萨尔县| 大方县| 辽源市| 醴陵市| 襄樊市| 南充市| 九寨沟县|