找回密码
 立即注册

html+css实现滚动到元素位置显示加载动画效果

2022-7-13 16:23:58 · 站长社区

每次滚动到元素时,都显示加载动画,如何添加?

元素添加初始参数

以上图中的动画为例,添加俩个左右容器,将内容放置在容器内部。

添加初始数据,默认透明度0、左右分别移动100px。

  1. //左侧容器
  2.   .item-leftContainer {
  3.     opacity: 0;
  4.     transform: translateX(-100px);
  5.   }
  6.   //右侧容器
  7.   .item-rightContainer {
  8.     opacity: 0;
  9.     transform: translateX(100px);
  10.   }
复制代码
添加动画数据

在less中添加动画数据。这里只设置了to,也可以省略第1步中的初始参数设置而在动画里设置from。

执行后,透明度由0到1,俩个容器向中间移动100px回到原处。

  1. //动画
  2.   @keyframes showLeft {
  3.     to {
  4.       opacity: 1;
  5.       transform: translateX(0px);
  6.     }
  7.   }
  8.   @keyframes showRight {
  9.     to {
  10.       opacity: 1;
  11.       transform: translateX(0px);
  12.     }
  13.   }
  14.   @keyframes hideLeft {
  15.     to {
  16.       opacity: 0;
  17.       transform: translateX(-100px);
  18.     }
  19.   }
  20.   @keyframes hideRight {
  21.     to {
  22.       opacity: 0;
  23.       transform: translateX(100px);
  24.     }
  25.   }
复制代码
触发动画

页面加载/刷新触发 -在componentDidMount中执行

页面滚动时触发 - 在componentDidMount、componentWillUnmount添加监听/注销页面滚动的事件

校验当前滚动高度与元素的位置差异:

window.pageYOffset(滚动距离) + windowHeight(窗口高度) > leftElement.offsetTop (元素的相对位置)+ parentOffsetTop(父元素的相对位置) + 200

  • 真正的滚动视觉位置 - window.pageYOffset(滚动距离) + windowHeight(窗口高度)
  • 元素距离顶部的高度 - 这里使用了leftElement.offsetTop + parentOffsetTop,原因是父容器使用了absolute绝对定位。如果是正常布局的话,使用元素当前的位置leftElement.offsetTop即可
  • 额外添加200高度,是为了优化视觉体验。当超出200高度时才触发动画

当页面滚动到下方,触发显示动画;当页面重新滚动到上方,触发隐藏动画。

  1. componentDidMount() {
  2.         this.checkScrollHeightAndLoadAnimation();
  3.         window.addEventListener('scroll', this.bindHandleScroll);
  4.     }
  5.     componentWillUnmount() {
  6.         window.removeEventListener('scroll', this.bindHandleScroll);
  7.     }
  8.     bindHandleScroll = (event) => {
  9.         this.checkScrollHeightAndLoadAnimation();
  10.     }
  11.     checkScrollHeightAndLoadAnimation() {
  12.         const windowHeight = window.innerHeight;
  13.         let parentEelement = document.getElementById("softwareUsingWays-container") as htmlElement;
  14.         const parentOffsetTop = parentEelement.offsetTop;
  15.         let leftElement = (parentEelement.getElementsByClassName("item-leftContainer") as htmlCollectionOf<HTMLElement>)[0];
  16.         if (window.pageYOffset + windowHeight > leftElement.offsetTop + parentOffsetTop + 200) {
  17.             leftElement.style.animation = "showLeft .6s forwards" //添加动画
  18.         } else {
  19.             leftElement.style.animation = "hideLeft 0s forwards" //隐藏动画
  20.         }
  21.         let rightElement = (parentEelement.getElementsByClassName(".item-rightContainer") as HTMLCollectionOf<HTMLElement>)[0];
  22.         if (window.pageYOffset + windowHeight > rightElement.offsetTop + parentOffsetTop + 200) {
  23.             rightElement.style.animation = "showRight .6s forwards" //添加动画
  24.         } else {
  25.             rightElement.style.animation = "hideRight 0s forwards" //隐藏动画
  26.         }
  27.     }
复制代码


全部评论 0

每次滚动到元素时,都显示加载动画,如何添加?

元素添加初始参数

以上图中的动画为例,添加俩个左右容器,将内容放置在容器内部。

添加初始数据,默认透明度0、左右分别移动100px。

  1. //左侧容器
  2.   .item-leftContainer {
  3.     opacity: 0;
  4.     transform: translateX(-100px);
  5.   }
  6.   //右侧容器
  7.   .item-rightContainer {
  8.     opacity: 0;
  9.     transform: translateX(100px);
  10.   }
复制代码
添加动画数据

在less中添加动画数据。这里只设置了to,也可以省略第1步中的初始参数设置而在动画里设置from。

执行后,透明度由0到1,俩个容器向中间移动100px回到原处。

  1. //动画
  2.   @keyframes showLeft {
  3.     to {
  4.       opacity: 1;
  5.       transform: translateX(0px);
  6.     }
  7.   }
  8.   @keyframes showRight {
  9.     to {
  10.       opacity: 1;
  11.       transform: translateX(0px);
  12.     }
  13.   }
  14.   @keyframes hideLeft {
  15.     to {
  16.       opacity: 0;
  17.       transform: translateX(-100px);
  18.     }
  19.   }
  20.   @keyframes hideRight {
  21.     to {
  22.       opacity: 0;
  23.       transform: translateX(100px);
  24.     }
  25.   }
复制代码
触发动画

页面加载/刷新触发 -在componentDidMount中执行

页面滚动时触发 - 在componentDidMount、componentWillUnmount添加监听/注销页面滚动的事件

校验当前滚动高度与元素的位置差异:

window.pageYOffset(滚动距离) + windowHeight(窗口高度) > leftElement.offsetTop (元素的相对位置)+ parentOffsetTop(父元素的相对位置) + 200

  • 真正的滚动视觉位置 - window.pageYOffset(滚动距离) + windowHeight(窗口高度)
  • 元素距离顶部的高度 - 这里使用了leftElement.offsetTop + parentOffsetTop,原因是父容器使用了absolute绝对定位。如果是正常布局的话,使用元素当前的位置leftElement.offsetTop即可
  • 额外添加200高度,是为了优化视觉体验。当超出200高度时才触发动画

当页面滚动到下方,触发显示动画;当页面重新滚动到上方,触发隐藏动画。

  1. componentDidMount() {
  2.         this.checkScrollHeightAndLoadAnimation();
  3.         window.addEventListener('scroll', this.bindHandleScroll);
  4.     }
  5.     componentWillUnmount() {
  6.         window.removeEventListener('scroll', this.bindHandleScroll);
  7.     }
  8.     bindHandleScroll = (event) => {
  9.         this.checkScrollHeightAndLoadAnimation();
  10.     }
  11.     checkScrollHeightAndLoadAnimation() {
  12.         const windowHeight = window.innerHeight;
  13.         let parentEelement = document.getElementById("softwareUsingWays-container") as htmlElement;
  14.         const parentOffsetTop = parentEelement.offsetTop;
  15.         let leftElement = (parentEelement.getElementsByClassName("item-leftContainer") as htmlCollectionOf<HTMLElement>)[0];
  16.         if (window.pageYOffset + windowHeight > leftElement.offsetTop + parentOffsetTop + 200) {
  17.             leftElement.style.animation = "showLeft .6s forwards" //添加动画
  18.         } else {
  19.             leftElement.style.animation = "hideLeft 0s forwards" //隐藏动画
  20.         }
  21.         let rightElement = (parentEelement.getElementsByClassName(".item-rightContainer") as HTMLCollectionOf<HTMLElement>)[0];
  22.         if (window.pageYOffset + windowHeight > rightElement.offsetTop + parentOffsetTop + 200) {
  23.             rightElement.style.animation = "showRight .6s forwards" //添加动画
  24.         } else {
  25.             rightElement.style.animation = "hideRight 0s forwards" //隐藏动画
  26.         }
  27.     }
复制代码


热门推荐
您需要登录后才可以回帖 立即登录
说说你的想法......
0
0
0
返回顶部