All files / src/components/f-scroll-view.vue bar.vue

100% Statements 19/19
50% Branches 3/6
100% Functions 3/3
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154      7x     7x             7x                                                                                                                                                                                                                                       7x 7x 490x 490x     490x                                    
<template>
    <div :class="$style.root" :direction="direction" @mousedown="onClickTrack">
        <div ref="thumb" :class="$style.thumb" @mousedown="onClickThumb" :style="renderThumbStyle({ move, size, bar })"></div>
    </div>
</template>
 
<script>
const BAR_MAP = {
    vertical: {
        offset: 'offsetHeight',
        scroll: 'scrollTop',
        scrollSize: 'scrollHeight',
        size: 'height',
        key: 'vertical',
        axis: 'Y',
        client: 'clientY',
        direction: 'top',
    },
    horizontal: {
        offset: 'offsetWidth',
        scroll: 'scrollLeft',
        scrollSize: 'scrollWidth',
        size: 'width',
        key: 'horizontal',
        axis: 'X',
        client: 'clientX',
        direction: 'left',
    },
};
 
export default {
    name: 'f-scroll-view-bar',
    props: {
        direction: { type: String, default: 'horizontal' },
        size: String,
        move: Number,
    },
    computed: {
        bar() {
            return BAR_MAP[this.direction];
        },
        wrapEl() {
            return this.$parent.$refs.wrap;
        },
    },
    methods: {
        onClickTrack(e) {
            const wrapEl = this.wrapEl;
            const thumbEl = this.$refs.thumb;
            const offset = Math.abs(e.target.getBoundingClientRect()[this.bar.direction] - e[this.bar.client]);
 
            const thumbHalf = (thumbEl[this.bar.offset] / 2);
            const thumbPositionPercentage = ((offset - thumbHalf) * 100 / this.$el[this.bar.offset]);
            wrapEl[this.bar.scroll] = (thumbPositionPercentage * wrapEl[this.bar.scrollSize] / 100);
        },
        onClickThumb(e) {
            if (e.ctrlKey || e.button === 2) {
                return;
            }
 
            this.startDrag(e);
            this[this.bar.axis] = (
                e.currentTarget[this.bar.offset] - (e[this.bar.client] - e.currentTarget.getBoundingClientRect()[this.bar.direction])
            );
        },
        startDrag(e) {
            e.stopImmediatePropagation();
            this.cursorDown = true;
            document.addEventListener('mousemove', this.mouseMoveDocumentHandler, false);
            document.addEventListener('mouseup', this.mouseUpDocumentHandler, false);
            // 拖拽滑动块时,此时禁止鼠标长按划过文本选中
            document.onselectstart = () => false;
        },
        mouseMoveDocumentHandler(e) {
            if (this.cursorDown === false) {
                return;
            }
 
            const wrapEl = this.wrapEl;
            const thumbEl = this.$refs.thumb;
 
            const prevPage = this[this.bar.axis];
            if (!prevPage) {
                return;
            }
            const offset = ((this.$el.getBoundingClientRect()[this.bar.direction] - e[this.bar.client]) * -1);
            const thumbClickPosition = (thumbEl[this.bar.offset] - prevPage);
            const thumbPositionPercentage = ((offset - thumbClickPosition) * 100 / this.$el[this.bar.offset]);
            wrapEl[this.bar.scroll] = (thumbPositionPercentage * wrapEl[this.bar.scrollSize] / 100);
        },
        mouseUpDocumentHandler(e) {
            this.cursorDown = false;
            this[this.bar.axis] = 0;
            document.removeEventListener('mousemove', this.mouseMoveDocumentHandler, false);
            document.onselectstart = null;
        },
        renderThumbStyle({ move, size, bar }) {
            const style = {};
            const translate = `translate${bar.axis}(${move}%)`;
 
            style[bar.size] = size;
            style.transform = translate;
            style.webkitTransform = translate;
 
            return style;
        },
    },
};
</script>
 
<style module>
.root {
    position: absolute;
    right: var(--scrollbar-size);
    bottom: var(--scrollbar-size);
    z-index: 1;
    border-radius: 3px;
    opacity: 0;
    transition: opacity .12s ease-out;
}
 
.thumb {
    position: relative;
    display: block;
    width: 0;
    height: 0;
    border-radius: inherit;
    background-color: var(--scrollbar-background);
    transition: background-color .3s;
}
 
.thumb:hover {
    background-color: var(--scrollbar-background-hover);
}
 
.root[direction="horizontal"] {
    height: var(--scrollbar-size);
    left: var(--scrollbar-size);
}
 
.root[direction="horizontal"] > div {
    height: 100%;
}
 
.root[direction="vertical"] {
    width: var(--scrollbar-size);
    top: var(--scrollbar-size);
}
 
.root[direction="vertical"] > div {
    width: 100%;
}
</style>