模拟钉钉官网动画

实现思路:利用粘性定位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

相关推荐
白白白鲤鱼11 天前
Vue2项目—基于路由守卫实现钉钉小程序动态更新标题
服务器·前端·spring boot·后端·职场和发展·小程序·钉钉
后青春期的诗go11 天前
对接钉钉审批过程记录(C#版本)
钉钉·钉钉对接·流程对接·钉钉流程对接
yidiancaijing23 天前
钉钉换帅后,先砍自己人
钉钉
Wezzer23 天前
Zabbix告警系统集成指南:从钉钉机器人到网易邮件的全流程配置
运维·钉钉·zabbix·触发器·网易邮箱
Better Rose23 天前
2025年“创新杯”(原钉钉杯) A题 建模思路
人工智能·数学建模·钉钉
钉钉开发者社区23 天前
AI开放课堂:钉钉MCP开发实战
人工智能·钉钉
热心网友俣先生23 天前
20255年第四届创新杯(原钉钉杯)参考论文+标准答案发布
钉钉
CZZDg23 天前
zabbix自动发现、自动注册、及自动预警到邮箱
钉钉·zabbix
Layux24 天前
使用钉钉开源api发送钉钉工作消息
java·spring boot·钉钉
yuzx200825 天前
ubuntu 24.04 xfce4 钉钉输入抢焦点问题
linux·ubuntu·钉钉