From d8d77c79101576fdd5c75230bc6644b90f80be43 Mon Sep 17 00:00:00 2001 From: qiang Date: Wed, 9 Feb 2022 17:11:21 +0800 Subject: [PATCH] fix: image mode=heightFix (question/135134) --- packages/uni-components/src/vue/image/index.tsx | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/uni-components/src/vue/image/index.tsx b/packages/uni-components/src/vue/image/index.tsx index aad28a85d..45f94a76b 100644 --- a/packages/uni-components/src/vue/image/index.tsx +++ b/packages/uni-components/src/vue/image/index.tsx @@ -35,10 +35,15 @@ const props = { type ImageProps = ExtractPropTypes type ImageState = ReturnType type FixSize = ReturnType['fixSize'] +type FixMode = [ + 'offsetWidth' | 'offsetHeight', + 'height' | 'width', + (value: number, ratio: number) => number +] -const FIX_MODES = { - widthFix: ['offsetWidth', 'height'], - heightFix: ['offsetHeight', 'width'], +const FIX_MODES: Record = { + widthFix: ['offsetWidth', 'height', (value, ratio) => value / ratio], + heightFix: ['offsetHeight', 'width', (value, ratio) => value * ratio], } const IMAGE_MODES = { aspectFit: ['center center', 'contain'], @@ -209,10 +214,9 @@ function useImageSize( return } const rootEl = rootRef.value! - const value = rootEl[names[0] as 'offsetWidth' | 'offsetHeight'] + const value = rootEl[names[0]] if (value) { - rootEl.style[names[1] as 'height' | 'width'] = - fixNumber(value / ratio) + 'px' + rootEl.style[names[1]] = fixNumber(names[2](value, ratio)) + 'px' } if (__PLATFORM__ === 'app') { window.dispatchEvent(new CustomEvent('updateview')) -- GitLab