-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(sticky-scroller): 增加 v-sticky-scroller 指令 (#4)
- Loading branch information
Showing
8 changed files
with
168 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Global styles for el-table with directive v-sticky-scroller | ||
@import 'gemini-scrollbar/gemini-scrollbar.css'; | ||
|
||
.el-table[data-sticky-scroll] { | ||
overflow: visible !important; | ||
|
||
.el-table__body-wrapper { | ||
&::-webkit-scrollbar { | ||
height: 0; | ||
} | ||
} | ||
|
||
&:hover, | ||
&:focus, | ||
&:active { | ||
.el-table-horizontal-scrollbar .gm-scrollbar { | ||
opacity: 1; | ||
} | ||
} | ||
|
||
.el-table-horizontal-scrollbar { | ||
position: sticky; | ||
height: 10px; | ||
width: 100%; | ||
bottom: 0; | ||
z-index: 4; | ||
transition: all 0.3s ease; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { checkElTable } from '@/utils' | ||
import Scroller from '@/utils/scroller' | ||
|
||
export default class StickyScroller { | ||
static name = 'StickyScroller' | ||
|
||
/** | ||
* Init directive config for Vue | ||
* @returns {Object} directive config | ||
*/ | ||
init() { | ||
return { | ||
inserted: (el, binding, vnode) => { | ||
checkElTable(binding, vnode) | ||
el.scroller = new Scroller(el, binding, vnode) | ||
}, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// reference https://github.com/noeldelgado/gemini-scrollbar | ||
import GeminiScrollbar from 'gemini-scrollbar' | ||
import { throttle } from 'throttle-debounce' | ||
|
||
const THROTTLE_TIME = 1000 / 60 | ||
|
||
export default class Scroller { | ||
constructor(el, binding, vnode) { | ||
this.#createScroller(el, binding, vnode) | ||
} | ||
|
||
/** | ||
* Create custom horizontal scrollbar for el-table | ||
* @param {Element} el el-table element | ||
* @param {Object} binding binding | ||
* @param {Object} vnode vnode | ||
*/ | ||
async #createScroller(el, binding, vnode) { | ||
// create scroller only once for the same el-table | ||
if (el.scroller) { | ||
return | ||
} | ||
// wait for el-table render | ||
await vnode.componentInstance.$nextTick() | ||
// check if el-table has horizontal scrollbar | ||
if (el.querySelector('.is-scrolling-none')) { | ||
return | ||
} | ||
|
||
el.dataset.stickyScroll = '' | ||
const tableBodyWrapperEl = el.querySelector('.el-table__body-wrapper') | ||
// create scroller | ||
const scroller = document.createElement('div') | ||
scroller.classList.add('el-table-horizontal-scrollbar') | ||
// set scroller content width to .el-table__body-wrapper table width | ||
const content = document.createElement('div') | ||
content.style.width = `${tableBodyWrapperEl.querySelector('table').offsetWidth}px` | ||
scroller.appendChild(content) | ||
el.appendChild(scroller) | ||
|
||
this.#initScrollBar(binding, scroller) | ||
this.#initScrollSyncHandler(tableBodyWrapperEl) | ||
} | ||
|
||
/** | ||
* Init scroll bar | ||
* @param {Object} binding binding | ||
* @param {Element} scroller scroller element | ||
*/ | ||
#initScrollBar(binding, scroller) { | ||
const { always = false } = binding.modifiers | ||
this.scrollbar = new GeminiScrollbar({ | ||
element: scroller, | ||
forceGemini: true, | ||
autoshow: !always, | ||
}).create() | ||
// remove vertical scrollbar | ||
this.scrollbar.element.querySelector('.gm-scrollbar.-vertical').remove() | ||
} | ||
|
||
/** | ||
* Init scroll sync handler | ||
* @param {Element} tableBodyWrapperEl .el-table__body-wrapper element | ||
*/ | ||
#initScrollSyncHandler(tableBodyWrapperEl) { | ||
const scrollViewEl = this.scrollbar.getViewElement() | ||
|
||
// sync tableBodyWrapperEl horizontal scroll to scrollView | ||
tableBodyWrapperEl.addEventListener('scroll', throttle(THROTTLE_TIME, () => { | ||
scrollViewEl.scrollLeft = tableBodyWrapperEl.scrollLeft | ||
})) | ||
// sync scrollViewEl horizontal scroll to tableBodyWrapperEl | ||
scrollViewEl.addEventListener('scroll', throttle(THROTTLE_TIME, () => { | ||
tableBodyWrapperEl.scrollLeft = scrollViewEl.scrollLeft | ||
})) | ||
} | ||
|
||
/** | ||
* Recalculate the viewbox and scrollbar dimensions | ||
*/ | ||
update() { | ||
this.scrollbar.update() | ||
} | ||
|
||
/** | ||
* Unbind the events and remove the custom scrollbar elements | ||
*/ | ||
destroy() { | ||
this.scrollbar.destroy() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters