<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>从左侧渐入</title>
<style>
/* 从左侧渐入 */
div{
background-color: rgb(138, 188, 209);
width: 600px;
height: 100px;
margin: 10px;
}
.fade{
opacity: 0;
transform: translateX(-600px);
}
.left-in {
/* 定义动画 */
animation-duration: 1000ms; /* 动画时间 */
animation-name: left-in; /* 关键帧名字 */
}
/* 定义关键帧 */
@keyframes left-in {
0% {
opacity: 0;
transform: translateX(-600px);
}
100% {
opacity: 1;
transform: translateX(0px);
}
}
.right-in {
/* 定义动画 */
animation-duration: 1000ms; /* 动画时间 */
animation-name: right-in; /* 关键帧名字 */
}
/* 定义关键帧 */
@keyframes right-in {
0% {
opacity: 0;
transform: translateX(600px);
}
100% {
opacity: 1;
transform: translateX(0px);
}
}
</style>
</head>
<body>
<div style="height: 800px">box</div>
<div class="fade fade-left">模块一</div>
<div class="fade fade-left">模块二</div>
<div class="fade fade-left">模块三</div>
<div class="fade fade-left">模块四</div>
<div class="fade fade-right">模块五</div>
<div class="fade fade-right">模块六</div>
<script>
function fadeIn () {
window.onscroll = function () {
// let reClassName = /fade-[^"]*/
// let str = 'fade-right'
// console.log(str.match(reClassName).length);
// //div距离顶部的距离
let fadeInBox = document.getElementsByClassName('fade');
// console.log(typeof fadeInBox);
// console.log(fadeInBox);
// console.log('fade-' + obj.direction);
// console.log(fadeInBox.length);
// console.log(fadeInBox[0].innerHTML);
// console.log(fadeInBox[0].className);
let i = 0;
while (i < fadeInBox.length) {
// console.log(fadeInBox[i].innerHTML);
// 遍历所有匹配的元素
let toBox = fadeInBox[i];
let toTop = toBox.getBoundingClientRect().top;
// 获取每个元素的class全部属性
let boxClass = fadeInBox[i].className;
console.log(boxClass)
setTimeout(function (){
if (toTop<800 && boxClass.includes('fade-left')) {
toBox.classList.add('left-in');
toBox.classList.remove('fade','fade-left');
}else if (toTop<800 && boxClass.includes('fade-right')) {
toBox.classList.add('right-in');
toBox.classList.remove('fade','fade-right');
}
},5000);
i++;
}
// let toTop = fadeInBox.getBoundingClientRect();
// console.log(toTop);
// if (toTop.top<500) {
// console.log('测试', toTop.top)
// fadeInBox.classList.add('left-in')
// }
}
}
fadeIn();
</script>
<!-- <script>-->
<!-- $(function(){-->
<!-- if ($("#SuspensionFrame").offset().top>200) {-->
<!-- console.log($(window).height())-->
<!-- }else {-->
<!-- console.log('dddd')-->
<!-- }-->
<!-- })-->
<!-- </script>-->
</body>
</html>
js
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- HTML 学习笔记 May 9,2017 js运行原理、js开发工具介绍、js程序(hello)、js基本语法、j...
- 相等运算符 相等运算符用来比较两个值是否相等, 如果相等会返回true,否则返回false 使用 == 来做相等运...
- 代码块:一个{}中的语句我们称为一个代码块 用{}语句进行分组,一个中称为是一组语句,要么都执行,要么都不执行 j...
- 代码块: 代码块:一个{}中的语句我们称为一个代码块 用{}语句进行分组,一个中称为是一组语句,要么都执行,要么都...
- JS操作属性: JS换肤: JS操作style属性: JS操作class: JS中括号操作属性: JS函数: JS...