模拟钉钉官网动画

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

相关推荐
陌陌6232 天前
Clawdbot接入钉钉 / Moltbot接入钉钉
钉钉·clawdbot
Yolanda943 天前
【项目经验】钉钉免密登录实现
前端·javascript·钉钉
Yolanda943 天前
【教程技巧】钉钉自定义机器人新建及url获取流程
机器人·钉钉
gjxDaniel6 天前
钉钉是什么?
钉钉·生活
AITOP1008 天前
钉钉8.2.5版本上线 “AI 差旅”,携手高德支付宝实现智能比价与免垫资
钉钉·钉钉ai差旅·aitop100
光锥智能9 天前
飞书钉钉AI硬件争夺战:录音背后的入口之争
人工智能·钉钉·飞书
kkoral9 天前
【FFmpeg 智慧园区场景应用】5.企业微信 / 钉钉告警一键切换脚本(含静默周期 + 恢复通知)
ffmpeg·钉钉·企业微信
kkoral9 天前
【FFmpeg 智慧园区场景应用】4.企业微信 / 钉钉机器人告警配置(替换邮件告警)
ffmpeg·钉钉·企业微信
季布,12 天前
本地Windows测试:钉钉群消息/文件传输到Python服务(完整教程)
windows·python·钉钉
右手 无名指13 天前
Github Actions工作流配置webhook推送到钉钉机器人
机器人·github·钉钉