康巴易测肤/伤疤uniapp小程序类
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

367 行
10KB

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