模拟钉钉官网动画

实现思路:利用粘性定位sticky,以及滚动事件实现。首先我们应该设置滚动动画开始位置和结束位置 ,然后根据位置计算透明度或者transform,scale的值。

首先根据上述图线计算属性值,代码如下:

javascript 复制代码
function createAnimate(scrollStart, scrollEnd, valueStart, valueEnd) {
      return function (x) {
        if (x < scrollStart) {
          return valueStart;
        }
        if (x > scrollEnd) {
          return valueEnd;
        }
        const bili = (valueEnd - valueStart) / (scrollEnd - scrollStart);
        return bili * (x - scrollStart) + valueStart;
      };
    }

然后考虑到每个dom它的动画不是只有一个属性可能有多个,例如scale,opacity,transform等。然后应该将dom和属性存储到map集合中,key为dom,属性为value。

javascript 复制代码
const items = document.querySelectorAll('.list-item');
const animationMap = new Map();


const getTitleAnimation = (scrollStart, scrollEnd, dom) => {
      const opacityAnimation = createAnimate(scrollStart, scrollEnd, 1, 0);
      const yAnimation = createAnimate(scrollStart, scrollEnd, 0, -200);
      const transform = function (x) {
        return `translate(0,${yAnimation(x)}px)`;
      };
      const opacity = function (x) {
        return opacityAnimation(x);
      };
      return {
        transform,
        opacity,
      };
    };


const updateMap=()=>{
    for (const item of items) {
        animationMap.set(
          item,
          getTitleAnimation(scrollStart + 50, scrollEnd, item)
        );
      }
}

然后就是属性赋值:

javascript 复制代码
const updateStyles = () => {
      const scrollY = window.scrollY;
      for (const [dom, animations] of animationMap) {
        for (const prop in animations) {
          const value = animations[prop](scrollY);
          dom.style[prop] = value;
        }
      }
    };
updateStyles();
window.addEventListener('scroll', updateStyles);

全部代码访问: liuzicheng/web - Gitee.com

相关推荐
paopao_djshddhdj1 天前
钉钉定制开发收费全解析:费用构成、报价区间与避坑指南
钉钉
paopao_djshddhdj1 天前
杭州钉钉服务商怎么选?本地化服务更安心
钉钉
paopao_djshddhdj2 天前
钉钉智能绩效深度解析:从目标对齐到组织提效的数字化实践
钉钉
dsyuan0012 天前
基于 Vue3 + ElementPlus 极简搭建仿钉钉流程设计器,简洁架构、极易扩展,纯div+css布局
css·架构·钉钉
paopao_djshddhdj4 天前
钉钉专属版办公:与其他版本的核心差异及适用企业解析
钉钉
paopao_djshddhdj5 天前
钉钉千问办公完整介绍:功能、收费与对比分析
钉钉
ShineWinsu5 天前
对于OpenClaw:核心命令以及飞书钉钉等渠道的解析
人工智能·钉钉·飞书
paopao_djshddhdj5 天前
钉钉考勤机如何结合钉钉使用:从安装到管理的完整指南
钉钉
paopao_djshddhdj10 天前
钉钉私有化部署详解:适用企业、部署模式与选型指南
钉钉
paopao_djshddhdj10 天前
钉钉与钉钉服务商有什么区别?为什么建议选择服务商
android·钉钉