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

Element-ui之ElScrollBar組件滾動條的使用方法

 更新時間:2018年09月14日 10:35:49   作者:wywar  
這篇文章主要介紹了Element-ui之ElScrollBar組件滾動條的使用方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

在使用vue + element-ui 搭建后臺管理頁面的時候,做了一個頭部、側(cè)欄、面包屑固定的布局,導(dǎo)航欄和主要內(nèi)容區(qū)域當(dāng)內(nèi)容超出時自動滾動。

使用的原因:

原來是采用優(yōu)化瀏覽器樣式的方式,對滾動條進(jìn)行樣式調(diào)整。但這個方法并不兼容火狐瀏覽器,在火狐訪問時依然是瀏覽器默認(rèn)的滾動條樣式。

.sidebar {
 position: fixed;
 border-right: 1px solid rgba(0,0,0,.07);
 overflow-y: auto;
 position: absolute;
 top: 0;
 bottom: 0;
 left: 0;
 transition: transform .25s ease-out;
 width: 300px;
 z-index: 3;
}
.sidebar::-webkit-scrollbar {
 width: 4px
}

.sidebar::-webkit-scrollbar-thumb {
 background: transparent;
 border-radius: 4px
}

.sidebar:hover::-webkit-scrollbar-thumb {
 background: hsla(0,0%,53%,.4)
}

.sidebar:hover::-webkit-scrollbar-track {
 background: hsla(0,0%,53%,.1)
}

靈感來源

在翻看 element-ui官網(wǎng)的文檔時,發(fā)現(xiàn)其左側(cè)導(dǎo)航和右邊的內(nèi)容超出屏幕時,滾動條的樣式比較小巧,通過瀏覽器審查工具查看,發(fā)現(xiàn)它是使用了el-scrollbar的樣式,跟element-ui的組件樣式命名一致。但文檔中并沒有關(guān)于這個 scrollbar組件的使用文檔,搜索一番得知這是一個隱藏組件,官方在 github 的 issues 中表示不會寫在文檔中,需要用的自己看源碼進(jìn)行調(diào)用。

最終實現(xiàn)效果

實現(xiàn)步驟

一、閱讀源碼

通過閱讀源碼,scrollbar組件暴露了 native, wrapStyle, wrapClass, viewClass, viewStyle, noresize, tag 這7個 props屬性

props: {
 native: Boolean, // 是否使用本地,設(shè)為true則不會啟用element-ui自定義的滾動條
 wrapStyle: {}, // 包裹層自定義樣式
 wrapClass: {}, // 包裹層自定義樣式類
 viewClass: {}, // 可滾動部分自定義樣式類
 viewStyle: {}, // 可滾動部分自定義樣式
 noresize: Boolean, // 如果 container 尺寸不會發(fā)生變化,最好設(shè)置它可以優(yōu)化性能
 tag: { // 生成的標(biāo)簽類型,默認(rèn)使用 `div`標(biāo)簽包裹
  type: String,
  default: 'div'
 }
}

二、在頁面中使用 el-scrollbar組件

<template>
 <div>
  <el-scrollbar :native="false" wrapStyle="" wrapClass="" viewClass="" viewStyle="" noresize="false" tag="section">
   <div>
    <p v-for="(item, index) in 200" :key="index">{{index}} 這里是一些文本。</p>
   </div>
  <el-scrollbar>
 </div>
</template>

以上代碼就是對 el-scrollbar 的使用了,屬性不需要用的就不用寫。

源碼

源碼在node_modules 目錄下的 element-ui/packages/scrollbar

模塊入口index.js,從main導(dǎo)入 scrollbar并提供一個安裝方法注冊成全局組件

import Scrollbar from './src/main';

/* istanbul ignore next */
Scrollbar.install = function(Vue) {
 Vue.component(Scrollbar.name, Scrollbar);
};

export default Scrollbar;

src/main.js 源碼

// reference https://github.com/noeldelgado/gemini-scrollbar/blob/master/index.js

import { addResizeListener, removeResizeListener } from 'element-ui/src/utils/resize-event';
import scrollbarWidth from 'element-ui/src/utils/scrollbar-width';
import { toObject } from 'element-ui/src/utils/util';
import Bar from './bar';

/* istanbul ignore next */
export default {
 name: 'ElScrollbar',

 components: { Bar },

 props: {
 native: Boolean,
 wrapStyle: {},
 wrapClass: {},
 viewClass: {},
 viewStyle: {},
 noresize: Boolean, // 如果 container 尺寸不會發(fā)生變化,最好設(shè)置它可以優(yōu)化性能
 tag: {
  type: String,
  default: 'div'
 }
 },

 data() {
 return {
  sizeWidth: '0',
  sizeHeight: '0',
  moveX: 0,
  moveY: 0
 };
 },

 computed: {
 wrap() {
  return this.$refs.wrap;
 }
 },

 render(h) {
 let gutter = scrollbarWidth();
 let style = this.wrapStyle;

 if (gutter) {
  const gutterWith = `-${gutter}px`;
  const gutterStyle = `margin-bottom: ${gutterWith}; margin-right: ${gutterWith};`;

  if (Array.isArray(this.wrapStyle)) {
  style = toObject(this.wrapStyle);
  style.marginRight = style.marginBottom = gutterWith;
  } else if (typeof this.wrapStyle === 'string') {
  style += gutterStyle;
  } else {
  style = gutterStyle;
  }
 }
 const view = h(this.tag, {
  class: ['el-scrollbar__view', this.viewClass],
  style: this.viewStyle,
  ref: 'resize'
 }, this.$slots.default);
 const wrap = (
  <div
  ref="wrap"
  style={ style }
  onScroll={ this.handleScroll }
  class={ [this.wrapClass, 'el-scrollbar__wrap', gutter ? '' : 'el-scrollbar__wrap--hidden-default'] }>
  { [view] }
  </div>
 );
 let nodes;

 if (!this.native) {
  nodes = ([
  wrap,
  <Bar
   move={ this.moveX }
   size={ this.sizeWidth }></Bar>,
  <Bar
   vertical
   move={ this.moveY }
   size={ this.sizeHeight }></Bar>
  ]);
 } else {
  nodes = ([
  <div
   ref="wrap"
   class={ [this.wrapClass, 'el-scrollbar__wrap'] }
   style={ style }>
   { [view] }
  </div>
  ]);
 }
 return h('div', { class: 'el-scrollbar' }, nodes);
 },

 methods: {
 handleScroll() {
  const wrap = this.wrap;

  this.moveY = ((wrap.scrollTop * 100) / wrap.clientHeight);
  this.moveX = ((wrap.scrollLeft * 100) / wrap.clientWidth);
 },

 update() {
  let heightPercentage, widthPercentage;
  const wrap = this.wrap;
  if (!wrap) return;

  heightPercentage = (wrap.clientHeight * 100 / wrap.scrollHeight);
  widthPercentage = (wrap.clientWidth * 100 / wrap.scrollWidth);

  this.sizeHeight = (heightPercentage < 100) ? (heightPercentage + '%') : '';
  this.sizeWidth = (widthPercentage < 100) ? (widthPercentage + '%') : '';
 }
 },

 mounted() {
 if (this.native) return;
 this.$nextTick(this.update);
 !this.noresize && addResizeListener(this.$refs.resize, this.update);
 },

 beforeDestroy() {
 if (this.native) return;
 !this.noresize && removeResizeListener(this.$refs.resize, this.update);
 }
};

示例

<div style="height: 100vh;">
 <!-- 注意需要給 el-scrollbar 設(shè)置高度,判斷是否滾動是看它的height判斷的 -->
 <el-scrollbar style="height: 100%;"> <!-- 滾動條 -->
  <div style="height: 500px;width: 100%;background: red;"></div>
  <div style="height: 500px;width: 100%;background: yellowgreen;"></div>
  <div style="height: 500px;width: 100%;background: blueviolet;"></div>
 </el-scrollbar><!-- /滾動條 -->
</div>

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

相關(guān)文章

  • VsCode里的Vue模板的實現(xiàn)

    VsCode里的Vue模板的實現(xiàn)

    這篇文章主要介紹了VsCode里的Vue模板的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Vue+jsPlumb實現(xiàn)連線效果(支持滑動連線和點擊連線)

    Vue+jsPlumb實現(xiàn)連線效果(支持滑動連線和點擊連線)

    jsPlumb 是一個比較強(qiáng)大的繪圖組件,它提供了一種方法,主要用于連接網(wǎng)頁上的元素。本文將利用jsPlumb實現(xiàn)連線效果,同時支持滑動連線和點擊連線,感興趣的可以了解一下
    2023-01-01
  • Vue計算屬性實現(xiàn)成績單

    Vue計算屬性實現(xiàn)成績單

    這篇文章主要為大家詳細(xì)介紹了Vue計算屬性實現(xiàn)成績單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • 詳解關(guān)于Vue單元測試的幾個坑

    詳解關(guān)于Vue單元測試的幾個坑

    這篇文章主要介紹了關(guān)于Vue單元測試的幾個坑,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • 關(guān)于vant折疊面板默認(rèn)展開問題

    關(guān)于vant折疊面板默認(rèn)展開問題

    這篇文章主要介紹了關(guān)于vant折疊面板默認(rèn)展開問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • vue單頁面在微信下只能分享落地頁的解決方案

    vue單頁面在微信下只能分享落地頁的解決方案

    這篇文章主要介紹了vue單頁面在微信下只能分享落地頁的解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-04-04
  • vue3前端實現(xiàn)全屏顯示及元素垂直填滿頁面效果

    vue3前端實現(xiàn)全屏顯示及元素垂直填滿頁面效果

    這篇文章主要給大家介紹了關(guān)于vue3前端實現(xiàn)全屏顯示及元素垂直填滿頁面效果的相關(guān)資料,文中還給大家介紹了vue3實現(xiàn)某一個元素全屏之后就黑屏了的解決辦法,需要的朋友可以參考下
    2024-02-02
  • vue+watermark-dom實現(xiàn)頁面水印效果(示例代碼)

    vue+watermark-dom實現(xiàn)頁面水印效果(示例代碼)

    watermark.js 是基于 DOM 對象實現(xiàn)的 BS 系統(tǒng)的水印,確保系統(tǒng)保密性,安全性,降低數(shù)據(jù)泄密風(fēng)險,簡單輕量,支持多屬性配置,本文將通過 vue 結(jié)合 watermark-dom 庫,教大家實現(xiàn)簡單而有效的頁面水印效果,感興趣的朋友跟隨小編一起看看吧
    2024-07-07
  • vue.js移動端app之上拉加載以及下拉刷新實戰(zhàn)

    vue.js移動端app之上拉加載以及下拉刷新實戰(zhàn)

    這篇文章主要介紹了vue.js移動端app之上拉加載以及下拉刷新實戰(zhàn),非常具有實用價值,需要的朋友可以參考下
    2017-09-09
  • vue中使用v-if,v-else來設(shè)置css樣式的步驟

    vue中使用v-if,v-else來設(shè)置css樣式的步驟

    我們在使用vue項目開發(fā)時,v-if是使用的非常多的,在這里我們談?wù)勅绾问褂胿-i來綁定修改css樣式,使用的主要是雙向數(shù)據(jù)綁定,即通過改變他的狀態(tài)來改變他的樣式,這篇文章主要介紹了vue中如何使用v-if,v-else來設(shè)置css樣式,需要的朋友可以參考下
    2023-03-03

最新評論

鄂伦春自治旗| 白山市| 醴陵市| 夹江县| 道孚县| 江阴市| 连州市| 于田县| 亚东县| 普宁市| 密云县| 星子县| 洮南市| 武宁县| 甘泉县| 大足县| 扬州市| 盈江县| 应城市| 呼图壁县| 东丽区| 鄱阳县| 宽甸| 永州市| 双江| 万盛区| 尚志市| 六枝特区| 衢州市| 定日县| 昆明市| 麻城市| 恩施市| 淮阳县| 荣昌县| 兴宁市| 双牌县| 威宁| 深州市| 隆尧县| 肇东市|