LightBulbSwitch.vue
1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<script lang="ts" setup>
import on from '../assets/light-bulb-on.svg';
import off from '../assets/light-bulb-off.svg';
const props = defineProps<{
value?: boolean;
}>();
const emit = defineEmits(['update:value', 'change']);
const handleChange = (event: Event) => {
const _value = (event.target as HTMLInputElement).checked;
emit('update:value', _value);
emit('change', _value);
};
</script>
<template>
<label class="light-bulb-switch">
<input :value="props.value" :checked="props.value" type="checkbox" @change="handleChange" />
<div class="light-bulb__on"> <img :src="on" alt="开" /> </div>
<div class="light-bulb__off"> <img :src="off" alt="关" /> </div>
</label>
</template>
<style scoped lang="less">
.light-bulb-switch {
input[type='checkbox'] {
display: none;
}
input:checked ~ .light-bulb__off {
display: none;
}
input:not(:checked) ~ .light-bulb__on {
display: none;
}
.light-bulb__on > img,
.light-bulb__off > img {
width: 48px;
height: 48px;
cursor: pointer;
}
}
</style>