mirror of
https://gitee.com/element-plus/element-plus.git
synced 2024-12-02 19:28:14 +08:00
fix(components): [scrollbar] multiple rendering (#15822)
* fix:The namespace of the showOverflowTooltip is invalid. * fix(components): [scrollbar] multiple rendering * fix(components): [scrollbar] multiple rendering * fix(components): [scrollbar] multiple rendering * Update packages/components/scrollbar/src/bar.vue Co-authored-by: kooriookami <38392315+kooriookami@users.noreply.github.com> * test(components): [scrollbar] adjusting style attributes --------- Co-authored-by: kooriookami <38392315+kooriookami@users.noreply.github.com>
This commit is contained in:
parent
f33979d228
commit
5e3fa20774
@ -30,11 +30,11 @@ describe('ScrollBar', () => {
|
|||||||
|
|
||||||
await makeScroll(scrollDom, 'scrollTop', 100)
|
await makeScroll(scrollDom, 'scrollTop', 100)
|
||||||
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
||||||
'height: 80px; transform: translateY(50%);'
|
'transform: translateY(50%); height: 80px;'
|
||||||
)
|
)
|
||||||
await makeScroll(scrollDom, 'scrollTop', 300)
|
await makeScroll(scrollDom, 'scrollTop', 300)
|
||||||
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
||||||
'height: 80px; transform: translateY(150%);'
|
'transform: translateY(150%); height: 80px;'
|
||||||
)
|
)
|
||||||
offsetHeightRestore()
|
offsetHeightRestore()
|
||||||
scrollHeightRestore()
|
scrollHeightRestore()
|
||||||
@ -64,11 +64,11 @@ describe('ScrollBar', () => {
|
|||||||
|
|
||||||
await makeScroll(scrollDom, 'scrollLeft', 100)
|
await makeScroll(scrollDom, 'scrollLeft', 100)
|
||||||
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
|
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
|
||||||
'width: 80px; transform: translateX(50%);'
|
'transform: translateX(50%); width: 80px;'
|
||||||
)
|
)
|
||||||
await makeScroll(scrollDom, 'scrollLeft', 300)
|
await makeScroll(scrollDom, 'scrollLeft', 300)
|
||||||
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
|
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
|
||||||
'width: 80px; transform: translateX(150%);'
|
'transform: translateX(150%); width: 80px;'
|
||||||
)
|
)
|
||||||
offsetWidthRestore()
|
offsetWidthRestore()
|
||||||
scrollWidthRestore()
|
scrollWidthRestore()
|
||||||
@ -111,18 +111,18 @@ describe('ScrollBar', () => {
|
|||||||
await makeScroll(scrollDom, 'scrollTop', 100)
|
await makeScroll(scrollDom, 'scrollTop', 100)
|
||||||
await makeScroll(scrollDom, 'scrollLeft', 100)
|
await makeScroll(scrollDom, 'scrollLeft', 100)
|
||||||
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
||||||
'height: 80px; transform: translateY(50%);'
|
'transform: translateY(50%); height: 80px;'
|
||||||
)
|
)
|
||||||
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
|
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
|
||||||
'width: 80px; transform: translateX(50%);'
|
'transform: translateX(50%); width: 80px;'
|
||||||
)
|
)
|
||||||
await makeScroll(scrollDom, 'scrollTop', 300)
|
await makeScroll(scrollDom, 'scrollTop', 300)
|
||||||
await makeScroll(scrollDom, 'scrollLeft', 300)
|
await makeScroll(scrollDom, 'scrollLeft', 300)
|
||||||
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
||||||
'height: 80px; transform: translateY(150%);'
|
'transform: translateY(150%); height: 80px;'
|
||||||
)
|
)
|
||||||
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
|
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
|
||||||
'width: 80px; transform: translateX(150%);'
|
'transform: translateX(150%); width: 80px;'
|
||||||
)
|
)
|
||||||
|
|
||||||
offsetHeightRestore()
|
offsetHeightRestore()
|
||||||
@ -221,10 +221,10 @@ describe('ScrollBar', () => {
|
|||||||
scrollbar.setScrollLeft(100)
|
scrollbar.setScrollLeft(100)
|
||||||
await nextTick()
|
await nextTick()
|
||||||
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
||||||
'height: 80px; transform: translateY(0%);'
|
'transform: translateY(0%); height: 80px;'
|
||||||
)
|
)
|
||||||
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
|
expect(wrapper.find('.is-horizontal div').attributes('style')).toContain(
|
||||||
'width: 80px; transform: translateX(0%);'
|
'transform: translateX(0%); width: 80px;'
|
||||||
)
|
)
|
||||||
|
|
||||||
offsetHeightRestore()
|
offsetHeightRestore()
|
||||||
@ -257,7 +257,7 @@ describe('ScrollBar', () => {
|
|||||||
|
|
||||||
await makeScroll(scrollDom, 'scrollTop', 0)
|
await makeScroll(scrollDom, 'scrollTop', 0)
|
||||||
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
expect(wrapper.find('.is-vertical div').attributes('style')).toContain(
|
||||||
'height: 20px; transform: translateY(0%);'
|
'transform: translateY(0%); height: 20px;'
|
||||||
)
|
)
|
||||||
offsetHeightRestore()
|
offsetHeightRestore()
|
||||||
scrollHeightRestore()
|
scrollHeightRestore()
|
||||||
|
@ -7,15 +7,9 @@ export const barProps = buildProps({
|
|||||||
type: Boolean,
|
type: Boolean,
|
||||||
default: true,
|
default: true,
|
||||||
},
|
},
|
||||||
width: String,
|
minSize: {
|
||||||
height: String,
|
|
||||||
ratioX: {
|
|
||||||
type: Number,
|
type: Number,
|
||||||
default: 1,
|
required: true,
|
||||||
},
|
|
||||||
ratioY: {
|
|
||||||
type: Number,
|
|
||||||
default: 1,
|
|
||||||
},
|
},
|
||||||
} as const)
|
} as const)
|
||||||
export type BarProps = ExtractPropTypes<typeof barProps>
|
export type BarProps = ExtractPropTypes<typeof barProps>
|
||||||
|
@ -1,35 +1,67 @@
|
|||||||
<template>
|
<template>
|
||||||
<thumb :move="moveX" :ratio="ratioX" :size="width" :always="always" />
|
<thumb :move="moveX" :ratio="ratioX" :size="sizeWidth" :always="always" />
|
||||||
<thumb
|
<thumb
|
||||||
:move="moveY"
|
:move="moveY"
|
||||||
:ratio="ratioY"
|
:ratio="ratioY"
|
||||||
:size="height"
|
:size="sizeHeight"
|
||||||
vertical
|
vertical
|
||||||
:always="always"
|
:always="always"
|
||||||
/>
|
/>
|
||||||
</template>
|
</template>
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { ref } from 'vue'
|
import { inject, ref } from 'vue'
|
||||||
import { GAP } from './util'
|
import { GAP } from './util'
|
||||||
import Thumb from './thumb.vue'
|
import Thumb from './thumb.vue'
|
||||||
import { barProps } from './bar'
|
import { barProps } from './bar'
|
||||||
|
import { scrollbarContextKey } from './constants'
|
||||||
|
|
||||||
const props = defineProps(barProps)
|
const props = defineProps(barProps)
|
||||||
|
|
||||||
|
const scrollbar = inject(scrollbarContextKey)
|
||||||
|
|
||||||
const moveX = ref(0)
|
const moveX = ref(0)
|
||||||
const moveY = ref(0)
|
const moveY = ref(0)
|
||||||
|
const sizeWidth = ref('')
|
||||||
|
const sizeHeight = ref('')
|
||||||
|
const ratioY = ref(1)
|
||||||
|
const ratioX = ref(1)
|
||||||
|
|
||||||
const handleScroll = (wrap: HTMLDivElement) => {
|
const handleScroll = (wrap: HTMLDivElement) => {
|
||||||
if (wrap) {
|
if (wrap) {
|
||||||
const offsetHeight = wrap.offsetHeight - GAP
|
const offsetHeight = wrap.offsetHeight - GAP
|
||||||
const offsetWidth = wrap.offsetWidth - GAP
|
const offsetWidth = wrap.offsetWidth - GAP
|
||||||
|
|
||||||
moveY.value = ((wrap.scrollTop * 100) / offsetHeight) * props.ratioY
|
moveY.value = ((wrap.scrollTop * 100) / offsetHeight) * ratioY.value
|
||||||
moveX.value = ((wrap.scrollLeft * 100) / offsetWidth) * props.ratioX
|
moveX.value = ((wrap.scrollLeft * 100) / offsetWidth) * ratioX.value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const update = () => {
|
||||||
|
const wrap = scrollbar?.wrapElement
|
||||||
|
if (!wrap) return
|
||||||
|
const offsetHeight = wrap.offsetHeight - GAP
|
||||||
|
const offsetWidth = wrap.offsetWidth - GAP
|
||||||
|
|
||||||
|
const originalHeight = offsetHeight ** 2 / wrap.scrollHeight
|
||||||
|
const originalWidth = offsetWidth ** 2 / wrap.scrollWidth
|
||||||
|
const height = Math.max(originalHeight, props.minSize)
|
||||||
|
const width = Math.max(originalWidth, props.minSize)
|
||||||
|
|
||||||
|
ratioY.value =
|
||||||
|
originalHeight /
|
||||||
|
(offsetHeight - originalHeight) /
|
||||||
|
(height / (offsetHeight - height))
|
||||||
|
ratioX.value =
|
||||||
|
originalWidth /
|
||||||
|
(offsetWidth - originalWidth) /
|
||||||
|
(width / (offsetWidth - width))
|
||||||
|
|
||||||
|
sizeHeight.value = height + GAP < offsetHeight ? `${height}px` : ''
|
||||||
|
sizeWidth.value = width + GAP < offsetWidth ? `${width}px` : ''
|
||||||
|
}
|
||||||
|
|
||||||
defineExpose({
|
defineExpose({
|
||||||
handleScroll,
|
handleScroll,
|
||||||
|
update,
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
@ -20,14 +20,7 @@
|
|||||||
</component>
|
</component>
|
||||||
</div>
|
</div>
|
||||||
<template v-if="!native">
|
<template v-if="!native">
|
||||||
<bar
|
<bar ref="barRef" :always="always" :min-size="minSize" />
|
||||||
ref="barRef"
|
|
||||||
:height="sizeHeight"
|
|
||||||
:width="sizeWidth"
|
|
||||||
:always="always"
|
|
||||||
:ratio-x="ratioX"
|
|
||||||
:ratio-y="ratioY"
|
|
||||||
/>
|
|
||||||
</template>
|
</template>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@ -45,7 +38,6 @@ import {
|
|||||||
import { useEventListener, useResizeObserver } from '@vueuse/core'
|
import { useEventListener, useResizeObserver } from '@vueuse/core'
|
||||||
import { addUnit, debugWarn, isNumber, isObject } from '@element-plus/utils'
|
import { addUnit, debugWarn, isNumber, isObject } from '@element-plus/utils'
|
||||||
import { useNamespace } from '@element-plus/hooks'
|
import { useNamespace } from '@element-plus/hooks'
|
||||||
import { GAP } from './util'
|
|
||||||
import Bar from './bar.vue'
|
import Bar from './bar.vue'
|
||||||
import { scrollbarContextKey } from './constants'
|
import { scrollbarContextKey } from './constants'
|
||||||
import { scrollbarEmits, scrollbarProps } from './scrollbar'
|
import { scrollbarEmits, scrollbarProps } from './scrollbar'
|
||||||
@ -69,12 +61,7 @@ let stopResizeListener: (() => void) | undefined = undefined
|
|||||||
const scrollbarRef = ref<HTMLDivElement>()
|
const scrollbarRef = ref<HTMLDivElement>()
|
||||||
const wrapRef = ref<HTMLDivElement>()
|
const wrapRef = ref<HTMLDivElement>()
|
||||||
const resizeRef = ref<HTMLElement>()
|
const resizeRef = ref<HTMLElement>()
|
||||||
|
|
||||||
const sizeWidth = ref('0')
|
|
||||||
const sizeHeight = ref('0')
|
|
||||||
const barRef = ref<BarInstance>()
|
const barRef = ref<BarInstance>()
|
||||||
const ratioY = ref(1)
|
|
||||||
const ratioX = ref(1)
|
|
||||||
|
|
||||||
const wrapStyle = computed<StyleValue>(() => {
|
const wrapStyle = computed<StyleValue>(() => {
|
||||||
const style: CSSProperties = {}
|
const style: CSSProperties = {}
|
||||||
@ -135,26 +122,7 @@ const setScrollLeft = (value: number) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const update = () => {
|
const update = () => {
|
||||||
if (!wrapRef.value) return
|
barRef.value?.update()
|
||||||
const offsetHeight = wrapRef.value.offsetHeight - GAP
|
|
||||||
const offsetWidth = wrapRef.value.offsetWidth - GAP
|
|
||||||
|
|
||||||
const originalHeight = offsetHeight ** 2 / wrapRef.value.scrollHeight
|
|
||||||
const originalWidth = offsetWidth ** 2 / wrapRef.value.scrollWidth
|
|
||||||
const height = Math.max(originalHeight, props.minSize)
|
|
||||||
const width = Math.max(originalWidth, props.minSize)
|
|
||||||
|
|
||||||
ratioY.value =
|
|
||||||
originalHeight /
|
|
||||||
(offsetHeight - originalHeight) /
|
|
||||||
(height / (offsetHeight - height))
|
|
||||||
ratioX.value =
|
|
||||||
originalWidth /
|
|
||||||
(offsetWidth - originalWidth) /
|
|
||||||
(width / (offsetWidth - width))
|
|
||||||
|
|
||||||
sizeHeight.value = height + GAP < offsetHeight ? `${height}px` : ''
|
|
||||||
sizeWidth.value = width + GAP < offsetWidth ? `${width}px` : ''
|
|
||||||
}
|
}
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
|
Loading…
Reference in New Issue
Block a user