'admin-21.03.10:删除tagsView中的scroll.vue,优化tagsView中的滚动代码'

This commit is contained in:
lyt 2021-03-10 18:54:18 +08:00
parent f7134e3f31
commit f6ca3c0a6d
2 changed files with 73 additions and 90 deletions

View File

@ -1,74 +0,0 @@
<template>
<el-scrollbar ref="elScrollbarRef" @wheel.native.prevent="onHandleScroll">
<slot />
</el-scrollbar>
</template>
<script lang="ts">
import { getCurrentInstance, reactive, nextTick, toRefs } from "vue";
export default {
setup() {
const { proxy } = getCurrentInstance();
const state = reactive({
tagsArr: [],
});
const onHandleScroll = (e: object) => {
const eventDelta = e.wheelDelta || -e.deltaY * 40;
proxy.$refs.elScrollbarRef.$refs.wrap.scrollLeft =
proxy.$refs.elScrollbarRef.$refs.wrap.scrollLeft + eventDelta / 4;
};
const setScrollLeft = (tags: Array<object> = []) => {
nextTick(() => {
state.tagsArr = tags.value;
});
};
const moveToTarget = (currentTag: any) => {
const tagList = state.tagsArr;
let firstTag = null;
let lastTag = null;
if (tagList.length > 0) {
firstTag = tagList[0];
lastTag = tagList[tagList.length - 1];
}
if (firstTag === currentTag) {
proxy.$refs.elScrollbarRef.$refs.wrap.scrollLeft = 0;
} else if (lastTag === currentTag) {
proxy.$refs.elScrollbarRef.$refs.wrap.scrollLeft =
proxy.$refs.elScrollbarRef.$refs.wrap.scrollWidth -
proxy.$refs.elScrollbarRef.$refs.wrap.offsetWidth;
} else {
const currentIndex = tagList.findIndex((item) => item === currentTag);
const prevTag = tagList[currentIndex - 1];
const nextTag = tagList[currentIndex + 1];
const afterNextTagOffsetLeft =
nextTag.offsetLeft + nextTag.offsetWidth + 4;
const beforePrevTagOffsetLeft = prevTag.offsetLeft - 4;
if (
afterNextTagOffsetLeft >
proxy.$refs.elScrollbarRef.$refs.wrap.scrollLeft +
proxy.$refs.elScrollbarRef.$refs.wrap.offsetWidth
) {
proxy.$refs.elScrollbarRef.$refs.wrap.scrollLeft =
afterNextTagOffsetLeft -
proxy.$refs.elScrollbarRef.$refs.wrap.offsetWidth;
} else if (
beforePrevTagOffsetLeft <
proxy.$refs.elScrollbarRef.$refs.wrap.scrollLeft
) {
proxy.$refs.elScrollbarRef.$refs.wrap.scrollLeft = beforePrevTagOffsetLeft;
}
}
};
const updateScrollbar = () => {
proxy.$refs.elScrollbarRef.update();
};
return {
setScrollLeft,
onHandleScroll,
moveToTarget,
updateScrollbar,
...toRefs(state),
};
},
};
</script>

View File

@ -1,7 +1,7 @@
<template>
<div class="layout-navbars-tagsview" :class="{'layout-navbars-tagsview-shadow': getThemeConfig.layout === 'classic'}">
<Scroll ref="scrollRef">
<ul class="layout-navbars-tagsview-ul" :class="setTagsStyle">
<el-scrollbar ref="scrollbarRef" @wheel.native.prevent="onHandleScroll">
<ul class="layout-navbars-tagsview-ul" :class="setTagsStyle" ref="tagsUlRef">
<li v-for="(v,k) in tagsViewList" :key="k" class="layout-navbars-tagsview-ul-li" :data-name="v.name"
:class="{'is-active':isActive(v.path)}" @contextmenu.prevent="onContextmenu(v,$event)"
@click="onTagsClick(v,k)" :ref="el => { if (el) tagsRefs[k] = el }">
@ -18,7 +18,7 @@
@click="closeCurrentTagsView(v.path)"></i>
</li>
</ul>
</Scroll>
</el-scrollbar>
</div>
<Contextmenu :dropdown="dropdown" ref="contextmenuRef" @currentContextmenuClick="onCurrentContextmenuClick" />
</template>
@ -50,8 +50,9 @@ export default {
setup() {
const { proxy } = getCurrentInstance();
const tagsRefs = ref([]);
const scrollRef = ref();
const scrollbarRef = ref();
const contextmenuRef = ref();
const tagsUlRef = ref();
const store = useStore();
const route = useRoute();
const router = useRouter();
@ -103,10 +104,10 @@ export default {
});
addTagsView(route.path);
}
//
scrollRef.value.setScrollLeft(tagsRefs);
// (li)
getTagsRefsIndex(route.path);
moveToCurrentTag();
//
tagsViewmoveToCurrentTag();
};
// 1 tagsViewisHide tagsView
const addTagsView = (path: string) => {
@ -203,21 +204,75 @@ export default {
state.tagsRefsIndex = k;
router.push(v.path);
};
// tagsView +
const moveToCurrentTag = () => {
//
const updateScrollbar = () => {
proxy.$refs.scrollbarRef.update();
};
//
const onHandleScroll = (e: object) => {
proxy.$refs.scrollbarRef.$refs.wrap.scrollLeft += e.wheelDelta / 4;
};
// tagsView
const tagsViewmoveToCurrentTag = () => {
nextTick(() => {
scrollRef.value.moveToTarget(tagsRefs.value[state.tagsRefsIndex]);
scrollRef.value.updateScrollbar();
if (tagsRefs.value.length <= 0) return false;
// ul
let ulWidth = tagsUlRef.value.offsetWidth;
// li
let liDom = tagsRefs.value[state.tagsRefsIndex];
// li
let liIndex = state.tagsRefsIndex;
// ul li
let liLength = tagsRefs.value.length;
// li
let liFirst = tagsRefs.value[0];
// li
let liLast = tagsRefs.value[tagsRefs.value.length - 1];
//
let scrollRefs = proxy.$refs.scrollbarRef.$refs.wrap;
//
let scrollS = scrollRefs.scrollWidth;
//
let offsetW = scrollRefs.offsetWidth;
//
let scrollL = scrollRefs.scrollLeft;
// tags li dom
let liPrevTag = tagsRefs.value[state.tagsRefsIndex - 1];
// tags li dom
let liNextTag = tagsRefs.value[state.tagsRefsIndex + 1];
// tags li dom
let beforePrevL = "";
// tags li dom
let afterNextL = "";
if (liDom === liFirst) {
//
scrollRefs.scrollLeft = 0;
} else if (liDom === liLast) {
//
scrollRefs.scrollLeft = scrollS - offsetW;
} else {
// /
if (liIndex === 0) beforePrevL = liFirst.offsetLeft - 5;
else beforePrevL = liPrevTag.offsetLeft - 5;
if (liIndex === liLength)
afterNextL = liLast.offsetLeft + liLast.offsetWidth + 5;
else afterNextL = liNextTag.offsetLeft + liNextTag.offsetWidth + 5;
if (afterNextL > scrollL + offsetW) {
scrollRefs.scrollLeft = afterNextL - offsetW;
} else if (beforePrevL < scrollL) {
scrollRefs.scrollLeft = beforePrevL;
}
}
//
updateScrollbar();
});
};
// tagsView tagsView
const getTagsRefsIndex = (path: string) => {
if (state.tagsViewList.length > 0) {
const tagsRefsFindIndex = state.tagsViewList.findIndex(
state.tagsRefsIndex = state.tagsViewList.findIndex(
(item) => item.path === path
);
if (tagsRefsFindIndex <= 0) return false;
state.tagsRefsIndex = tagsRefsFindIndex;
}
};
// tagsView
@ -281,7 +336,7 @@ export default {
state.routePath = to.path;
addTagsView(to.path);
getTagsRefsIndex(to.path);
moveToCurrentTag();
tagsViewmoveToCurrentTag();
});
return {
isActive,
@ -290,7 +345,9 @@ export default {
onTagsClick,
tagsRefs,
contextmenuRef,
scrollRef,
scrollbarRef,
tagsUlRef,
onHandleScroll,
getThemeConfig,
setTagsStyle,
refreshCurrentTagsView,