|
| 1 | +<!-- 留着备用, 万一可以变得很可爱呢 --> |
| 2 | + |
| 3 | +<script lang="ts"> |
| 4 | + import {onMount, onDestroy} from 'svelte'; |
| 5 | + import {currentPage} from '../store'; |
| 6 | + import {wikis} from '../config'; |
| 7 | + import gsap from 'gsap'; |
| 8 | + import FishFlag from '../assets/flag.svg?raw'; |
| 9 | +
|
| 10 | + let visible = true; |
| 11 | + let showModal = false; |
| 12 | + let lines: string[] = []; |
| 13 | + let ticker: gsap.core.Tween; |
| 14 | + let textEl: HTMLDivElement; |
| 15 | +
|
| 16 | + $: wikiText = wikis[$currentPage]?.trim() || ''; |
| 17 | + $: lines = wikiText ? wikiText.split('\n').map((l) => l.trim()) : []; |
| 18 | +
|
| 19 | + $: if (wikiText && textEl) { |
| 20 | + gsap.killTweensOf(textEl); |
| 21 | + scroll(); |
| 22 | + } |
| 23 | +
|
| 24 | + $: if (!currentPage) { |
| 25 | + visible = false; |
| 26 | + showModal = false; |
| 27 | + } |
| 28 | +
|
| 29 | + async function scroll() { |
| 30 | + if (!textEl) return; |
| 31 | + gsap.killTweensOf(textEl); |
| 32 | +
|
| 33 | + await gsap.delayedCall(0.02, () => {}); |
| 34 | +
|
| 35 | + const totalWidth = textEl.scrollWidth; |
| 36 | + const singleCopyWidth = totalWidth / 2; |
| 37 | + const viewportWidth = textEl.parentElement?.clientWidth || 0; |
| 38 | +
|
| 39 | + if (singleCopyWidth <= viewportWidth) { |
| 40 | + gsap.set(textEl, {x: 0}); |
| 41 | + return; |
| 42 | + } |
| 43 | +
|
| 44 | + gsap.set(textEl, {x: 0}); |
| 45 | +
|
| 46 | + ticker = gsap.to(textEl, { |
| 47 | + x: -singleCopyWidth, |
| 48 | + duration: singleCopyWidth / 60, |
| 49 | + ease: 'linear', |
| 50 | + repeat: -1, |
| 51 | + force3D: true, |
| 52 | + roundProps: 'x', |
| 53 | + }); |
| 54 | + } |
| 55 | +
|
| 56 | + onMount(() => { |
| 57 | + document.addEventListener('click', handleClick); |
| 58 | + setTimeout(scroll, 50); |
| 59 | +
|
| 60 | + setTimeout(() => { |
| 61 | + document.querySelectorAll<HTMLElement>('.koi-flag').forEach((el, i) => { |
| 62 | + gsap.to(el, { |
| 63 | + y: gsap.utils.random(-12, 12), |
| 64 | + x: gsap.utils.random(-6, 6), |
| 65 | + duration: gsap.utils.random(0.8, 1.5), |
| 66 | + yoyo: true, |
| 67 | + repeat: -1, |
| 68 | + ease: 'sine.inOut', |
| 69 | + delay: i * 0.1, |
| 70 | + }); |
| 71 | + }); |
| 72 | + }, 100); |
| 73 | +
|
| 74 | + return () => { |
| 75 | + document.removeEventListener('click', handleClick); |
| 76 | + }; |
| 77 | + }); |
| 78 | +
|
| 79 | + onDestroy(() => { |
| 80 | + if (ticker) ticker.kill(); |
| 81 | + }); |
| 82 | +
|
| 83 | + function handleClick(event: MouseEvent) { |
| 84 | + const isTextInteraction = window.getSelection()?.toString(); |
| 85 | + if (showModal && (event.target as HTMLElement).closest('#wiki-modal') && !isTextInteraction) { |
| 86 | + showModal = false; |
| 87 | + } |
| 88 | + } |
| 89 | +</script> |
| 90 | + |
| 91 | +{#if visible && wikiText} |
| 92 | + <div class="fixed top-0 left-0 w-full text-[var(--textColor)] backdrop-blur-sm z-10 h-12 flex items-center"> |
| 93 | + <div class="relative overflow-visible flex-1 h-full -ml-2 -mr-2"> |
| 94 | + <div |
| 95 | + bind:this={textEl} |
| 96 | + class="absolute whitespace-nowrap text-sm lg:text-base will-change-transform top-0 left-12 h-full flex items-center gap-2" |
| 97 | + > |
| 98 | + {#each Array(2) as _} |
| 99 | + {#each lines as line} |
| 100 | + <div class="koi-flag relative flex items-center justify-center w-48 h-[40px] mx-2"> |
| 101 | + {@html FishFlag} |
| 102 | + <span class="absolute text-xs left-4 text-center top-[14px]">{line}</span> |
| 103 | + </div> |
| 104 | + {/each} |
| 105 | + {/each} |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + </div> |
| 109 | +{/if} |
0 commit comments