模拟钉钉官网动画

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

相关推荐
新老农3 天前
php数据导出pdf,然后pdf转图片,再推送钉钉群
pdf·php·钉钉
z日火3 天前
集成钉钉消息推送功能
钉钉·消息推送
xiaogai_gai6 天前
高效管理钉钉收款单数据集成到MySQL的技术方案
android·mysql·钉钉
邹卓为1 个月前
Jenkins 发送钉钉消息
运维·jenkins·钉钉
在荒野的梦想1 个月前
若依微服务集成Flowable仿钉钉工作流
spring cloud·微服务·钉钉
薄荷你玩_1 个月前
[Python] 企业内部应用接入钉钉登录,端内免登录+浏览器授权登录
开发语言·python·钉钉
生活百般滋味,人生需要笑对。 --佚名2 个月前
springboot对接OK交易所实现发送钉钉通知
spring boot·后端·钉钉
走,带你去玩2 个月前
uniapp运行到支付宝开发者工具
uni-app·钉钉
dragonchow1232 个月前
钉钉机器人
机器人·钉钉