当前位置:   article > 正文

vue 鼠标移入不同div悬浮显示不同的表格el-table内容,鼠标移出悬浮内容消失_vue鼠标悬浮显示内容

vue鼠标悬浮显示内容

一、封装悬浮显示的内容

表格:elment-UI

  1. ShowBox.vue
  2. <template>
  3. <div class="table-box">
  4. <div class="table-title">{{title}}</div>
  5. <el-table :data="tableData" stripe style="width: 100%;">
  6. <el-table-column prop="date" label="日期">
  7. </el-table-column>
  8. <el-table-column prop="name" label="姓名">
  9. </el-table-column>
  10. <el-table-column prop="address" label="地址">
  11. </el-table-column>
  12. </el-table>
  13. </div>
  14. </template>
  15. <script>
  16. export default {
  17. props: {
  18. title: {
  19. type: String,
  20. default: '无',
  21. }
  22. },
  23. data() {
  24. return {
  25. tableData: [{
  26. date: '2016-05-02',
  27. name: '王小虎',
  28. address: '上海市普陀区金沙江路 1518 弄'
  29. }, {
  30. date: '2016-05-04',
  31. name: '王小虎',
  32. address: '上海市普陀区金沙江路 1517 弄'
  33. }, {
  34. date: '2016-05-01',
  35. name: '王小虎',
  36. address: '上海市普陀区金沙江路 1519 弄'
  37. }, {
  38. date: '2016-05-03',
  39. name: '王小虎',
  40. address: '上海市普陀区金沙江路 1516 弄'
  41. }]
  42. }
  43. }
  44. }
  45. </script>
  46. <style>
  47. .table-box {
  48. width: 400px;
  49. /* height: 200px; */
  50. border: 1px solid #000;
  51. }
  52. .table-title {
  53. font-size: 23px;
  54. text-align: left;
  55. font-weight: 600;
  56. }
  57. </style>

二、鼠标移入、移出操作(重点)

根据鼠标移入的x、y坐标确定悬浮内容的位置。

完整代码如下:

  1. <template>
  2. <div class="container">
  3. <div v-for="(item, index) in items" :key="index" @mouseenter="mouseOver($event,item)" @mouseleave="mouseLeave"
  4. class="item-box">{{ item }}
  5. </div>
  6. <ShowBox id="atable" v-if="showTable" :title="title"></ShowBox>
  7. </div>
  8. </template>
  9. <script>
  10. import ShowBox from "./ShowBox.vue";
  11. export default {
  12. components: {
  13. ShowBox
  14. },
  15. data() {
  16. return {
  17. items: [
  18. "1-1-1",
  19. "1-1-2",
  20. "1-1-3",
  21. "1-1-4",
  22. "1-2-1",
  23. ],
  24. showTable: false, // 初始化表格为关闭状态
  25. title: '',
  26. };
  27. },
  28. mounted() {
  29. // this.activation()
  30. },
  31. methods: {
  32. //鼠标移入,显示相对于的表格数据
  33. mouseOver(e, item) {
  34. this.showTable = true;
  35. console.log("鼠标移入" + e + item)
  36. this.title = item
  37. const x = e.clientX;
  38. const y = e.clientY;
  39. console.log("x:" + x + "y:" + y)
  40. this.$nextTick(function() {
  41. var divElement = document.getElementById("atable")
  42. console.log("divElement:" + divElement)
  43. divElement.style.position = "absolute"
  44. divElement.style.left = x + "px"
  45. divElement.style.top = y + "px"
  46. divElement.style.backgroundColor = "coral"
  47. })
  48. },
  49. //鼠标移出li,隐藏表格数据
  50. mouseLeave() {
  51. this.showTable = false
  52. console.log("鼠标移出")
  53. },
  54. },
  55. };
  56. </script>
  57. <style>
  58. .container {
  59. /* display: flex; */
  60. /* 设置容器为flex布局 */
  61. /* justify-content: space-between; */
  62. /* 水平居中显示子项 */
  63. }
  64. .item-box {
  65. width: 100px;
  66. /* 设置每个div的宽度 */
  67. height: 30px;
  68. /* 设置每个div的高度 */
  69. background-color: forestgreen;
  70. /* 设置每个div的背景色 */
  71. margin-top: 7px;
  72. margin-left: 3px;
  73. /* 设置每个div之间的外边距 */
  74. color: #fff;
  75. display: flex;
  76. justify-content: center;
  77. align-items: center;
  78. font-size: 22px;
  79. font-weight: 500;
  80. border-style: ridge;
  81. border-width: 2px;
  82. border-color: #0085AA;
  83. }
  84. </style>

效果:

如果想要改变表格内容,在ShowBox.vue里面添加传值参数,如下

  1. props: {
  2. title: {
  3. type: String,
  4. default: '无',
  5. },
  6. tableData: {
  7. type: Array,
  8. default: null,
  9. }
  10. },

调用界面添加如下

<ShowBox id="atable" v-if="showTable" :title="title" :tableData="tableData"></ShowBox>
  1. data() {
  2. return {
  3. tableData:[],
  4. };
  5. },

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

闽ICP备14008679号