ScrollMagic 是一个强大的 JavaScript 库,可以帮助开发者在页面滚动时触发各种动画效果。它支持复杂的滚动交互,非常适合制作富交互的网页。
这里他使用了ScrollMagic
的几种滚动效果:
demo:
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
<style>
body {
margin: 0;
padding: 0;
}
.section {
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
font-size: 3rem;
font-weight: bold;
color: white;
}
.section-1 {
background-color: #4CAF50;
}
.section-2 {
background-color: #2196F3;
}
.section-3 {
background-color: #E91E63;
}
.animated-element {
width: 200px;
height: 200px;
background-color: white;
border-radius: 50%;
/* -100%就是从下往上 */
transform: translateY(100%);
box-shadow: 0 0 20px rgba(0, 0, 0, 0.3);
opacity: 0;
}
style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js">script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.8/ScrollMagic.min.js">script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.7.1/gsap.min.js">script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.8/plugins/animation.gsap.min.js">script>
<script>
// 创建 ScrollMagic 控制器
$(document).ready(function () {
// 创建 ScrollMagic 控制器
var controller = new ScrollMagic.Controller();
// 定义滚动动画场景
var scene1 = new ScrollMagic.Scene({
triggerElement: ".section-1",
// 从不透明
triggerHook: 0.5
})
.setTween(".section-1 .animated-element", {
// transform: translate(100%, 0px);
x: "100%",
// 到透明
opacity: 1,
// 平滑自然的移动
ease: Power2.easeOut
})
.addTo(controller);
var scene2 = new ScrollMagic.Scene({
triggerElement: ".section-2",
triggerHook: 0.5
})
.setTween(".section-2 .animated-element", {
y: "0",
opacity: 1,
ease: Power2.easeOut
})
.addTo(controller);
var scene3 = new ScrollMagic.Scene({
triggerElement: ".section-3",
triggerHook: 0.5
})
.setTween(".section-3 .animated-element", {
x: "100%",
opacity: 1,
ease: Power2.easeOut
})
.addTo(controller);
})
script>
head>
<body>
<div class="section section-1">
<div class="animated-element">
<h2>Section 1h2>
div>
div>
<div class="section section-2" style="align-items: flex-start;">
<div class="animated-element">
<h2>Section 2h2>
div>
div>
<div class="section section-3">
<div class="animated-element">
<h2>Section 3h2>
div>
div>
body>
html>