From bf251f851d5701b8b80c7436ae025193d0999ca8 Mon Sep 17 00:00:00 2001 From: younglei Date: Sat, 7 May 2022 16:49:59 +0800 Subject: [PATCH] fix: removeChild #121 --- packages/preload/loading.ts | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/preload/loading.ts b/packages/preload/loading.ts index a73a630..84d72df 100644 --- a/packages/preload/loading.ts +++ b/packages/preload/loading.ts @@ -1,5 +1,3 @@ - - /** * https://tobiasahlin.com/spinkit * https://connoratherton.com/loaders @@ -45,12 +43,25 @@ export function useLoading() { return { appendLoading() { - document.head.appendChild(oStyle) - document.body.appendChild(oDiv) + safe.append(document.head, oStyle) + safe.append(document.body, oDiv) }, removeLoading() { - document.head.removeChild(oStyle) - document.body.removeChild(oDiv) + safe.remove(document.head, oStyle) + safe.remove(document.body, oDiv) }, } } + +const safe = { + append(parent: HTMLElement, child: HTMLElement) { + if (!Array.from(parent.children).find(e => e === child)) { + return parent.appendChild(child) + } + }, + remove(parent: HTMLElement, child: HTMLElement) { + if (Array.from(parent.children).find(e => e === child)) { + return parent.removeChild(child) + } + }, +}