随着滚动条滚动动画效果

html文档结构

  <div>
       <div class="header">
           <h1>HEADER</h1>
       </div>
       <div class="palyground">
           <div class="list">
               <div class="list-item"></div>
               <div class="list-item"></div>
               <div class="list-item"></div>
               <div class="list-item"></div>
               <div class="list-item"></div>
               <div class="list-item"></div>
               <div class="list-item"></div>
               <div class="list-item"></div>
               <div class="list-item"></div>
               <div class="list-item"></div>
               <div class="list-item"></div>
               <div class="list-item"></div>
               <div class="list-item"></div>
               <div class="list-item"></div>
           </div>
       </div>
       <div class="footer"></div>
   </div>

js

 // 创建动画
        function createAnimation(scrollStart, scrollEnd, startValue, endValue) {
            return function (x) {
                if (x < scrollStart) {
                    return startValue
                }
                if (x > scrollEnd) {
                    return endValue
                }
                const progress = (x - scrollStart) / (scrollEnd - scrollStart);
                return startValue + (endValue - startValue) * progress
            }
        }
        const animationMap = new Map()
        const items = document.querySelectorAll('.list-item')
        const palyground = document.querySelector('.palyground')
        const list = document.querySelector('.list')

        // 设置动画映射
        function getDomAnimation(scrollStart, scrollEnd, dom) {
            //透明度
            const opacityAnimation = createAnimation(scrollStart, scrollEnd, 0, 1)
            const opacity = function (x) {
                return opacityAnimation(x)
            }
            //缩放和位移
            const scaleAnimation = createAnimation(scrollStart, scrollEnd, 0.5, 1)
            const { clientWidth, clientHeight, offsetTop, offsetLeft } = dom
            const rect = list.getBoundingClientRect()
            const xAnimation = createAnimation(scrollStart, scrollEnd,
                rect.width / 2 - clientWidth / 2 - offsetLeft
                , 0);
            const yAnimation = createAnimation(scrollStart, scrollEnd,
                rect.height / 2 - clientHeight / 2 - offsetTop
                , 0);
            const transform = function (x) {
                return `translate(${xAnimation(x)}px, ${yAnimation(x)}px) scale(${scaleAnimation(x)})`
            }
            return {
                opacity,
                transform
            }
        }

        //设置动画map
        function updateMap() {
            const palygroundRect = palyground.getBoundingClientRect()
            const scrollY = window.scrollY
            const scrollStart = palygroundRect.top + scrollY
            const scrollEnd = palygroundRect.bottom + scrollY - window.innerHeight
            for (const item of items) {
                animationMap.set(item, getDomAnimation(scrollStart, scrollEnd, item))
            }
        }
        updateMap()
        // 将map结构中的动画应用到元素上
        function 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)

css

.header {
    height: 800px;
    background-color: #f2f2f2;
    display: flex;
    align-items: center;
    justify-content: center;
}

.list {
    position: -webkit-sticky;
    /* Safari */
    position: sticky;/*设置背景粘性定位*/
    top: 0;
    background-color: rgb(0, 0, 0);
    height: 500px;
    z-index: 100;

    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding: 10% 15%;
}

.list-item {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background-color: aquamarine;
    margin: 40px;
}

.palyground {
    height: 1500px;
    background-color: #000000;
    padding: 20px;
}

.footer {
    height: 3000px;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值