{{ value }}
当前位置:   article > 正文

vue设置显示几行,超出鼠标hover显示全部_vue中如何在div展示的时候只展示一部分,在鼠标悬浮的时候展示全部

vue中如何在div展示的时候只展示一部分,在鼠标悬浮的时候展示全部

在组件中添加vueText

  1. <template>
  2. <div
  3. class="vue-text"
  4. ref="text"
  5. :style="textStyle"
  6. @mouseenter="mouseenter"
  7. @mouseleave="mouseleave"
  8. >
  9. {{ value }}
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. // 显示文字组件,可以设置最多显示几行,超过后会隐藏并且鼠标hover显示全部信息(需要给组件设置宽度)
  15. name: 'VueText',
  16. props: {
  17. value: {
  18. type: String,
  19. default: ''
  20. },
  21. row: {
  22. //最多显示几行,超过后会...隐藏 为0时不隐藏
  23. type: [Number, String],
  24. default: 0
  25. }
  26. },
  27. computed: {
  28. text() {
  29. return this.$refs.text
  30. }
  31. },
  32. data() {
  33. return {
  34. isShowHover: false,
  35. textStyle: {},
  36. div: null
  37. }
  38. },
  39. watch: {
  40. row: function (val) {
  41. this.init()
  42. },
  43. value: function () {
  44. this.isShowHover = false
  45. this.textStyle = {
  46. cursor: 'text',
  47. }
  48. this.$nextTick(() => {
  49. this.getStyle(this.row - 0)
  50. })
  51. }
  52. },
  53. mounted() {
  54. this.init()
  55. },
  56. methods: {
  57. init() {
  58. this.div = document.querySelector('.hover-wrap')
  59. if (!this.div && this.row - 0) {
  60. this.div = document.createElement('div')
  61. this.div.className = 'hover-wrap'
  62. this.div.style.top = 0
  63. this.div.style.left = 0
  64. document.body.append(this.div)
  65. }
  66. if (this.div && this.row - 0) {
  67. this.getStyle(this.row - 0)
  68. }
  69. },
  70. getStyle(val) {
  71. let lineHeight =
  72. getComputedStyle(this.text).lineHeight.replace('px', '') - 0 || 20
  73. let height = getComputedStyle(this.text).height.replace('px', '') - 0
  74. if (height > lineHeight * val) {
  75. this.isShowHover = true
  76. this.textStyle = {
  77. height: `${lineHeight * val}px`,
  78. overflow: 'hidden',
  79. textOverflow: 'ellipsis',
  80. display: '-webkit-box',
  81. webkitLineClamp: val,
  82. webkitBoxOrient: 'vertical',
  83. cursor: 'pointer',
  84. // lineHeight:'inherit'
  85. }
  86. } else {
  87. this.isShowHover = false
  88. this.textStyle = {
  89. cursor: 'text'
  90. }
  91. }
  92. },
  93. mouseenter(e) {
  94. if (!this.isShowHover) {
  95. return
  96. }
  97. let box = e.target.getBoundingClientRect()
  98. let top = box.top + box.height + 'px'
  99. let left = 0
  100. this.div.innerHTML = this.value
  101. left =
  102. box.left +
  103. (box.width - this.div.getBoundingClientRect().width) / 2 +
  104. 'px'
  105. this.div.style.top = top
  106. this.div.style.left = left
  107. this.div.classList.add('active')
  108. },
  109. mouseleave(e) {
  110. if (!this.isShowHover) {
  111. return
  112. }
  113. if (e.relatedTarget !== this.div) {
  114. this.div.style.top = 0
  115. this.div.style.left = 0
  116. this.div.classList.remove('active')
  117. }
  118. this.div.onmouseleave = () => {
  119. this.div.style.top = 0
  120. this.div.style.left = 0
  121. this.div.classList.remove('active')
  122. }
  123. }
  124. }
  125. }
  126. </script>
  127. <style lang="css">
  128. .hover-wrap {
  129. position: absolute;
  130. background-color: #fff;
  131. color: #666;
  132. font-size: 14px;
  133. line-height: 1.4;
  134. padding: 6px 10px;
  135. border-radius: 5px;
  136. /* border: 1px solid #FF8A51; */
  137. max-width: 400px;
  138. transition: opacity 0.3s linear;
  139. box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  140. z-index: -1;
  141. opacity: 0;
  142. }
  143. .hover-wrap.active {
  144. z-index: 1999;
  145. opacity: 1;
  146. }
  147. </style>

vue页面引用

  1. <template>
  2. <vue-text
  3. value="显示文字组件,可以设置最多显示几行,超过后会隐藏并且鼠标hover显示全部信息(需要给组件设置宽度)"
  4. :row="3"
  5. />
  6. </template>
  7. <script>
  8. import VueText from '@/components/VueText'
  9. export default {
  10. components: { VueText }
  11. }
  12. </script>
  13. <style>
  14. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/91792
推荐阅读
相关标签
  

闽ICP备14008679号