cwg-tooltip.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <view class="cwg-tooltip" @mouseenter="showTooltip" @mouseleave="hideTooltip">
  3. <!-- 触发元素插槽 -->
  4. <slot></slot>
  5. <!-- 弹出层 -->
  6. <view v-if="content || $slots.content" class="cwg-tooltip-popup"
  7. :class="[`placement-${placement}`, { show: isVisible }]" :style="popupStyle">
  8. <!-- 内容插槽 -->
  9. <slot name="content">
  10. {{ content }}
  11. </slot>
  12. </view>
  13. </view>
  14. </template>
  15. <script setup>
  16. import { ref, watch, computed } from 'vue'
  17. const props = defineProps({
  18. content: {
  19. type: String,
  20. default: ''
  21. },
  22. placement: {
  23. type: String,
  24. default: 'bottom',
  25. validator: (value) => ['left', 'right', 'top', 'bottom'].includes(value)
  26. },
  27. manual: {
  28. type: Boolean,
  29. default: false
  30. },
  31. visible: {
  32. type: Boolean,
  33. default: false
  34. },
  35. // 添加间距控制
  36. spacing: {
  37. type: String,
  38. default: '12rpx'
  39. },
  40. // 添加最大宽度控制
  41. maxWidth: {
  42. type: String,
  43. default: ''
  44. }
  45. })
  46. const emit = defineEmits(['update:visible', 'show', 'hide'])
  47. // 内部显示状态
  48. const isVisible = ref(props.manual ? props.visible : false)
  49. // 监听visible变化
  50. watch(() => props.visible, (val) => {
  51. if (props.manual) {
  52. isVisible.value = val
  53. }
  54. })
  55. // 弹窗样式
  56. const popupStyle = computed(() => {
  57. const style = {}
  58. if (props.maxWidth) {
  59. style.maxWidth = props.maxWidth
  60. }
  61. return style
  62. })
  63. // 显示弹窗
  64. const showTooltip = () => {
  65. if (!props.manual) {
  66. isVisible.value = true
  67. emit('show')
  68. }
  69. }
  70. // 隐藏弹窗
  71. const hideTooltip = () => {
  72. if (!props.manual) {
  73. isVisible.value = false
  74. emit('hide')
  75. }
  76. }
  77. // 切换显示状态(手动模式)
  78. const toggle = () => {
  79. if (props.manual) {
  80. isVisible.value = !isVisible.value
  81. emit('update:visible', isVisible.value)
  82. }
  83. }
  84. // 显示(手动模式)
  85. const show = () => {
  86. if (props.manual) {
  87. isVisible.value = true
  88. emit('update:visible', true)
  89. emit('show')
  90. }
  91. }
  92. // 隐藏(手动模式)
  93. const hide = () => {
  94. if (props.manual) {
  95. isVisible.value = false
  96. emit('update:visible', false)
  97. emit('hide')
  98. }
  99. }
  100. // 暴露方法给父组件
  101. defineExpose({
  102. toggle,
  103. show,
  104. hide
  105. })
  106. </script>
  107. <style scoped lang="scss">
  108. .cwg-tooltip {
  109. position: relative;
  110. display: inline-block;
  111. cursor: pointer;
  112. }
  113. /* 弹窗基础样式 */
  114. .cwg-tooltip-popup {
  115. position: absolute;
  116. z-index: 1000;
  117. text-align: center;
  118. line-height: 1.5;
  119. word-break: break-word;
  120. white-space: nowrap;
  121. display: none;
  122. padding: px2rpx(16) px2rpx(24);
  123. font-size: px2rpx(16);
  124. color: var(--bs-heading-color);
  125. /* 你的样式要求 */
  126. background-color: var(--color-slate-100) !important;
  127. border-radius: px2rpx(8);
  128. /* 让箭头可见 */
  129. overflow: visible;
  130. /* 默认箭头样式(右侧定位) */
  131. &::after {
  132. content: "";
  133. position: absolute;
  134. top: 50%;
  135. left: -12px;
  136. transform: translateY(-50%);
  137. width: 0;
  138. height: 0;
  139. border-width: px2rpx(6);
  140. border-style: solid;
  141. border-color: transparent var(--color-slate-100) transparent transparent;
  142. pointer-events: none;
  143. z-index: 100001;
  144. }
  145. }
  146. /* 显示状态 */
  147. .cwg-tooltip-popup.show {
  148. display: block;
  149. }
  150. /* 不同位置的定位 - 箭头方向也相应调整 */
  151. /* 左侧定位 */
  152. .cwg-tooltip-popup.placement-left {
  153. top: 50%;
  154. right: 100%;
  155. transform: translateY(-50%);
  156. margin-right: v-bind('props.spacing');
  157. &::after {
  158. left: auto;
  159. right: -12px;
  160. border-color: transparent transparent transparent var(--color-slate-100);
  161. }
  162. }
  163. /* 右侧定位 */
  164. .cwg-tooltip-popup.placement-right {
  165. top: 50%;
  166. left: 100%;
  167. transform: translateY(-50%);
  168. margin-left: v-bind('props.spacing');
  169. &::after {
  170. left: -12px;
  171. border-color: transparent var(--color-slate-100) transparent transparent;
  172. }
  173. }
  174. /* 上方定位 */
  175. .cwg-tooltip-popup.placement-top {
  176. bottom: 100%;
  177. left: 50%;
  178. transform: translateX(-50%);
  179. margin-bottom: v-bind('props.spacing');
  180. &::after {
  181. top: auto;
  182. bottom: -12px;
  183. left: 50%;
  184. transform: translateX(-50%);
  185. border-color: var(--color-slate-100) transparent transparent transparent;
  186. }
  187. }
  188. /* 下方定位 */
  189. .cwg-tooltip-popup.placement-bottom {
  190. top: 100%;
  191. left: 50%;
  192. transform: translateX(-50%);
  193. margin-top: v-bind('props.spacing');
  194. &::after {
  195. top: -12px;
  196. left: 50%;
  197. transform: translateX(-50%);
  198. border-color: transparent transparent var(--color-slate-100) transparent;
  199. }
  200. }
  201. /* 长内容换行 - 当设置了最大宽度时 */
  202. .cwg-tooltip-popup[style*="max-width"] {
  203. white-space: normal;
  204. }
  205. </style>