模拟钉钉官网动画

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

相关推荐
路边闲人29 天前
用python向钉钉群发送消息
钉钉
腾飞开源9 天前
02_钉钉消息处理流程设计
人工智能·钉钉·agent智能体·ai智能体开发·全网首发·新课上线·消息处理器
蓝婴天使17 天前
Debian13 钉钉无法打开问题解决
linux·服务器·钉钉
阿登林17 天前
C#调用钉钉API实现安全企业内部通知推送
安全·c#·钉钉
后青春期的诗go1 个月前
金蝶云星空插件开发记录(一)
c#·钉钉·金蝶云星空·插件开发
Dark_programmer1 个月前
钉钉小程序 - - - - - 小程序内打开OA文档链接
小程序·钉钉
热爱生活的五柒1 个月前
系统代理开启时,钉钉页面加载失败
钉钉
阿三08121 个月前
钉钉 AI 深度赋能制造业 LTC 全流程:以钉钉宜搭、Teambition 为例
人工智能·低代码·钉钉·teambition
极新1 个月前
163起融资,梅卡曼德融资额夺冠,钉钉、百度智能云10周年,汉桑科技IPO| 2025年8月人工智能投融资观察 · 极新月报
科技·百度·钉钉
Dolphin_Home1 个月前
IT需求提示未读信息查询:深度技术解析与性能优化指南【类似:钉钉已读 功能】
性能优化·钉钉