uni-tooltip.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <template>
  2. <view class="uni-tooltip">
  3. <slot></slot>
  4. <view v-if="content || $slots.content" class="uni-tooltip-popup" :style="initPlacement">
  5. <slot name="content">
  6. {{content}}
  7. </slot>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. /**
  13. * Tooltip 提示文字
  14. * @description 常用于展示鼠标 hover 时的提示信息。
  15. * @tutorial https://uniapp.dcloud.io/component/uniui/uni-tooltip
  16. * @property {String} content 弹出层显示的内容
  17. * @property {String} placement出现位置, 目前支持:left right top bottom
  18. */
  19. export default {
  20. name: "uni-tooltip",
  21. data() {
  22. return {
  23. };
  24. },
  25. methods: {
  26. },
  27. computed: {
  28. initPlacement() {
  29. let style = {};
  30. switch (this.placement) {
  31. case 'left':
  32. style = {
  33. top: '50%',
  34. transform: 'translateY(-50%)',
  35. right: '100%',
  36. "margin-right": '10rpx',
  37. }
  38. break;
  39. case 'right':
  40. style = {
  41. top: '50%',
  42. transform: 'translateY(-50%)',
  43. left: '100%',
  44. "margin-left": '10rpx',
  45. }
  46. break;
  47. case 'top':
  48. style = {
  49. bottom: '100%',
  50. transform: 'translateX(-50%)',
  51. left: '50%',
  52. "margin-bottom": '10rpx',
  53. }
  54. break;
  55. case 'bottom':
  56. style = {
  57. top: '100%',
  58. transform: 'translateX(-50%)',
  59. left: '50%',
  60. "margin-top": '10rpx',
  61. }
  62. break;
  63. }
  64. return style;
  65. }
  66. },
  67. props: {
  68. content: {
  69. type: String,
  70. default: ''
  71. },
  72. placement: {
  73. type: String,
  74. default: 'left'
  75. },
  76. }
  77. }
  78. </script>
  79. <style>
  80. .uni-tooltip {
  81. position: relative;
  82. cursor: pointer;
  83. display: inline-block;
  84. }
  85. .uni-tooltip-popup {
  86. z-index: 1;
  87. display: none;
  88. position: absolute;
  89. background-color: #333;
  90. border-radius: 8px;
  91. color: #fff;
  92. font-size: 12px;
  93. text-align: left;
  94. line-height: 16px;
  95. padding: 12px;
  96. overflow: auto;
  97. }
  98. .uni-tooltip:hover .uni-tooltip-popup {
  99. display: block;
  100. }
  101. </style>