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>

相关推荐
老华带你飞21 小时前
农产品销售管理|基于java + vue农产品销售管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
C_心欲无痕1 天前
vue3 - 类与样式的绑定
javascript·vue.js·vue3
JIngJaneIL1 天前
基于springboot + vue房屋租赁管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
期待のcode1 天前
@RequestBody的伪表单提交场景
java·前端·vue.js·后端
一颗烂土豆1 天前
vfit.js v2.0.0 发布:精简、语义化与核心重构 🎉
前端·vue.js·响应式设计
legendary_1631 天前
Type-C 一拖二快充线:实用、便携的移动充电方式
计算机外设·电脑·音视频
总之就是非常可爱1 天前
vue3 KeepAlive 核心原理和渲染更新流程
前端·vue.js·面试
red润1 天前
手把手封装Iframe父子单向双向通讯功能
前端·javascript·vue.js
肖老师xy1 天前
Ai生成时间排期进度
javascript·vue.js·elementui
Aliex_git1 天前
Vue 错误处理机制源码理解
前端·javascript·vue.js