본문 바로가기
vue

방향에 따른 툴팁 tooltip

by uni:D 2024. 8. 2.

방향에 다른 툴팁

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<template>
  <div class="tooltip-container" @mouseenter="showTooltip" @mouseleave="hideTooltip">
    <slot></slot>
    <div
      class="tooltip"
      :class="computedClass"
      v-if="visible"
    >
      {{ text }}
    </div>
  </div>
</template>
 
<script setup>
import { ref, computed } from 'vue'
 
// Props 정의
const props = defineProps({
  text: {
    type: String,
    required: true
  },
  className: {
    type: String,
    default'tooltip-top' // 기본값 설정
  }
})
 
const visible = ref(false)
 
// `className`을 직접 사용할 수 있도록 설정
const computedClass = computed(() => props.className)
 
// 툴팁 표시 및 숨김 함수
const showTooltip = () => {
  visible.value = true
}
 
const hideTooltip = () => {
  visible.value = false
}
</script>
 
<style scoped>
.tooltip-container {
  position: relative;
  display: inline-block;
}
.tooltip {
  position: absolute;
  background-color: #333;
  color: #fff;
  padding: 5px;
  border-radius: 3px;
  white-space: nowrap;
  z-index: 1000;
  font-size: 14px;
  visibility: hidden; /* 기본적으로 툴팁은 숨김 */
}
.tooltip-container:hover .tooltip {
  visibility: visible; /* 호버 시 툴팁 표시 */
}
/* 방향별 툴팁 위치 스타일 */
.tooltip-top {
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
}
.tooltip-bottom {
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
}
.tooltip-left {
  right: 100%;
  top: 50%;
  transform: translateY(-50%);
}
.tooltip-right {
  left: 100%;
  top: 50%;
  transform: translateY(-50%);
}
</style>
cs
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
<template>
  <div class="container">
    <!-- 다양한 스타일과 방향을 가진 툴팁 예제 -->
    <div class="tooltip-example">
      <Tooltip text="Default Tooltip" position="top" styleType="default">
        <button class="tooltip-button">Default Top Tooltip</button>
      </Tooltip>
    </div>
    <div class="tooltip-example">
      <Tooltip text="Info Tooltip" position="bottom" styleType="info">
        <button class="tooltip-button">Info Bottom Tooltip</button>
      </Tooltip>
    </div>
    <div class="tooltip-example">
      <Tooltip text="Warning Tooltip" position="left" styleType="warning">
        <button class="tooltip-button">Warning Left Tooltip</button>
      </Tooltip>
    </div>
    <div class="tooltip-example">
      <Tooltip text="Default Tooltip" position="right" styleType="default">
        <button class="tooltip-button">Default Right Tooltip</button>
      </Tooltip>
    </div>
  </div>
</template>
 
<script setup>
import Tooltip from './Tooltip.vue'
</script>
 
<style scoped>
.container {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
  justify-content: center;
  margin-top: 20px;
}
.tooltip-example {
  position: relative;
}
.tooltip-button {
  padding: 10px 20px;
  font-size: 16px;
  border: none;
  border-radius: 4px;
  background-color: #007bff;
  color: #fff;
  cursor: pointer;
}
.tooltip-button:hover {
  background-color: #0056b3;
}
</style>
cs

'vue' 카테고리의 다른 글

:class  (0) 2024.08.04
레이어팝업  (0) 2024.08.02
클릭시 상단 push  (1) 2024.08.02
vuetify3 클릭시 아이콘변경 show 구조  (0) 2024.08.02
v-model  (0) 2024.08.01