Vue 鼠标滑入元素改变其背景颜色,且鼠标划入另一块区域,背景颜色保持不变

在这里插入图片描述
在这里插入图片描述如上图所示:鼠标划入"条件区域",对应ul元素改变背景颜色,且划入内容区域时,ul元素的背景颜色保持不变。只有当鼠标划出"内容区域",或者切换到"条件区域"的其他ul元素上时,背景颜色才恢复

HTML

<template>
  <div class="apps">
    <div class="parent">
      <ul
        v-for="(item, index) in 5"
        :key="index"
        @mouseover="mouseoverParent(index)"
        @mouseout="mouseoutParent(index)"
        class="parent-ul"
      >
        <li>触摸条件-{{ item }}</li>
      </ul>

      <div
        class="other-div"
        @mouseover="mouseoverOther()"
        @mouseout="mouseoutOther()"
      >
        <ul v-for="(item, index) in 10" :key="index">
          <li>内容-{{ item }}</li>
        </ul>
      </div>
    </div>
  </div>
</template>

JS

this.index 初始值为 0

    mouseoverParent(index) {
      let i
      i = index + 1
      this.index = i
      const style = "background:#ff6700"
      document.querySelector(`.parent > :nth-child(${i})`).style = style
    },
    mouseoutParent(index) {
      let i
      i = index + 1
      this.index = i
      const style = "background:#b0b0b0"
      document.querySelector(`.parent > :nth-child(${i})`).style = style
    },
    mouseoverOther() {
      const style = "background:#ff6700"
      document.querySelector(`.parent > :nth-child(${this.index})`).style =
        style
    },
    mouseoutOther() {
      const style = "background:#b0b0b0"
      document.querySelector(`.parent > :nth-child(${this.index})`).style =
        style
    },

SCSS

.apps {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.parent {
  position: relative;
  width: 200px;
  height: 400px;
  background: #b0b0b0;
  text-align: center;

  .parent-ul {
    padding-top: 30px;
    height: 40px;
    width: 100%;
    &:hover ~ .other-div {
      width: 400px;
    }
    li {
      font-size: 16px;
    }
  }
}

.other-div {
  overflow: hidden;
  height: 0;
  transition: all 0.3s;

  position: absolute;
  width: 0; // 当元素内存在内容时,设置宽度才显示
  height: 400px;
  left: 200px;
  top: 0;
  background: palevioletred;
  box-shadow: 5px 0 15px -5px rgba(0, 0, 0, 0.6);

  display: inline-flex;
  flex-wrap: wrap;
  flex-direction: column;
  align-content: flex-start;

  &:hover {
    width: 400px;
  }

  ul {
    width: 120px;
    height: 60px;
    li {
      font-size: 16px;
    }
  }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值