ToolTip.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 } 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 = ref({
  43. x: 0,
  44. y: 0
  45. })
  46. // const tooltipContainer = document.getElementsByClassName('wq-tooltip')
  47. const tooltipStyle = computed(() => {
  48. return {
  49. transform: `translate3d(${tooltipPosition.value.x}px,${tooltipPosition.value.y}px,0)`
  50. }
  51. })
  52. return {
  53. tooltipShow,
  54. showTip,
  55. hiddenTip,
  56. setContent,
  57. tooltipPosition,
  58. tooltipStyle,
  59. text,
  60. placements,
  61. }
  62. }
  63. }
  64. </script>
  65. <style lang="scss" scoped>
  66. // tooltip
  67. .wq-tooltip {
  68. padding: 5px 16px;
  69. font-size: .875rem;
  70. line-height: 1.6;
  71. min-width: 10px;
  72. word-wrap: break-word;
  73. position: fixed;
  74. left: 0;
  75. top: 0;
  76. pointer-events: none;
  77. background: rgb(66, 66, 66);
  78. color: rgb(238, 238, 238);
  79. border-radius: 4px;
  80. font-size: .875rem;
  81. line-height: 1.6;
  82. display: block;
  83. z-index: 2000;
  84. opacity: 1;
  85. width: auto;
  86. transition-property: opacity,width;
  87. }
  88. // 小箭头
  89. .wq-tooltip-arrow {
  90. position: absolute;
  91. width: 0;
  92. height: 0;
  93. border-width: 8px;
  94. border-style: solid;
  95. }
  96. // 小箭头如果在左侧
  97. .wq-tooltip-arrow.left {
  98. border-color: transparent transparent transparent #303133;
  99. right: -15px;
  100. top: 50%;
  101. transform: translate3d(0, -50%, 0);
  102. }
  103. // 小箭头如果在下侧
  104. .wq-tooltip-arrow.bottom {
  105. top: -15px;
  106. border-color: transparent transparent #303133 transparent;
  107. left: 50%;
  108. transform: translate3d(-50%, 0, 0);
  109. }
  110. // 小箭头如果在右侧
  111. .wq-tooltip-arrow.right {
  112. left: -15px;
  113. top: 50%;
  114. transform: translate3d(0, -50%, 0);
  115. border-color: transparent #303133 transparent transparent;
  116. }
  117. // 小箭头如果在上侧
  118. .wq-tooltip-arrow.top {
  119. bottom: -15px;
  120. border-color: #303133 transparent transparent transparent;
  121. left: 50%;
  122. transform: translate3d(-50%, 0, 0);
  123. }
  124. /* 动画 */
  125. .tooltip-enter-from,
  126. .tooltip-leave-to {
  127. opacity: 0;
  128. transition: opacity .3s ease;
  129. }
  130. .tooltip-leave-from,
  131. .tooltip-enter-to {
  132. transition: opacity .1s ease;
  133. }
  134. </style>