模拟钉钉官网动画

实现思路:利用粘性定位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_djshddhdj4 天前
钉钉私有化部署详解:适用企业、部署模式与选型指南
钉钉
paopao_djshddhdj4 天前
钉钉与钉钉服务商有什么区别?为什么建议选择服务商
android·钉钉
dsyuan0014 天前
ak-design 仿钉钉流程设计引擎图
钉钉·状态模式
paopao_djshddhdj5 天前
钉钉宜搭版本与收费标准全解析(2026 版)
钉钉
钉钉开发者社区5 天前
来钉钉,接入 DeepSeek Harness
人工智能·ai·钉钉·钉钉cli
皮皮虾❀6 天前
钉钉AI服务商×教育行业:AI现代产业学院共建一体化实战——从“高校AI教学缺平台”到“校企共建AI产业学院培养数字化人才
人工智能·钉钉
钉钉开发者社区7 天前
钉钉dws CLI更新 v1.0.59
ai·钉钉
皮皮虾❀7 天前
钉钉AI服务商+AI表格+AI听记+千问办公全产品线一体化实战:从“办公工具各自为战”到“服务商打包部署全套AI办公能力
人工智能·钉钉
皮皮虾❀7 天前
钉钉AI服务商+知识库梳理+Agent编排+系统集成一体化实战:从“AI回答不专业”到“服务商梳理企业知识库训练专属AI助手
人工智能·钉钉
paopao_djshddhdj7 天前
钉钉培训系统功能与收费详解:企业数字化培训的优选方案
运维·钉钉