tooltip.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <script>
  2. import {ref, computed} from 'vue'
  3. export default {
  4. setup(){
  5. // 显示弹框
  6. const tooltipShow = ref(false);
  7. // 提示内容
  8. const text = ref()
  9. // 方向
  10. const placements = ref('left')
  11. // 显示
  12. function showTip(){
  13. tooltipShow.value = true
  14. }
  15. function hiddenTip(){
  16. tooltipShow.value = false
  17. }
  18. // 位置
  19. const tooltipPostiton = ref({
  20. x: 0,
  21. y: 0
  22. })
  23. const tooltipStyle = computed(()=>{
  24. return {
  25. transform: `translate3d(${tooltipPostiton.value.x}px,${tooltipPostiton.value.y}px,0)`
  26. }
  27. })
  28. return {
  29. tooltipShow,
  30. showTip,
  31. hiddenTip,
  32. tooltipPostiton,
  33. tooltipStyle,
  34. text,
  35. placements,
  36. }
  37. }
  38. }
  39. </script>
  40. <template>
  41. <!-- 指示 -->
  42. <transition name="tooltip">
  43. <div class="zc-tooltip" v-show="tooltipShow" :style="tooltipStyle"
  44. >
  45. <span class="zc-tooltip-text" v-html="text"></span>
  46. <div class="zc-tooltip-arrow" :class="[{'left':placements=='left'},
  47. {'bottom':placements=='bottom'},
  48. {'right':placements=='right'},
  49. {'top':placements=='top'}]"></div>
  50. </div>
  51. </transition>
  52. </template>
  53. <style scoped>
  54. .zc-tooltip {
  55. padding: 10px;
  56. font-size: 12px;
  57. line-height: 1.2;
  58. min-width: 10px;
  59. word-wrap: break-word;
  60. position: fixed;
  61. left: 0;
  62. top: 0;
  63. background: #303133;
  64. color: #fff;
  65. z-index: 1000;
  66. display: inline-block;
  67. border-radius: 8px;
  68. font-weight: 500;
  69. pointer-events: none;
  70. }
  71. .zc-tooltip-arrow {
  72. position: absolute;
  73. width: 0;
  74. height: 0;
  75. border-width: 8px;
  76. border-style: solid;
  77. }
  78. .zc-tooltip-arrow.left {
  79. border-color: transparent transparent transparent #303133;
  80. right: -15px;
  81. top: 50%;
  82. transform: translate3d(0, -50%, 0);
  83. }
  84. .zc-tooltip-arrow.bottom {
  85. top: -15px;
  86. border-color: transparent transparent #303133 transparent;
  87. left: 50%;
  88. transform: translate3d(-50%, 0, 0);
  89. }
  90. .zc-tooltip-arrow.right {
  91. left: -15px;
  92. top: 50%;
  93. transform: translate3d(0, -50%, 0);
  94. border-color: transparent #303133 transparent transparent;
  95. }
  96. .zc-tooltip-arrow.top {
  97. bottom: -15px;
  98. border-color: #303133 transparent transparent transparent;
  99. left: 50%;
  100. transform: translate3d(-50%, 0, 0);
  101. }
  102. /* 动画 */
  103. .tooltip-enter-from,
  104. .tooltip-leave-to {
  105. opacity: 0;
  106. transition: opacity .3s ease;
  107. }
  108. .tooltip-leave-from,
  109. .tooltip-enter-to {
  110. transition: opacity .1s ease;
  111. }
  112. </style>