当前位置:首页 > 技术博客 > JavaScript > javascript 滚动插入元素动画特效

javascript 滚动插入元素动画特效

5年前 (2021-08-17)JavaScript1053

监听窗体滚动条,新的元素会以 左移、右移、淡出、淡入 等各种方式插入到文档流中


<head>    
<meta charset="UTF-8">    
<meta name="viewport" content="width=device-width, initial-scale=1.0">    
<title>javascript 滚动插入元素动画特效</title>    
<style>    
* {    
box-sizing: border-box;    
}    
body {    
background-color: #efedd6;    
display: flex;    
flex-direction: column;    
align-items: center;    
justify-content: center;    
margin: 0;    
overflow-x: hidden;    
}    
h1 {    
margin: 10px;    
}    
.box {    
background-color: steelblue;    
color: #fff;    
display: flex;    
align-items: center;    
justify-content: center;    
width: 400px;    
height: 200px;    
margin: 10px;    
border-radius: 10px;    
box-shadow: 2px 4px 5px rgba(0, 0, 0, 0.3);    
transform: translateX(400%);    
transition: transform 0.4s ease;    
}    
.box:nth-of-type(even) {    
transform: translateX(-400%);    
}    
.box.show {    
transform: translateX(0);    
}    
.box h2 {    
font-size: 45px;    
}    
.box img{    
width: 400px;    
height: 200px;    
}    
</style>    
</head>    
<body>    
<div>    
<h1>javascript 滚动插入元素动画特效</h1>    
<div class="box">    
<h2>Content1</h2>    
</div>    
<div class="box">    
<h2>Content</h2>    
</div>    
<div class="box">    
<h2>Content2</h2>    
</div>    
<div class="box">    
<h2>Content3</h2>    
</div>    
<div class="box">    
<h2>Content4</h2>    
</div>    
<div class="box">    
<h2>Content5</h2>    
</div>    
<div class="box">    
<img src="https://www.uxqr.com/background.php?t1.png">    
</div>    
<div class="box">    
<img src="https://www.uxqr.com/background.php?t2.png">    
</div>    
<div class="box">    
<img src="https://www.uxqr.com/background.php?t3.png">    
</div>    
<div class="box">    
<img src="https://www.uxqr.com/background.php?t4.png">    
</div>    
</div>    
<script>    
const boxes = document.querySelectorAll('.box')    
window.addEventListener('scroll', checkBoxes)    
checkBoxes()    
function checkBoxes() {    
const triggerBottom = window.innerHeight / 5 * 4    
boxes.forEach(box => {    
const boxTop = box.getBoundingClientRect().top    
if (boxTop < triggerBottom) {    
box.classList.add('show')    
} else {    
box.classList.remove('show')    
}    
})    
}    
</script>    
</body>

电脑端的朋友可以直接点击上面的【运行代码】查看效果。注:在线预览可能遇到bug,代码保存到本地后可解决!


写在最后:

zblog纯净主题食用方法

css代码:

.block .post {    
transform: translateX(400%);    
transition: transform 0.4s ease;    
}    
.block .show {    
transform: translateX(0);    
}

js代码:

const boxes = document.querySelectorAll('.block .post')    
window.addEventListener('scroll', checkBoxes)    
checkBoxes()    
function checkBoxes() {    
const triggerBottom = window.innerHeight / 5 * 4    
boxes.forEach(box => {    
const boxTop = box.getBoundingClientRect().top    
if (boxTop < triggerBottom) {    
box.classList.add('show')    
} else {    
box.classList.remove('show')    
}    
})    
}


版权声明:本文由 LzxBlog 发布,如需转载请注明出处。

本文链接:https://www.liuzhixi.cn/html/145.html

相关文章

JavaScript 中定义数组和操作数组

JavaScript 中定义数组和操作数组

一.认识数组数组就是某类数据的集合,数据类型可以是整型、字符串、甚至是对象Javascript不支持多维数组,但是因为数组里面可以包含对象(数组也是一个对象),...

百度地图JavaScript简单标点连线

百度地图JavaScript简单标点连线

背景最近在研究轨迹数据的挖掘,第一步就是把轨迹数据在地图上可视化出来,然后再进行后续的算法研究。从一开始懵懂知道百度地图有免费的 API 可以调用,到后面知道还...

禁止F12控制台调试JavaScript

禁止F12控制台调试JavaScript

(function (a) {     return (function (a...

四种标题文字动画特效

四种标题文字动画特效

本页面的标题就是用的其中一种动画特效h2.TitleAnimate b {   display: inlin...

圆形光影mp3音频播放器特效

圆形光影mp3音频播放器特效

原生js css3绘制圆形光影音乐播放器,支持上一首或下一首切换,播放暂停,音量控制,音乐播放进度条控制,音乐列表播放,输入添加音乐播放地址等功能。这是一款很全...

响应式人物介绍列表ui切换特效

响应式人物介绍列表ui切换特效

一款js css3制作响应式的人物介绍图文列表,带左右按钮控制列表滑动切换效果。可用于企业团队人物介绍ui布局。下载链接: https://pan.baidu....

评论列表

南通伍林堂文化传播有限公司

测试评论

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。