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>

相关推荐
AntsData5 小时前
用 12K+ 条亚马逊评论信号,定位机械键盘下一版该改什么
java·大数据·计算机外设
小郎君6585 小时前
Vue 多标签页的加载态设计:为什么我没有用 Suspense 实现 PageLoading
vue.js·算法
yilaisai6 小时前
前端开发遇到的一些细节问题整理
vue.js
华玥作者7 小时前
Vue 3 + TypeScript + uni-app 组件库推荐:hy-app(华玥组件库)
vue.js·typescript·uni-app
阿琳a_7 小时前
b-map-viewer:面向 Vue 3 的地图可视化 SDK,从接入到展示再到交互分析
前端·javascript·vue.js·gis·数据可视化·地图
齐 飞8 小时前
VUE2常用语法总结
javascript·vue.js
时间的情敌8 小时前
Vue3 + Cesium 鼠标交互绘制多边形 / 线(贴地绘制,动态预览,Entity 实现)
计算机外设
北城笑笑8 小时前
Vue 106 ,上篇:前端桌面端应用实现方案( Electron 框架详细介绍,Vue + Electron )
前端·javascript·vue.js·python·electron
不磕了还不行9 小时前
用 Vue + Element UI 把元宝搜索的返回值做成“能点开检查的证据卡片”
前端·vue.js·ui
FRing0319 小时前
vue2干货总结2(v-bind、v-for、v-model、指令修饰符、v-bind操作class和style)
开发语言·前端·vue.js·vue2