使用javascript和CSS3创建滚动触发的图标动画

所属分类: 网页特效-动画效果    2024-01-03 10:41:04

使用javascript和CSS3创建滚动触发的图标动画 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

使用javascript和CSS3创建滚动触发的图标动画(共4个文件)

    • thumbnail.gif
    • motionscroll.min.js
    • motionscroll.js
    • index.html

使用方法

/*!
 * JavaScript scroll animation library
 *
 * Copyright (c) 2023 @louisho5
 * Under the MIT license.
 *
 * @version 0.1.0
 */

"use strict";

class MotionScroll {
  constructor(options = {}) {
    this.options = options;

    if (typeof options === 'string') {
      this.options = { container: options };
    } else {
      this.options = options;
    }

    if (this.options.container == undefined) {
      this.options.container = '.motionscroll';
    }
    if (this.options.layer == undefined) {
      this.options.layer = '[data-motionscroll-to],[data-motionscroll-animate]';
    }
    if (this.options.ease == undefined) {
      this.options.ease = 'linear';
    }
    if (this.options.offsetTop == undefined) {
      this.options.offsetTop = 0;
    }
    if (this.options.offsetBottom == undefined) {
      this.options.offsetBottom = 0;
    }
    if (this.options.override == undefined) {
      this.options.override = false;
    }
    if (this.options.reverse == undefined) {
      this.options.reverse = true;
    }
    if (this.options.playOnce == undefined) {
      this.options.playOnce = false;
    }
    this.options.override = (this.options.override) ? " !important" : "";
    this.destroy = this.destroy.bind(this);
    this.init = this.init.bind(this);
    document.addEventListener("DOMContentLoaded", this.init);
  }

  destroy() {
    var motionscrollContainer = this.options.container;
    var motionscrollLayer = this.options.layer;

    var motionscroll = document.querySelectorAll(motionscrollContainer);
    motionscroll.forEach(function(msContainer){
      var motionscrollLayers = msContainer.querySelectorAll(motionscrollLayer);
      motionscrollLayers.forEach(function(msLayer) {
        msLayer.classList.forEach(function(className) {
          if (className.startsWith("_motionscroll-animate-")) {
            msLayer.classList.remove(className);
          }
        });
      });
    });
    window.removeEventListener("beforeunload", this.destroy);
  }

  init() {
    var motionscrollContainer = this.options.container;
    var motionscrollLayer = this.options.layer;
    var easeMode = this.options.ease;
    var offsetTop = this.options.offsetTop;
    var offsetBottom = this.options.offsetBottom;
    var override = this.options.override;
    var reverse = this.options.reverse;
    var playOnce = this.options.playOnce;
    var played = false;
    var generatedNumbers = new Set();
    function generateRandomPrefixNumber() {
      var prefix, number, paddedNumber, randomPrefixNumber;
      do {
        prefix = Math.floor(Math.random() * 256);
        number = Math.floor(Math.random() * 65536);
        paddedNumber = number.toString(16).padStart(4, '0');
        randomPrefixNumber = 'ms' + prefix.toString(16) + paddedNumber;
      } while (generatedNumbers.has(randomPrefixNumber));
        generatedNumbers.add(randomPrefixNumber);
      return randomPrefixNumber;
    }

        var motionscroll = document.querySelectorAll(motionscrollContainer);
        motionscroll.forEach(function(msContainer){
          var motionscrollLayers = msContainer.querySelectorAll(motionscrollLayer);
          motionscrollLayers.forEach(function(msLayer) {
            var dataMsTo = msLayer.getAttribute('data-motionscroll-to') || null;
            var dataMsAnimate = msLayer.getAttribute('data-motionscroll-animate') || null;
            var prefixNumber = generateRandomPrefixNumber();

            var msStyle = `
            <style>
            ._motionscroll-animate-${prefixNumber} {
              animation: ${prefixNumber}-animate 1s ${easeMode} infinite${override};
              animation-play-state: paused${override};
              animation-delay: calc(var(--${prefixNumber}-progress) * -1s)${override};
              animation-iteration-count: 1${override};
              animation-fill-mode: both${override};
            }`;

            if (dataMsAnimate) {
              msStyle += `
              @keyframes ${prefixNumber}-animate {
                ${dataMsAnimate}
              }
              </style>`;
            } else if (dataMsTo) {
              msStyle += `
              @keyframes ${prefixNumber}-animate {
                to {
                  ${dataMsTo}
                }
              }
              </style>`;
            }
            document.head.insertAdjacentHTML("beforeend", msStyle);

            msLayer.classList.add(`_motionscroll-animate-${prefixNumber}`);
            
            //var start = window.innerHeight - msContainer.offsetTop + offsetTop;
            var viewportHeight = window.innerHeight;
            window.addEventListener('scroll', function() {
              var currentPosition = window.innerHeight - (msContainer.offsetTop - window.pageYOffset);
              var viewportPercentage = Math.max(0, Math.min(100, Math.round((currentPosition) / (viewportHeight + msContainer.offsetHeight) * 100)));
              var percentage = Math.max(0, Math.min(100, Math.round((currentPosition - offsetTop) / (viewportHeight + msContainer.offsetHeight - offsetTop - offsetBottom) * 100)));
              if(playOnce && percentage >= 99) {
                msContainer.style.setProperty(`--${prefixNumber}-progress`, 1);
                played = true;
              } else if(playOnce && !played) {
                msContainer.style.setProperty(`--${prefixNumber}-progress`, percentage/100);
              } else if(!playOnce && reverse) {
                if(percentage >= 99) {
                  msContainer.style.setProperty(`--${prefixNumber}-progress`, 1);
                } else {
                  msContainer.style.setProperty(`--${prefixNumber}-progress`, percentage/100);
                }
              } else if(!playOnce && !reverse) {
                if(percentage >= 99) {
                  msContainer.style.setProperty(`--${prefixNumber}-progress`, 1);
                  played = true;
                } else if(viewportPercentage <= 1) {
                  msContainer.style.setProperty(`--${prefixNumber}-progress`, 0);
                  played = false;
                } else if(!played) {
                  msContainer.style.setProperty(`--${prefixNumber}-progress`, percentage/100);
                }
              }
            });

            
        });
    });

    window.dispatchEvent(new Event('scroll'));
    
    document.addEventListener("resize", function(){
      window.dispatchEvent(new Event('scroll'));
    });
  }
}

站长提示:
1. 苦力吧素材官方QQ群:950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励2K币
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 动画效果

jquery根据鼠标进入的方向触发波纹动画特效

一款鼠标悬停触发的波纹动画特效代码,根据鼠标进入button元素的方向,会出现一种奇特的、有方向意识的波纹式悬停效果。
  动画效果
 3227  0

js鼠标点击页面触发烟花动画特效

一款轻量级鼠标单击事件动画特效代码,当用户点击或点击时,网页自动触发烟花散开动画效果。
  动画效果
 3177  0

原生js控制gif图片停止与播放效果

鼠标单击可暂停、播放的GIF动态图片插件,代码精简高效!
  动画效果
 3558  0

jquery带多种文本文字切换动画特效代码

一款文本文字切换动画插件,包含7种CSS3支持的平滑过渡效果,非常的漂亮。
  动画效果
 2229  0

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论