ima.gif

从左到右,图片在逐渐缩小。


| gsap 和 React实现它

  1. .img-container {
  2. width: 100%;
  3. height: 960px;
  4. position: relative;
  5. overflow: hidden;
  6. &:after {
  7. position: absolute;
  8. content: "";
  9. width: 100%;
  10. height: 100%;
  11. background: #fff;
  12. top: 0;
  13. right: 0;
  14. }
  15. img {
  16. width: 100%;
  17. margin: 0 auto;
  18. }
  19. }
  1. import React, { useRef, useEffect } from "react";
  2. import "./App.min.css";
  3. import People from "./images/people.webp";
  4. import { TimelineLite, Power2 } from "gsap";
  5. import CSSRulePlugin from "gsap/CSSRulePlugin";
  6. const App = () => {
  7. let image = useRef(null);
  8. let container = useRef(null);
  9. let imageReveal = CSSRulePlugin.getRule(".img-container:after"); // 图片after
  10. let tl = new TimelineLite();
  11. /*
  12. 三个步骤
  13. 1. 设置可见
  14. 2. after元素右移
  15. 3. 图片缩小可见
  16. */
  17. useEffect(() => {
  18. tl.to(container, 0, { css: { visibility: "visible" } });
  19. tl.to(imageReveal, 1.4, { width: "0%", ease: Power2.easeInOut });
  20. tl.from(image, 1.4, {
  21. scale: 1.6,
  22. ease: Power2.easeInOut,
  23. delay: -1.4
  24. });
  25. });
  26. return (
  27. <section className="main">
  28. <p>GSAP IMAGE REVEAL</p>
  29. <div className="container" ref={el => (container = el)}>
  30. <>
  31. <div className="img-container">
  32. <img
  33. ref={el => {
  34. image = el;
  35. }}
  36. src={People}
  37. />
  38. </div>
  39. </>
  40. </div>
  41. </section>
  42. );
  43. };
  44. export default App;

展示

ScreenShort.gif