00004.【CSS】演示before的简单案例4

需求

演示::before的简单案例

实现

 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
48
49
50
51
52
53
54
55
56
57
58
59

<template>
  <ul ref="ulRef">
    <li>Buy milk</li>
    <li>Take the dog for a walk</li>
    <li>Exercise</li>
    <li>Write code</li>
    <li>Play music</li>
    <li>Relax</li>
  </ul>
</template>

<script setup>

import {onMounted, ref} from "vue";

let ulRef = ref(null);

onMounted(() => {
  ulRef.value.addEventListener('click', function (ev) {
    if (ev.target.tagName === 'LI') {
      ev.target.classList.toggle('done')
    }
  })
})

// var list = document.querySelector('ul');

</script>

<style scoped>
li {
  list-style-type: none;
  position: relative;
  margin: 2px;
  padding: 0.5em 0.5em 0.5em 2em;
  background: lightgrey;
  font-family: sans-serif;
}

li.done {
  background: #CCFF99;
}

li.done::before {
  content: '';
  position: absolute;
  border-color: #009933;
  border-style: solid;
  border-width: 0 0.3em 0.25em 0;
  height: 1em;
  top: 1.3em;
  left: 0.6em;
  margin-top: -1em;
  transform: rotate(45deg);
  width: 0.5em;
}
</style>

小结

  1. list-style-typepositionmarginpaddingbackgroundfont-familybackgroundborder-colorborder-widthheighttopleftmargin-toptransformwidth属性不够熟悉。
  2. vue的ref标签和ref引用及OnMounted有点不太熟悉了。

参考

  1. ::before (:before)