loli position

This commit is contained in:
KUN1007 2023-05-17 13:41:58 +08:00
parent a6f5ca60ea
commit 1c310b9d42
6 changed files with 65 additions and 37 deletions

View file

@ -17,13 +17,7 @@ import { storeToRefs } from 'pinia'
const settingsStore = useSettingsPanelStore()
// 使
let {
showSettings,
showDarkMode,
showPageWidth,
showKUNGalgameBackground,
showFixedLoli,
} = storeToRefs(settingsStore)
let { showSettings } = storeToRefs(settingsStore)
//
interface topBar {

View file

@ -18,7 +18,15 @@ import { useFixedLoli } from '@/hooks/useFixedLoli'
const settingsStore = useSettingsPanelStore()
// 使 hook
const { kungalgameLoliStatus, setLoli, setLoliBtn, initLoli } = useFixedLoli()
const {
kungalgameLoliStatus,
kungalgameLoliPositionX,
kungalgameLoliPositionY,
setLoli,
setLoliX,
setLoliY,
initLoli,
} = useFixedLoli()
//
initLoli()
@ -30,20 +38,23 @@ const handleClose = () => {
let checked = kungalgameLoliStatus.value === 'true'
let flag = false
//
const handleClick = () => {
let str = 'false'
if (flag) {
setLoli(str)
str = 'false'
flag = !flag
if (!checked) {
setLoli('true')
} else {
setLoli(str)
str = 'true'
flag = !flag
setLoli('false')
}
}
//
let loliPositionX = parseFloat(kungalgameLoliPositionX.value)
let loliPositionY = parseFloat(kungalgameLoliPositionY.value)
//
const loliPosition = {
left: `${loliPositionX}px`,
top: `${loliPositionY}px`,
}
</script>
<template>
@ -97,7 +108,7 @@ const handleClick = () => {
<!-- 看板娘组件 -->
<!-- 此处使用 Teleport如果固定看板娘则将看板娘传送到根组件传送的状态使用全局 store 中的 showFixedLoli -->
<Teleport to="body" :disabled="kungalgameLoliStatus === 'false'">
<Loli class="loli" />
<Loli class="loli" :style="loliPosition" @get-position="getPosition" />
</Teleport>
<!-- 关闭面板 -->
<div class="close"><Icon @click="handleClose" icon="line-md:close" /></div>

View file

@ -39,6 +39,7 @@ const state = reactive({
const loliStyle: CSSProperties = {
top: `${state.translation.y}px`,
left: `${state.translation.x}px`,
width: 0,
}
const startDragLoli = (event: MouseEvent) => {
@ -101,8 +102,6 @@ onBeforeUnmount(() => {
position: fixed;
z-index: 9999;
//
// top: v-bind(loliPositionYPixel);
// left: v-bind(loliPositionXPixel);
}
.lass {
position: absolute;

View file

@ -4,31 +4,46 @@ import { ref, watchEffect } from 'vue'
import {
getLoliStatus,
setLoliStatus,
getLoliButtonStatus,
setLoliButtonStatus,
getLoliPositionX,
setLoliPositionX,
getLoliPositionY,
setLoliPositionY,
} from '@/utils/cache/local-storage'
// 初始化看板娘状态
const kungalgameLoliStatus = ref<string>(getLoliStatus() || 'false')
const kungalgameLoliBtnStatus = ref<string>(getLoliButtonStatus() || 'false')
const kungalgameLoliPositionX = ref<string>(getLoliPositionX() || '120')
const kungalgameLoliPositionY = ref<string>(getLoliPositionY() || '-250')
const setLoli = (status: string) => {
kungalgameLoliStatus.value = status
}
const setLoliBtn = (status: string) => {
kungalgameLoliBtnStatus.value = status
const setLoliX = (x: string) => {
kungalgameLoliPositionX.value = x
}
const setLoliY = (y: string) => {
kungalgameLoliPositionY.value = y
}
const initLoli = () => {
watchEffect(() => {
const loli = kungalgameLoliStatus.value
const loliBtn = kungalgameLoliStatus.value
setLoliStatus(loli)
setLoliButtonStatus(loliBtn)
setLoliStatus(kungalgameLoliStatus.value)
setLoliPositionX(kungalgameLoliPositionX.value)
setLoliPositionY(kungalgameLoliPositionY.value)
})
}
// 全局 hook
export function useFixedLoli() {
return { kungalgameLoliStatus, setLoli, setLoliBtn, initLoli }
return {
kungalgameLoliStatus,
kungalgameLoliPositionX,
kungalgameLoliPositionY,
setLoli,
setLoliX,
setLoliY,
initLoli,
}
}

View file

@ -12,8 +12,10 @@ class KUNCacheKey {
static BACKGROUND_PICTURE = `${KUN}-background-picture`
// 是否固定看板娘
static LOLI_STATUS = `${KUN}-loli-status`
// 看板娘点击按钮的选中状态
static LOLI_BTN_STATUS = `${KUN}-loli-btn-status`
// 看板娘定位 X
static LOLI_POSITION_X = `${KUN}-loli-position-x`
// 看板娘定位 Y
static LOLI_POSITION_Y = `${KUN}-loli-position-y`
}
export default KUNCacheKey

View file

@ -35,10 +35,17 @@ export const setLoliStatus = (loli: string) => {
// 看板娘的状态是一个布尔值,对应着固定还是不固定
localStorage.setItem(KUNCacheKey.LOLI_STATUS, loli)
}
export const getLoliButtonStatus = () => {
return localStorage.getItem(KUNCacheKey.LOLI_BTN_STATUS) as string
// 看板娘的水平位置
export const getLoliPositionX = () => {
return localStorage.getItem(KUNCacheKey.LOLI_POSITION_X) as string
}
export const setLoliButtonStatus = (loliBtn: string) => {
// 看板娘的状态是一个布尔值,对应着固定还是不固定
localStorage.setItem(KUNCacheKey.LOLI_BTN_STATUS, loliBtn)
export const setLoliPositionX = (loliX: string) => {
localStorage.setItem(KUNCacheKey.LOLI_POSITION_X, loliX)
}
// 看板娘的竖直位置
export const getLoliPositionY = () => {
return localStorage.getItem(KUNCacheKey.LOLI_POSITION_Y) as string
}
export const setLoliPositionY = (loliY: string) => {
localStorage.setItem(KUNCacheKey.LOLI_POSITION_Y, loliY)
}