添加常见一些动画问题

文章目录

    • 1.鼠标经过添加下划线
    • 2.滚动到元素开始动画

如果没有一些花里胡哨的操作,那么有什么意义呢,那么我会在这篇文章文章记录我搬运过来的笔记

1.鼠标经过添加下划线

<div class="page">
    <div class="text">测试文字</div>
  </div>
.page {
  .text {
    position: relative;
    display:inline-block;
    padding: 5px;
    &::after {
      content: '';
      position: absolute;
      width: 100%;
      height: 2px;
      bottom: 0;
      left: 0;
      transition: all 0.3s ease-in-out;
      transform: scaleX(0);
      transform-origin: right;
    }
    &:hover {
      &::after {
        transform: scaleX(1);
        background-color: red;
      }
    }
  }
}

2.滚动到元素开始动画

 <div class="page" ref="pageRef">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div
      class="box"
      :class="{ hidden: true, show: showVisible }"
      ref="divRef"
    ></div>
  </div>

import { ref, onMounted, nextTick } from 'vue'
const showVisible = ref<boolean>(false)
const divRef = ref<HTMLDivElement>()
const pageRef = ref<HTMLDivElement>()
function removeHandle() {
  if (!pageRef.value || !divRef.value) return
  const height = pageRef.value?.scrollTop + pageRef.value?.offsetHeight
  showVisible.value = height > divRef.value?.offsetTop
  if (showVisible.value)
    pageRef.value?.removeEventListener('scroll', removeHandle)
}
onMounted(() => {
  nextTick(() => {
    pageRef?.value?.addEventListener('scroll', removeHandle)
  })
})

.page {
  width: 100%;
  height: 100%;
  overflow: auto;
}
.box {
  width: 200px;
  height: 200px;
  background-color: aqua;
  margin-bottom: 30px;
}
.hidden {
  opacity: 0;
  transition: all 0.5s ease;
  transform: translateX(-30px);
}
.show {
  opacity: 1 !important;
  transform: translateX(0px) !important;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值