uni-tooltip.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. computed: {
  27. initPlacement() {
  28. let style = {};
  29. switch (this.placement) {
  30. case 'left':
  31. style = {
  32. top: '50%',
  33. transform: 'translateY(-50%)',
  34. right: '100%',
  35. "margin-right": '10rpx',
  36. }
  37. break;
  38. case 'right':
  39. style = {
  40. top: '50%',
  41. transform: 'translateY(-50%)',
  42. left: '100%',
  43. "margin-left": '10rpx',
  44. }
  45. break;
  46. case 'top':
  47. style = {
  48. bottom: '100%',
  49. transform: 'translateX(-50%)',
  50. left: '50%',
  51. "margin-bottom": '10rpx',
  52. }
  53. break;
  54. case 'bottom':
  55. style = {
  56. top: '100%',
  57. transform: 'translateX(-50%)',
  58. left: '50%',
  59. "margin-top": '10rpx',
  60. }
  61. break;
  62. }
  63. return Object.entries(style).map(([key, value]) => `${key}: ${value}`).join('; ');
  64. }
  65. },
  66. props: {
  67. content: {
  68. type: String,
  69. default: ''
  70. },
  71. placement: {
  72. type: String,
  73. default: 'bottom'
  74. },
  75. }
  76. }
  77. </script>
  78. <style>
  79. .uni-tooltip {
  80. position: relative;
  81. cursor: pointer;
  82. display: inline-block;
  83. }
  84. .uni-tooltip-popup {
  85. z-index: 1;
  86. display: none;
  87. position: absolute;
  88. background-color: #333;
  89. border-radius: 8px;
  90. color: #fff;
  91. font-size: 12px;
  92. text-align: left;
  93. line-height: 16px;
  94. padding: 12px;
  95. overflow: auto;
  96. }
  97. .uni-tooltip:hover .uni-tooltip-popup {
  98. display: block;
  99. }
  100. </style>