原生JS实现随着鼠标滚动到元素位置触发对应css3动画,简单易用滚动监测

 <style>
body{overflow-x: hidden;width:80%;margin:auto;}
body:after {
content: ".";
clear: both;
display: block;
}
div{width:32%;height: 20vw;background: #e3e4e5;margin-right:2%;box-sizing: content-box;margin-top:1.6vw;float:left;}
div:nth-of-type(3n+3) {
margin-right:;
}
@keyframes f-up {
0% {
will-change: transform;/*优化动画卡顿1*/
/*position:relative;优化动画卡顿2*/
/*z-index: 1;优化动画卡顿2*/
opacity:; /*初始状态 透明度为0*/
transform:translateY(100px);/*初始状态 文档流下100px*/
}
100% {
opacity:; /*结尾状态 透明度为1*/
transform:translateY(0px);/*初始状态 恢复正常位置*/
}
}
.f-up{
animation-name: f-up; /*动画名称*/
animation-duration: 0.8s; /*动画持续时间*/
animation-iteration-count:; /*动画次数*/
animation-delay: 0s; /*延迟时间*/
}
</style>
 <body>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<div class="vanwee"></div>
<script charset="utf-8" src="//blog-static.cnblogs.com/files/vanwee/fade.js"></script>
</body>
 <script>
// 获取浏览器可见区域高度
var window_height= document.documentElement.clientHeight;
// 用户手动修改浏览器可见区域高度时修改变量
window.onresize=function () {window_height=document.documentElement.clientHeight;};
// 获取所需效果元素
var My_vanwee=document.getElementsByClassName('vanwee');
// 鼠标滚轮滚动执行方法
window.onscroll = function(){
// 获取鼠标滚轮滚动距离
var _scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
// 循环类vanwee
for (var k=0;k<My_vanwee.length;k++){
if (My_vanwee[k].classList.contains("f-up")==false && _scrollTop >= getOffsetTop(My_vanwee[k]) - window_height && _scrollTop<= getOffsetTop(My_vanwee[k])){
My_vanwee[k].classList.add("f-up");
}
}
};
// 判断元素父集是否有已定位元素
function getOffsetTop(ele) {
var rtn = ele.offsetTop;
var o = ele.offsetParent;
while(o!=null) {
rtn += o.offsetTop;
o = o.offsetParent;
}
return rtn;
}
// 滚动条等于0时执行第一屏效果
function my_animation() {
var _scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
// 效果方法
for (var k=0;k<My_vanwee.length;k++) {
if (My_vanwee[k].classList.contains("f-up")==false && _scrollTop>= getOffsetTop(My_vanwee[k]) - window_height && _scrollTop<= getOffsetTop(My_vanwee[k])){
My_vanwee[k].classList.add("f-up");
}
}
}
my_animation();
</script>

 使用方法

在想要添加动画的元素上添加class类名:vanwee

由于我只需要从下渐现向上移动的效果,所以从原博主转载过来做了调整,大家有不明白的地方建议查看转载来源博主的详细解说,或者查看本博文最下方的原文复制内容。测试只兼容IE10及以上版本,应该是JS第14行.contains的原因。我魅族手机浏览器竖着看的时候有点闪烁,其他手机和电脑设备均正常。欢迎大佬看到支援。。

JS文件:https://blog-static.cnblogs.com/files/vanwee/fade.js

转载来源:https://blog.csdn.net/likeyou1207/article/details/80782935

原博文内容:

废话不多说,直接上代码,附用法,纯博主手写,看完后,觉得好左上角请点个赞,觉得不好的请留下您的建议,谢谢(此代码改版很方便,大家可以任意发挥)

(注意:如果不想自己写动画的,可以引入Animate.css  相信这个动画库大家都不陌生)

 body{overflow-x: hidden;}
div{width: 100px;height: 100px;margin: 100px auto 0;background: red;}
 <body>
    <div class="dream" data-animate="bounceInUp,1s,linear"></div>
    <div class="dream" data-animate="flash,2s,linear"></div>
    <div class="dream" data-animate="pulse,1s,linear"></div>
    <div class="dream" data-animate="rubberBand,1s,linear"></div>
    <div class="dream" data-animate="shake,1s,linear"></div>
    <div class="dream" data-animate="swing,1s,linear"></div>
    <div class="dream" data-animate="tade,1s,linear"></div>
    <div class="dream" data-animate="wobble,1s,linear"></div>
    <div class="dream" data-animate="jello,1s,linear"></div>
    <div class="dream" data-animate="bounceIn,1s,linear"></div>
    <div class="dream" data-animate="bounceInDown,1s,linear"></div>
    <div class="dream" data-animate="bounceInLeft,1s,linear"></div>
    <div class="dream" data-animate="bounceInRight,1s,linear"></div>
    <div class="dream" data-animate="bounceInUp,1s,linear"></div>
    <div class="dream" data-animate="bounceOut,1s,linear"></div>
    <div class="dream" data-animate="bounceOutDown,1s,linear"></div>
    <div class="dream" data-animate="bounceOutLeft,1s,linear"></div>
    <div class="dream" data-animate="bounceOutRight,1s,linear"></div>
    <div class="dream" data-animate="bounceOutUp,1s,linear"></div>
    <div class="dream" data-animate="fadeIn,1s,linear"></div>
    <div class="dream" data-animate="fadeInDown,1s,linear"></div>
    <div class="dream" data-animate="fadeInLeft,1s,linear"></div>
    <div class="dream" data-animate="fadeInRight,1s,linear"></div>
    <div class="dream" data-animate="fadeInUp,1s,linear"></div>
    <div class="dream" data-animate="fadeInDownBig,1s,linear"></div>
    <div class="dream" data-animate="fadeInLeftBig,1s,linear"></div>
    <div class="dream" data-animate="fadeInRightBig,1s,linear"></div>
    <div class="dream" data-animate="fadeInUpBig,1s,linear"></div>
    <div class="dream" data-animate="flip,1s,linear"></div>
    <div class="dream" data-animate="hinge,1s,linear"></div>
</body>
 <script>
// 获取浏览器可见区域高度
var window_height= document.documentElement.clientHeight;
// 用户手动修改浏览器可见区域高度时修改变量
window.onresize=function () {
window_height=document.documentElement.clientHeight;
};
// 获取所需效果元素
var My_dream=document.getElementsByClassName('dream');
// 鼠标滚轮滚动执行方法
window.onscroll = function(){
// 获取鼠标滚轮滚动距离
var _scrollTop = document.body.scrollTop || document.documentElement.scrollTop; // 循环类dream
for (var k=0;k<My_dream.length;k++){
if (_scrollTop>= getOffsetTop(My_dream[k]) - window_height && _scrollTop<= getOffsetTop(My_dream[k]) ){
My_dream[k].style.animationName=My_dream[k].dataset.animate.split(',')[0];
My_dream[k].style.animationDuration=My_dream[k].dataset.animate.split(',')[1];
My_dream[k].style.animationTimingFunction=My_dream[k].dataset.animate.split(',')[2];
}
}
}; // 判断元素父集是否有已定位元素
function getOffsetTop(ele) {
var rtn = ele.offsetTop;
var o = ele.offsetParent;
while(o!=null)
{
rtn += o.offsetTop;
o = o.offsetParent;
}
return rtn;
} // 滚动条等于0时执行第一屏效果
function my_animation() {
var _scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
// 效果方法
for (var k=0;k<My_dream.length;k++){
if (_scrollTop>= getOffsetTop(My_dream[k]) - window_height && _scrollTop<= getOffsetTop(My_dream[k]) ){
My_dream[k].style.animationName=My_dream[k].dataset.animate.split(',')[0];
My_dream[k].style.animationDuration=My_dream[k].dataset.animate.split(',')[1];
My_dream[k].style.animationTimingFunction=My_dream[k].dataset.animate.split(',')[2];
}
}
}
my_animation();
</script>

用法很简单:

1)注意:没有自己写动画效果时,记得引入animate.css动画库

2)在想要添加动画的元素上添加class类名 dream

3)然后添加自定义属性 ( data-animate="动画名称,动画时间,动画速率" ),注意中间有 ","号隔开

4)加上后查看效果即可,使用前可以先复制博主代码查看下效果

(您的评价是对博主最大的鼓励,欢迎评价,觉得好左上角请点个赞,觉得不好的请留下您的建议,谢谢 (* ̄︶ ̄)  )

如果您还是觉得很模糊的,请留下您的联系方式,博主会在一天内给您回复
---------------------
作者:likeYou1207
来源:CSDN
原文:https://blog.csdn.net/likeyou1207/article/details/80782935
版权声明:本文为博主原创文章,转载请附上博文链接!

友情链接:

上海网页设计公司

上一篇:Linux下搭建企业共享目录方案之------samba


下一篇:CocoaPods的安装和使用那些事(Xcode 7.2,iOS 9.2,Swift)