tooltip.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <template>
  2. <view
  3. class="u-tooltip"
  4. :style="[$u.addStyle(customStyle)]"
  5. >
  6. <u-overlay
  7. :show="showTooltip && tooltipTop !== -10000 && overlay"
  8. customStyle="backgroundColor: rgba(0, 0, 0, 0)"
  9. @click="overlayClickHandler"
  10. ></u-overlay>
  11. <view class="u-tooltip__wrapper">
  12. <view
  13. class="u-tooltip__wrapper__text"
  14. :id="textId"
  15. :ref="textId"
  16. :userSelect="false"
  17. :selectable="false"
  18. @longpress.stop="longpressHandler"
  19. :style="{
  20. color: color,
  21. backgroundColor: bgColor && showTooltip && tooltipTop !== -10000 ? bgColor : 'transparent'
  22. }"
  23. ><slot></slot></view>
  24. <u-transition
  25. mode="fade"
  26. :show="showTooltip"
  27. duration="200"
  28. :customStyle="{
  29. position: 'absolute',
  30. top: $u.addUnit(tooltipTop),
  31. zIndex: zIndex,
  32. ...tooltipStyle
  33. }"
  34. >
  35. <view
  36. class="u-tooltip__wrapper__popup"
  37. :id="tooltipId"
  38. :ref="tooltipId"
  39. >
  40. <view
  41. class="u-tooltip__wrapper__popup__indicator"
  42. hover-class="u-tooltip__wrapper__popup__indicator--hover"
  43. v-if="buttons.length"
  44. :style="[indicatorStyle, {
  45. width: $u.addUnit(indicatorWidth),
  46. height: $u.addUnit(indicatorWidth),
  47. }]"
  48. >
  49. <!-- 由于nvue不支持三角形绘制,这里就做一个四方形,再旋转45deg,得到露出的一个三角 -->
  50. </view>
  51. <view class="u-tooltip__wrapper__popup__list">
  52. <block v-for="(item , index) in buttons" :key="index">
  53. <view
  54. class="u-tooltip__wrapper__popup__list__btn"
  55. hover-class="u-tooltip__wrapper__popup__list__btn--hover"
  56. >
  57. <text
  58. class="u-tooltip__wrapper__popup__list__btn__text"
  59. @tap="btnClickHandler(index)"
  60. >{{ item }}</text>
  61. </view>
  62. <u-line
  63. direction="column"
  64. color="#8d8e90"
  65. v-if="index < buttons.length - 1"
  66. length="18"
  67. ></u-line>
  68. </block>
  69. </view>
  70. </view>
  71. </u-transition>
  72. </view>
  73. </view>
  74. </template>
  75. <script>
  76. // import props from './props.js';
  77. // #ifdef APP-NVUE
  78. const dom = uni.requireNativePlugin('dom')
  79. // #endif
  80. export default {
  81. // mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  82. props: {
  83. // 文本大小
  84. size: {
  85. type: [String, Number],
  86. default: 14,
  87. },
  88. // 字体颜色
  89. color: {
  90. type: String,
  91. default: 'black',
  92. },
  93. // 弹出提示框时,文本的背景色
  94. bgColor: {
  95. type: String,
  96. default: 'transparent',
  97. },
  98. // 弹出提示的方向,top-上方,bottom-下方
  99. direction: {
  100. type: String,
  101. default: 'top',
  102. },
  103. // 扩展的按钮组
  104. buttons: {
  105. type: Array,
  106. default: [],
  107. },
  108. },
  109. data() {
  110. return {
  111. overlay: true,// 是否显示透明遮罩以防止触摸穿透
  112. zIndex: 10071,// 弹出提示的z-index,nvue无效(默认 10071 )
  113. // 是否展示气泡
  114. showTooltip: true,
  115. // 生成唯一id,防止一个页面多个组件,造成干扰
  116. textId: uni.$u.guid(),
  117. tooltipId: uni.$u.guid(),
  118. // 初始时甚至为很大的值,让其移到屏幕外面,为了计算元素的尺寸
  119. tooltipTop: -10000,
  120. // 气泡的位置信息
  121. tooltipInfo: {
  122. width: 0,
  123. left: 0
  124. },
  125. // 文本的位置信息
  126. textInfo: {
  127. width: 0,
  128. left: 0
  129. },
  130. // 三角形指示器的样式
  131. indicatorStyle: {},
  132. // 气泡在可能超出屏幕边沿范围时,重新定位后,距离屏幕边沿的距离
  133. screenGap: 12,
  134. // 三角形指示器的宽高,由于对元素进行了角度旋转,精确计算指示器位置时,需要用到其尺寸信息
  135. indicatorWidth: 14,
  136. }
  137. },
  138. watch: {
  139. propsChange() {
  140. this.getElRect()
  141. }
  142. },
  143. computed: {
  144. // 特别处理H5的复制,因为H5浏览器是自带系统复制功能的,在H5环境
  145. // 当一些依赖参数变化时,需要重新计算气泡和指示器的位置信息
  146. propsChange() {
  147. // return [this.text, this.buttons]
  148. return [this.buttons]
  149. },
  150. // 计算气泡和指示器的位置信息
  151. tooltipStyle() {
  152. const style = {
  153. transform: `translateY(${this.direction === 'top' ? '-100%' : '100%'})`,
  154. },
  155. sys = uni.$u.sys(),
  156. getPx = uni.$u.getPx,
  157. addUnit = uni.$u.addUnit
  158. if (this.tooltipInfo.width / 2 > this.textInfo.left + this.textInfo.width / 2 - this.screenGap) {
  159. this.indicatorStyle = {}
  160. style.left = `-${addUnit(this.textInfo.left - this.screenGap)}`
  161. this.indicatorStyle.left = addUnit(this.textInfo.width / 2 - getPx(style.left) - this.indicatorWidth /
  162. 2)
  163. } else if (this.tooltipInfo.width / 2 > sys.windowWidth - this.textInfo.right + this.textInfo.width / 2 -
  164. this.screenGap) {
  165. this.indicatorStyle = {}
  166. style.right = `-${addUnit(sys.windowWidth - this.textInfo.right - this.screenGap)}`
  167. this.indicatorStyle.right = addUnit(this.textInfo.width / 2 - getPx(style.right) - this
  168. .indicatorWidth / 2)
  169. } else {
  170. const left = Math.abs(this.textInfo.width / 2 - this.tooltipInfo.width / 2)
  171. style.left = this.textInfo.width > this.tooltipInfo.width ? addUnit(left) : -addUnit(left)
  172. this.indicatorStyle = {}
  173. }
  174. if (this.direction === 'top') {
  175. style.marginTop = '-10px'
  176. this.indicatorStyle.bottom = '-4px'
  177. } else {
  178. style.marginBottom = '-10px'
  179. this.indicatorStyle.top = '-4px'
  180. }
  181. return style
  182. }
  183. },
  184. mounted() {
  185. this.init()
  186. },
  187. methods: {
  188. init() {
  189. this.getElRect()
  190. },
  191. // 长按触发事件
  192. async longpressHandler() {
  193. this.tooltipTop = 0
  194. this.showTooltip = true
  195. },
  196. // 点击透明遮罩
  197. overlayClickHandler() {
  198. this.showTooltip = false
  199. },
  200. // 点击弹出按钮
  201. btnClickHandler(index) {
  202. this.showTooltip = false
  203. // 如果需要展示复制按钮,此处index需要加1,因为复制按钮在第一个位置
  204. this.$emit('click', index)
  205. },
  206. // 查询内容高度
  207. queryRect(ref) {
  208. // #ifndef APP-NVUE
  209. // $uGetRect为uView自带的节点查询简化方法,详见文档介绍:https://www.uviewui.com/js/getRect.html
  210. // 组件内部一般用this.$uGetRect,对外的为uni.$u.getRect,二者功能一致,名称不同
  211. return new Promise(resolve => {
  212. this.$uGetRect(`#${ref}`).then(size => {
  213. resolve(size)
  214. })
  215. })
  216. // #endif
  217. // #ifdef APP-NVUE
  218. // nvue下,使用dom模块查询元素高度
  219. // 返回一个promise,让调用此方法的主体能使用then回调
  220. return new Promise(resolve => {
  221. dom.getComponentRect(this.$refs[ref], res => {
  222. resolve(res.size)
  223. })
  224. })
  225. // #endif
  226. },
  227. // 元素尺寸
  228. getElRect() {
  229. // 调用之前,先将指示器调整到屏幕外,方便获取尺寸
  230. this.showTooltip = true
  231. this.tooltipTop = -10000
  232. uni.$u.sleep(500).then(() => {
  233. this.queryRect(this.tooltipId).then(size => {
  234. this.tooltipInfo = size
  235. // 获取气泡尺寸之后,将其隐藏,为了让下次切换气泡显示与隐藏时,有淡入淡出的效果
  236. this.showTooltip = false
  237. })
  238. this.queryRect(this.textId).then(size => {
  239. this.textInfo = size
  240. })
  241. })
  242. },
  243. }
  244. }
  245. </script>
  246. <style lang="scss" scoped>
  247. @import "@/uview-ui/libs/css/components.scss";
  248. .u-tooltip {
  249. position: relative;
  250. @include flex;
  251. &__wrapper {
  252. @include flex;
  253. justify-content: center;
  254. /* #ifndef APP-NVUE */
  255. white-space: nowrap;
  256. /* #endif */
  257. &__text {
  258. font-size: 14px;
  259. }
  260. &__popup {
  261. @include flex;
  262. justify-content: center;
  263. &__list {
  264. background-color: #060607;
  265. position: relative;
  266. flex: 1;
  267. border-radius: 5px;
  268. padding: 0px 0;
  269. @include flex(row);
  270. align-items: center;
  271. overflow: hidden;
  272. &__btn {
  273. padding: 11px 13px;
  274. &--hover {
  275. background-color: #58595B;
  276. }
  277. &__text {
  278. line-height: 12px;
  279. font-size: 13px;
  280. color: #FFFFFF;
  281. }
  282. }
  283. }
  284. &__indicator {
  285. position: absolute;
  286. background-color: #060607;
  287. width: 14px;
  288. height: 14px;
  289. bottom: -4px;
  290. transform: rotate(45deg);
  291. border-radius: 2px;
  292. z-index: -1;
  293. &--hover {
  294. background-color: #58595B;
  295. }
  296. }
  297. }
  298. }
  299. }
  300. </style>