js实现元素根据鼠标滚轮滚动向左右上下滑动着从模糊到清楚显示出来

html代码

<div ref={test} id="animatedElement" className="not-animated">
        <div style={{width:"100px",height:"50px",backgroundColor:"red"}}>
        </div>
</div>

JS代码

const test = useRef(null)
  // 监听滚轮事件
  useEffect(() => {
    const handleScrollaa = () => {
      const scrollTop1 = document.documentElement.scrollTop || document.body.scrollTop;
      const h = test.current;
      const triggerPoint = window.innerHeight
      if (scrollTop1 > triggerPoint * 0.3) {
        // 触发动画,通过添加 animate 类
        console.log(32135456434687)

       (headerLogo2 as HTMLElement).style.animation = `ROW3 2s forwards`;
      }
      else if(scrollTop1 < triggerPoint * 0.3){
        h.style.animation=""
        h.style.opacity="0"
      }
    };

    window.addEventListener('scroll', handleScrollaa);
    // 组件卸载时移除事件监听器
    return () => {
      window.removeEventListener('scroll', handleScrollaa);
    };
  }, []);

CSS代码(实现从左到右滑动出来,并且由模糊到清楚的展现)

@keyframes ROW3 {
  0% {
    transform: translateX(-100%); /* 动画开始时,图片在容器外部 */
    filter: blur(30px);  /*模糊属性*/
    opacity:0; /*opacity: 0 隐藏  1 显示 */
  }
  100% {
    transform: translateX(0) rotate(1turn) ; /* 动画结束时,图片移动到容器内部 rotate(1turn)是内容旋转,随着移动而滚动 */
    filter: blur(0);
    opacity:1;
  }}

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值