鼠标进入和离开时,展示和隐藏的元素

<template>
  <div
    :class="{ 'nav-box': true, 'nav-box-hover': state.showNavBoxHover }"
    :style="{ height: state.navBoxHeight + 'px' }"
    @mouseenter="handleMouseEnter"
    @mouseleave="handleMouseOut"
  >
    <ul ref="ulRef">
      <li v-for="i in 10" :key="i">{{ i }}</li>
    </ul>
    <!-- 当ul的高度大于外层div的高度60时,展示隐藏的元素 -->
    <el-icon class="i-down" v-if="state.ulHeight > 60"><ArrowDown /></el-icon>
    <el-icon class="i-up" v-if="state.ulHeight > 60"><ArrowUp /></el-icon>
  </div>
</template>
<script setup>
import { onMounted, ref, reactive } from 'vue'
const ulRef = ref()
const state = reactive({
  showNavBoxHover: false,
  navBoxHeight: '60',
  ulHeight: ''
})
// 鼠标进入div时,添加类和样式
const handleMouseEnter = () => {
  if (state.ulHeight > '60') {
    state.showNavBoxHover = true
    state.navBoxHeight = state.ulHeight
  }
}
// 鼠标离开div时,去除类和样式
const handleMouseOut = () => {
  state.showNavBoxHover = false
  state.navBoxHeight = '60'
}
onMounted(() => {
  // 获取ul的高度
  state.ulHeight = ulRef.value.clientHeight
})
</script>
<style scoped lang="less">
.nav-box {
  box-sizing: border-box;
  position: relative;
  width: 500px;
  transition: height 0.3s;
  overflow: hidden;
  background-color: #ccc;
  margin: 0 auto;
  cursor: pointer;

  ul {
    box-sizing: border-box;
    margin: 0;
    padding: 0 20px;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    li {
      font-size: 20px;
      box-sizing: border-box;
      list-style: none;
      padding: 0 18px;
      height: 60px;
      line-height: 60px;
    }
  }
  .i-up,
  .i-down {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    bottom: 0px;
    background: #f5f6f7;
    border-top-left-radius: 20%;
    border-top-right-radius: 20%;
    width: 60px;
  }
  .i-up {
    display: none;
  }
}
.nav-box-hover {
  .i-up {
    display: flex;
  }
  .i-down {
    display: none;
  }
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值