1
0
mirror of https://gitee.com/vant-contrib/vant.git synced 2025-04-06 03:57:59 +08:00

fix(Badge): fix badge offset of different position ()

* fix(Badge): fix badge offset of different position

* perf: improve code
This commit is contained in:
Gavin 2022-12-24 12:54:40 +08:00 committed by GitHub
parent e39f0c67d1
commit 78847b8375
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -71,6 +71,9 @@ export default defineComponent({
} }
}; };
const getOffsetWithMinusString = (val: string) =>
val.startsWith('-') ? val.replace('-', '') : `-${val}`;
const style = computed(() => { const style = computed(() => {
const style: CSSProperties = { const style: CSSProperties = {
background: props.color, background: props.color,
@ -78,13 +81,23 @@ export default defineComponent({
if (props.offset) { if (props.offset) {
const [x, y] = props.offset; const [x, y] = props.offset;
const { position } = props;
const offsetY = position.indexOf('top') > -1 ? 'top' : 'bottom';
const offsetX = position.indexOf('left') > -1 ? 'left' : 'right';
if (slots.default) { if (slots.default) {
style.top = addUnit(y); if (typeof y === 'number') {
style[offsetY] = addUnit(offsetY === 'top' ? y : -y);
} else {
style[offsetY] =
offsetY === 'top' ? addUnit(y) : getOffsetWithMinusString(y);
}
if (typeof x === 'number') { if (typeof x === 'number') {
style.right = addUnit(-x); style[offsetX] = addUnit(offsetX === 'left' ? x : -x);
} else { } else {
style.right = x.startsWith('-') ? x.replace('-', '') : `-${x}`; style[offsetX] =
offsetX === 'left' ? addUnit(x) : getOffsetWithMinusString(x);
} }
} else { } else {
style.marginTop = addUnit(y); style.marginTop = addUnit(y);