ToolTip.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <!-- 指示 -->
  3. <transition name="el-fade-in-linear">
  4. <div v-show="tooltipShow" :style="tooltipStyle" class="wq-tooltip"
  5. >
  6. <span class="wq-tooltip-text" v-text="text"></span>
  7. <!-- 箭头 -->
  8. <!-- <div :class="[
  9. {'left':placements === 'left'},
  10. {'bottom':placements==='bottom'},
  11. {'right':placements==='right'},
  12. {'top':placements==='top'}]"
  13. class="wq-tooltip-arrow"
  14. >
  15. </div> -->
  16. </div>
  17. </transition>
  18. </template>
  19. <script>
  20. import {ref, computed, reactive } from 'vue'
  21. export default {
  22. setup() {
  23. // 显示弹框
  24. const tooltipShow = ref(false);
  25. // 提示内容
  26. const text = ref()
  27. // 方向
  28. const placements = ref('left')
  29. // 显示
  30. function showTip() {
  31. tooltipShow.value = true
  32. }
  33. //设置提示内容
  34. function setContent(content) {
  35. text.value = content
  36. }
  37. //隐藏
  38. function hiddenTip() {
  39. tooltipShow.value = false
  40. }
  41. // 位置
  42. const tooltipPosition = reactive({
  43. x: 0,
  44. y: 0
  45. })
  46. const tooltipStyle = computed(() => {
  47. return {
  48. transform: `translate3d(${tooltipPosition.value.x}px,${tooltipPosition.value.y}px,0)`
  49. }
  50. })
  51. return {
  52. tooltipShow,
  53. showTip,
  54. hiddenTip,
  55. setContent,
  56. tooltipPosition,
  57. tooltipStyle,
  58. text,
  59. placements,
  60. }
  61. }
  62. }
  63. </script>
  64. <style lang="scss" scoped>
  65. // tooltip
  66. .wq-tooltip {
  67. padding: 5px 16px;
  68. font-size: .875rem;
  69. line-height: 1.6;
  70. min-width: 10px;
  71. word-wrap: break-word;
  72. position: fixed;
  73. left: 0;
  74. top: 0;
  75. pointer-events: none;
  76. background: rgb(66, 66, 66);
  77. color: rgb(238, 238, 238);
  78. border-radius: 4px;
  79. font-size: .875rem;
  80. line-height: 1.6;
  81. display: block;
  82. z-index: 2000;
  83. opacity: 1;
  84. width: auto;
  85. transition-property: opacity,width;
  86. }
  87. // 小箭头
  88. .wq-tooltip-arrow {
  89. position: absolute;
  90. width: 0;
  91. height: 0;
  92. border-width: 8px;
  93. border-style: solid;
  94. }
  95. // 小箭头如果在左侧
  96. .wq-tooltip-arrow.left {
  97. border-color: transparent transparent transparent #303133;
  98. right: -15px;
  99. top: 50%;
  100. transform: translate3d(0, -50%, 0);
  101. }
  102. // 小箭头如果在下侧
  103. .wq-tooltip-arrow.bottom {
  104. top: -15px;
  105. border-color: transparent transparent #303133 transparent;
  106. left: 50%;
  107. transform: translate3d(-50%, 0, 0);
  108. }
  109. // 小箭头如果在右侧
  110. .wq-tooltip-arrow.right {
  111. left: -15px;
  112. top: 50%;
  113. transform: translate3d(0, -50%, 0);
  114. border-color: transparent #303133 transparent transparent;
  115. }
  116. // 小箭头如果在上侧
  117. .wq-tooltip-arrow.top {
  118. bottom: -15px;
  119. border-color: #303133 transparent transparent transparent;
  120. left: 50%;
  121. transform: translate3d(-50%, 0, 0);
  122. }
  123. /* 动画 */
  124. .tooltip-enter-from,
  125. .tooltip-leave-to {
  126. opacity: 0;
  127. transition: opacity .3s ease;
  128. }
  129. .tooltip-leave-from,
  130. .tooltip-enter-to {
  131. transition: opacity .1s ease;
  132. }
  133. </style>