DingDing-Dashboard 发表于 2023-05-15 | 分类于 Extensions 钉钉首页滚动动画 index.html 12345678910111213141516171819202122232425262728293031323334353637383940<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>DingDing</title> <link href="./index.css" rel="stylesheet"></head><body> <div class="playground"> <div class="content-container"></div> <div class="animation-container"> <div class="list"> <div data-order="0" class="list-item"></div> <div data-order="1" class="list-item"></div> <div data-order="2" class="list-item"></div> <div data-order="3" class="list-item"></div> <div data-order="3" class="list-item"></div> <div data-order="2" class="list-item"></div> <div data-order="1" class="list-item"></div> <div data-order="0" class="list-item"></div> <div data-order="0" class="list-item"></div> <div data-order="1" class="list-item"></div> <div data-order="2" class="list-item"></div> <div data-order="3" class="list-item"></div> <div data-order="3" class="list-item"></div> <div data-order="2" class="list-item"></div> <div data-order="1" class="list-item"></div> <div data-order="0" class="list-item"></div> </div> </div> </div> <script src="./index.js"></script></body></html> index.css 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354* { padding: 0; margin: 0;}.playground { width: 100%; height: 6000px; background-color: #000;}.content-container { width: 100%; height: 1400px; background-color: green;}.animation-container { position: relative; width: 100%; height: 2000px; background-color: orange;}.list { width: 1200px; height: 560px; padding: 0 70px; display: flex; align-items: center; justify-content: space-around; flex-wrap: wrap; background-color: rgba(23, 26, 29, .9); position: sticky; margin: 0 auto; top: 20%; box-sizing: border-box;}.list-item{ width: 80px; height: 80px; margin-left: 40px; border-radius: 15px;}.list-item:nth-child(odd) { background-color: pink;}.list-item:nth-child(even) { background-color: red;} index.js 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596const list = document.getElementsByClassName('list')[0]const items = document.getElementsByClassName('list-item')const playGround = document.getElementsByClassName('playground')const contentContainer = document.getElementsByClassName('content-container')[0]const animationContainer = document.getElementsByClassName('animation-container')[0]function createAnimation(scrollStart, scrollEnd, valueStart, valueEnd) { return function (scroll) { if (scroll <= scrollStart) return valueStart if (scroll >= scrollEnd) return valueEnd return (valueStart + ((valueEnd - valueStart) * (scroll - scrollStart)) / (scrollEnd - scrollStart)) }}const animationMap = new Map()function getDomAnimation(scrollStart, scrollEnd, dom) { scrollStart = scrollStart + dom.dataset.order * 200 const opacityAnimation = createAnimation(scrollStart, scrollEnd, 0, 1) const opacity = function (scroll) { return opacityAnimation(scroll) } const scaleAnimation = createAnimation(scrollStart, scrollEnd, 0.5, 1) const xAnimation = createAnimation(scrollStart, scrollEnd, list.clientWidth / 2 - dom.offsetLeft - dom.clientWidth / 2, 0) const yAnimation = createAnimation(scrollStart, scrollEnd, list.clientHeight / 2 - dom.offsetTop - dom.clientHeight / 2, 0) const transform = function (scroll) { return `translate(${xAnimation(scroll)}px,${yAnimation(scroll)}px) scale(${scaleAnimation(scroll)})` } return { opacity, transform }}function updateMap() { // 浏览器比例缩放等重置 animationMap.clear() console.log('contentContainer: ', getStyles(contentContainer, 'height'), window.scrollY); const scrollStart = getStyles(contentContainer, 'height') - 630 const scrollEnd = getStyles(contentContainer, 'height') + getStyles(animationContainer, 'height') for (const item of items) { animationMap.set(item, getDomAnimation(scrollStart, scrollEnd, item)) }}function updateStyles() { const scroll = window.scrollY for (let [dom, value] of animationMap) { // console.log(value.transform(scroll), '333') for (const cssProps in value) { dom.style[cssProps] = value[cssProps](scroll) } }}updateMap()updateStyles()window.addEventListener('scroll', updateStyles)function getScrollOffset() { if (window.pageXOffset) { return { left: window.pageXOffset, // scrollX top: window.pageYOffset // scrollY } } else { return { left: document.body.scrollLeft + document.documentElement.scrollLeft, top: document.body.scrollTop + document.documentElement.scrollTop } }}// 元素内容尺寸function getStyles(ele, props) { if (window.getComputedStyle) { if (props) { return parseInt(window.getComputedStyle(ele, null)[props]) } else { return window.getComputedStyle(ele, null) } } else { if (props) { return parseInt(ele.currentStyle[props]) } else { return ele.currentStyle } }}