Home >Web Front-end >JS Tutorial >GreenSock for Beginners: a Web Animation Tutorial (Part 1)
GreenSock (GSAP) is a high-performance JavaScript animation library for the modern web. This article provides a comprehensive introduction to GSAP's capabilities, core components, and basic usage. It's the first in a multi-part series.
Key Concepts:
TweenLite
, TweenMax
, TimelineLite
, and TimelineMax
, emphasizing their roles in animation creation and sequence management.to()
, from()
, and fromTo()
methods to animate DOM elements between states.play()
, pause()
, reverse()
, restart()
, and resume()
.repeat
, repeatDelay
, and yoyo
for creating cyclical and alternating animations.This article is part of a series, Beyond CSS: Dynamic DOM Animation Libraries, which explores JavaScript animation libraries. Previous articles covered Anime.js, KUTE.js, and Velocity.js.
This first part covers GSAP's capabilities, licensing, core components, and basic tweening syntax. Subsequent parts will delve into timeline functionality and advanced plugins.
What is GreenSock and its Applications?
GSAP is a leading JavaScript animation platform, built on a foundation of Flash animation expertise. It provides a comprehensive toolkit for handling diverse web animation challenges, including SVG animation, complex sequences, dragging interactions, and text manipulation.
Why Choose GSAP?
Core GSAP Modules:
TweenLite
: The core animation engine.TweenMax
: An extension of TweenLite
, including TimelineLite
, TimelineMax
, and various plugins.TimelineLite
: For managing multiple tweens and timelines.TimelineMax
: An enhanced version of TimelineLite
with additional features.GSAP also offers paid plugins (accessible via Club GreenSock) for advanced effects. However, free CodePen examples are available for testing.
Licensing:
GSAP uses a dual licensing model: a free Standard License for free digital products and a paid Business Green license for commercial projects. Despite not being open-source (MIT), GSAP encourages learning by providing access to its source code.
Tweening with GreenSock:
A basic GSAP tween animates a property over time. The core methods are:
TweenMax.to()
: Animates from the current value to a specified end value.TweenMax.from()
: Animates from a specified start value to the current value.TweenMax.fromTo()
: Animates from a specified start value to a specified end value.Including GSAP:
Add the following
npm install gsap
(Use npm for project management:
TweenMax.to()
A basic
<code class="language-javascript">TweenMax.to('.my-element', 1, { opacity: 0 });</code>
This fades out an element with the class "my-element" over one second.
Animating CSS Properties:
backgroundColor
GSAP supports nearly all CSS animatable properties. Use camelCase for property names (e.g.,
set()
Method:
set()
The
<code class="language-javascript">TweenMax.set(element, { rotation: -45 });</code>
Creating Animation Sequences:
autoAlpha
You can create sequences by chaining tweens, adjusting durations and delays. The ease
property controls both opacity and visibility. The
Staggering Animations:
staggerTo()
GSAP's staggerFrom()
, staggerFromTo()
, and
Controlling Tweens:
play()
Methods like pause()
, reverse()
, restart()
, resume()
, and
yoyo
repeat
, repeatDelay
, and :
These properties allow for cyclical and alternating animations.<script> tag before the closing <code></script>
The above is the detailed content of GreenSock for Beginners: a Web Animation Tutorial (Part 1). For more information, please follow other related articles on the PHP Chinese website!