每條游來游去的小魚,都是正在瀏覽這一頁的訪客,有一條是你自己 ( ´ ▽ ` )ノ 覺得干擾閱讀的話,可以把小魚關掉。
調皮的按鈕
button停用時會越跑越遠的按鈕。ᕕ( ゚ ∀。)ᕗ
越想嚕他,就跑得越遠,和你家的貓一樣。(._.`)
| 名稱 | 描述 |
|---|---|
| 向量計算 | 處理方向、加速度、速度等等數學運算 |
| Pointer 事件 | 偵測滑鼠或觸控點移動、點擊、懸停等等事件,取得座標、目標等等資訊 |
| CSS 動畫 | 基於 CSS transition 和 animation 實現 |
| IntersectionObserver | 偵測元素是否進入或離開視窗 |
當按鈕狀態為 disabled 並觸發 hover、click、key enter 事件時,按鈕會開始亂跑
<template> <div class="example-wrap w-full flex flex-col gap-4"> <div class="example-ctrl flex flex-col gap-4"> <base-checkbox v-model="disabled" :label="t('disableButton')" />
<base-input v-model="text" :placeholder="t('inputPlaceholder')" class="w-full" /> </div>
<div class="flex justify-center"> <btn-naughty :label="t('naughtyButtonLabel')" class="font-bold" :disabled="disabled" z-index="30" /> </div> </div></template>
<script setup lang="ts">import { ref } from 'vue'import { useI18n } from 'vue-i18n'import BaseCheckbox from '../../base-checkbox.vue'import BaseInput from '../../base-input.vue'import BtnNaughty from '../btn-naughty.vue'
const text = ref('')const disabled = ref(true)
const { t } = useI18n()</script>指定 maxDistanceMultiple 可以設定最大移動距離倍數(自身寬高倍數),若按鈕跑出指定範圍或超出畫面,都會自動回歸原點
<template> <div class="w-full flex flex-col items-center justify-center gap-4 example-wrap"> <base-input v-model="maxMultiple" type="number" outlined :label="t('倍數')" />
<btn-naughty :label="t('按鈕')" class="font-bold" disabled :max-distance-multiple="maxMultiple" z-index="30" /> </div></template>
<script setup lang="ts">import { ref } from 'vue'import { useI18n } from 'vue-i18n'import BaseInput from '../../base-input.vue'import BtnNaughty from '../btn-naughty.vue'
const { t } = useI18n()
const maxMultiple = ref(3)</script>呼叫 method
Section titled “呼叫 method”也支援直接呼叫 method 產生動作
<template> <div class="w-full flex flex-col items-center justify-center gap-4 example-wrap"> <div class="example-ctrl w-full flex gap-4"> <base-btn :label="t('move')" @click="run" /> <base-btn :label="t('back')" @click="back" /> </div>
<btn-naughty ref="btn" :label="t('buttonLabel')" z-index="30" /> </div></template>
<script setup lang="ts">import { ref } from 'vue'import { useI18n } from 'vue-i18n'import BaseBtn from '../../base-btn.vue'import BtnNaughty from '../btn-naughty.vue'
const { t } = useI18n()
const btn = ref<InstanceType<typeof BtnNaughty>>()function run() { btn.value?.run()}function back() { btn.value?.back()}</script>可以使用 default slot 自定義按鈕外觀
<template> <div class="flex justify-center example-wrap"> <btn-naughty disabled z-index="30" > <div class="custom-button"> {{ t('customButton') }} </div> </btn-naughty> </div></template>
<script setup lang="ts">import { useI18n } from 'vue-i18n'import BtnNaughty from '../btn-naughty.vue'
const { t } = useI18n()</script>
<style scoped lang="sass">.custom-button background: #ff8345 color: white padding: 0.5rem 1.5rem border-radius: 999rem</style>你說拓印能不能自定義?可以啦,哪次不可以了。
使用 rubbing slot,自訂按鈕拓印內容
啪!跑了
自定義按鈕
<template> <div class="flex justify-center example-wrap"> <btn-naughty disabled z-index="30" > <template #rubbing> <div class="rubbing"> {{ t('啪!跑了') }} </div> </template>
<template #default> <div class="btn"> {{ t('自定義按鈕') }} </div> </template> </btn-naughty> </div></template>
<script setup lang="ts">import { useI18n } from 'vue-i18n'import BtnNaughty from '../btn-naughty.vue'
const { t } = useI18n()</script>
<style scoped lang="sass">.btn padding: 0.5rem 1.5rem background: #26A69A border-radius: 999rem font-weight: bold color: white cursor: pointer
.rubbing padding: 0.5rem 1.5rem background: light-dark(#FEFEFE, #333) border-radius: 999rem border: 1px dashed light-dark(#777, #999) text-align: center</style>Slot Props
Section titled “Slot Props”使用 slot prop 可以玩出更多花樣
😗
點此中獎
<template> <div class="flex justify-center example-wrap"> <btn-naughty disabled z-index="30" > <template #rubbing="{ isRunning }"> <div class="rubbing"> {{ isRunning ? '😜' : '😗' }} </div> </template>
<template #default="{ isRunning }"> <div class="btn"> {{ isRunning ? t('點不到咧') : t('點此中獎') }} </div> </template> </btn-naughty> </div></template>
<script setup lang="ts">import { useI18n } from 'vue-i18n'import BtnNaughty from '../btn-naughty.vue'
const { t } = useI18n()</script>
<style scoped lang="sass">.btn padding: 0.5rem 1.5rem background: #26A69A border-radius: 999rem font-weight: bold color: white font-size: 1.25rem cursor: pointer
.rubbing padding: 0.5rem 1.5rem background: light-dark(#FEFEFE, #333) border-radius: 999rem border: 1px dashed light-dark(#777, #999) text-align: center font-size: 1.25rem</style>沒填完就沒得按。(╯•̀ὤ•́)╯
請完成表單
<template> <div class="relative w-full flex justify-center example-wrap"> <div class="max-w-[20rem] flex flex-col gap-4"> <base-input v-model="form.username" :label="t('帳號 *')" class="w-full" />
<base-input v-model="form.password" type="password" :label="t('密碼 *')" class="w-full" />
<div class="mt-3 flex justify-center"> <btn-naughty :label="t('送出表單')" :disabled z-index="30" @click="handleSubmit" > <template #rubbing> <div class="rubbing"> {{ t('請完成表單') }} </div> </template> </btn-naughty> </div> </div>
<transition name="opacity"> <div v-if="isSubmitted" class="absolute inset-0 z-[40] flex flex-col items-center justify-center gap-6 rounded-xl bg-slate-600 bg-opacity-90 text-white" @click="reset" > <span class="text-xl tracking-wide"> {{ t('表單已送出!(*´∀`)~♥') }} </span>
<span class="cursor-pointer text-xs"> {{ t('點一下再來一次') }} </span> </div> </transition> </div></template>
<script setup lang="ts">import { computed, ref } from 'vue'import { useI18n } from 'vue-i18n'import BaseInput from '../../base-input.vue'import BtnNaughty from '../btn-naughty.vue'
const { t } = useI18n()
const form = ref({ username: '', password: '',})const disabled = computed(() => { return form.value.username === '' || form.value.password === ''})
const isSubmitted = ref(false)function handleSubmit() { if (disabled.value) { return } isSubmitted.value = true}
function reset() { isSubmitted.value = false
form.value = { username: '', password: '', }}</script>
<style lang="sass" scoped>.rubbing padding: 0.75rem color: light-dark(#ff7530, #ff9660) opacity: 0.8 border: 1px dashed light-dark(#ff7530, #ff9660) border-radius: 0.2rem white-space: nowrap text-align: center
.opacity-enter-active, .opacity-leave-active transition-duration: 0.4s.opacity-enter-from, .opacity-leave-to opacity: 0 !important</style>滑鼠碰觸按鈕時,計算滑鼠位置到按鈕中心的單位向量,並以此向量為基準,移動一個按鈕尺寸的距離。
如果按鈕移動到畫面外,則會自動返回原點,使用 IntersectionObserver 實作。
詳細說明請見此文章。
interface Props { /** 按鈕內文字 */ label?: string; /** 是否停用 */ disabled?: boolean; /** 同 CSS z-index */ zIndex?: number | string; /** 最大移動距離,為按鈕尺寸倍數 */ maxDistanceMultiple?: number; /** 同 html tabindex */ tabindex?: number | string;}const emit = defineEmits<{ (e: 'click'): void; /** 開始移動時 */ (e: 'run'): void; /** 開始返回時 */ (e: 'back'): void;}>()Methods
Section titled “Methods”interface Expose { /** 移動 */ run: () => void; /** 返回原點 */ back: () => void; /** 是否正在移動 */ isRunning: Ref<boolean>;}defineSlots<{ /** 按鈕 */ default?: (props: { isRunning: boolean }) => unknown; /** 拓印 */ rubbing?: (props: { isRunning: boolean }) => unknown;}>()