vue+elementUI+transition实现鼠标滑过div展开内容,鼠标划出收起内容,加防抖功能

文章目录


一、场景

这两天做项目,此产品提出需求 要求详情页的顶部区域要在鼠标划入后展开里面的内容,鼠标划出要收起部分内容,详情底部的内容高度要自适应,我这里运用了鼠标事件+transition实现,加上lodash debounce进行防抖,下面是具体实现代码

二、实现代码

1.子组件代码结构

代码如下(示例):

javascript 复制代码
<template>
  <div
    class="project-header"
    @mouseenter="enterFun()"
    @mouseleave="leaveFun()"
    ref="project-header"
  >
    <div class="project-header-top">
    顶部要显示的部分
    </div>
    <div class="bottom" v-show="showProjectHeaderBottom">
    顶部要隐藏的部分,鼠标划入显示
    </div>
  </div>
</template>

<script>
import { debounce } from 'lodash';//npm install lodash引入
export default {
  name: '',
  components: {},
  props: {
  },
  data() {
    return {
      showProjectHeaderBottom: false,//控制顶部要隐藏的显隐
    };
  },
  methods: {
    // 鼠标滑过
    enterFun:debounce(function(){
      this.showProjectHeaderBottom = true;
      this.$emit('getHeight', 'hover');
    },100),
    // 鼠标划出
    leaveFun:debounce(function(){
      this.showProjectHeaderBottom = false;
      this.$emit('getHeight', null);
    },100),
  },
};
</script>
<style lang="scss" scoped>
.project-header {
  height: 68px;
}
</style>

2.父组件

代码如下(示例):

c 复制代码
<template>
  <div class="project-detail" ref="project-browse" >
    <Header
      ref="project-detail-top"
      class="project-detail-top"
      @getHeight="getHeight"
    >
    </Header>
    <div class="tab-content" style="position: relative" ref="tab-content">
    </div>
  </div>
</template>

<script>
import Header from './components/detailHeader.vue';
export default {
  name: '',
  components: {  Header},
  props: {},
  data() {
  },
  methods: {
    mountedInit() {
     this.getHeaderheight();
      window.addEventListener('resize', this.getHeaderheight);
    },
    getHeaderheight(type) {
      this.$nextTick(() => {
        let projectHeader = type === 'hover' ? 208 : 60;
        document.querySelector('.tab-content .el-tabs__content').style.overflow = type === 'hover' ? 'hidden' : 'auto';
        let height = this.$refs['project-browse']?.offsetHeight - projectHeader;
        this.$refs['tab-content'].style.height = height + 'px';//自适应
        this.$refs["project-detail-top"].$el.style.height=projectHeader+ 'px';
      });
    },

  },
  created() {
    this.createdInit();
  },
  mounted() {
    this.mountedInit();
  },
};
</script>
<style lang="scss" scoped>
.project-detail-top {
  transition: height 0.2s ease-in-out;
  overflow: hidden;
}
</style>

相关推荐
雨雨雨雨雨别下啦44 分钟前
心理健康AI助手 - 项目总结
前端·javascript·vue.js·人工智能·信息可视化
风之舞_yjf1 小时前
Vue基础(32)_TodoList案例
前端·javascript·vue.js
Jwest20213 小时前
工业显示器什么牌子质量最好性价比最高?
计算机外设
程序软件分享4 小时前
vue多语言交易所系统/期货/合约交易/质押生息/盲盒/挖矿/跟单源码
前端·javascript·vue.js·期货平台源码
Rooting++5 小时前
package.json三种依赖的区别
vue.js·json
Yeh2020585 小时前
springboot+vue笔记
vue.js·spring boot·笔记
m0_751018666 小时前
docker 安装 nginx
vue.js·nginx·docker
zyl837216 小时前
3Dmol.js + Vue3快速上手
vue.js
镜宇秋霖丶7 小时前
2026.5.18@霖宇博客制作中遇见的问题
vue.js
w_t_y_y7 小时前
VUE3(二)VUE2和VUE3区别
前端·javascript·vue.js