模拟钉钉官网动画

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

相关推荐
2601_958320578 小时前
【小白零基础上手 】钉钉内部机器人接入 OpenClaw 完整流程讲解(含安装包)
人工智能·windows·机器人·钉钉·open claw·open claw安装
无心水1 天前
【Hermes:多平台接入】19、钉钉/飞书/企业微信:国内办公场景接入指南 —— 将 Honcho 智能体部署到你的工作聊天软件
人工智能·钉钉·飞书·企业微信·openclaw·hermes·honcho
云中计算巫作坤2 天前
泛微OA,该不该扔?
钉钉·飞书·企业数字化·泛微oa
翼龙云_cloud3 天前
阿里云代理商:阿里云部署的Hermes Agent 钉钉接入指南
人工智能·阿里云·云计算·钉钉·ai 智能体·hermes agent
lizz6664 天前
Hermes-Agent:钉钉dingtalk增加语音识别
人工智能·钉钉·语音识别
tianxiaxue14 天前
企微群内超时提醒通知到飞书/钉钉?
钉钉·飞书·企业微信
lizz6668 天前
Hermes-Agent:钉钉dingtalk配置定时任务收集
人工智能·钉钉
光于前裕于后8 天前
OpenMetadata设置数据质量规则,并通过钉钉告警
钉钉
观测云9 天前
观测云集成钉钉 SSO 最佳实践
钉钉·sso·观测云
终端行者9 天前
Jenkins流水线添加企业微信或者钉钉通知 pipeline 如何通过企微/钉钉通知
ci/cd·jenkins·钉钉·企业微信