Responsive Follow the Line (GSAP/ScrollMagic)

In this example below you will see how to do a Responsive Follow the Line (GSAP/ScrollMagic) with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by SherriAlexander, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright SherriAlexander ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Responsive Follow the Line (GSAP/ScrollMagic)</title>
  
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">

  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <svg style="display:none;">
   <symbol id="vertline">
      <rect class="line-vert" x="0" y="0" width="6" height="1200" />
   </symbol>
</svg>
<div style="width:100%; height:450px;background-color:yellow;"></div>
<div id="line-group" class="line-group">
   <div class="line-section line-section1">
      <div class="curve-positioner">
         <svg id="section1-line-curved" viewBox="0 0 360 76" width="360" height="76">
            <path class="line-curved" id="lineCurved1" d="M 357 0 C 357 35 333 73 292 73 L 0 73 " />
         </svg>
      </div>
      <div id="lineVert1" class="line-positioner">
         <svg width="6" height="1200">
            <use xlink:href="#vertline" />
         </svg>
      </div>
      <div id="lineImage1" class="line-section-image">
         <img src="https://picsum.photos/325/270/?image=1025" alt="cute pug" />
         <div class="image-sprite"></div>
         <div class="image-sprite"></div>
         <div class="image-sprite"></div>
         <div class="image-sprite"></div>
         <div class="image-sprite"></div>
      </div>
      <div class="line-section-content">
         <h2>Making New Products</h2>
         <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Corporis, voluptatem. In repellat enim aliquid beatae iusto exercitationem maiores nobis ducimus eligendi nisi! Cupiditate alias totam laudantium facilis earum qui molestiae!</p>
         <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Corporis, voluptatem. In repellat enim aliquid beatae iusto exercitationem maiores nobis ducimus eligendi nisi! Cupiditate alias totam laudantium facilis earum qui molestiae!</p>
         <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Corporis, voluptatem. In repellat enim aliquid beatae iusto exercitationem maiores nobis ducimus eligendi nisi! Cupiditate alias totam laudantium facilis earum qui molestiae!</p>
      </div>
   </div>

   
   <div class="line-section line-section2">
      <div class="curve-positioner">
         <svg id="section2-line-curved" viewBox="0 0 360 76" width="360" height="76">
            <path class="line-curved" id="lineCurved2" d="M 3 0 C 3 35 30 73 68 73 L 360 73" />
         </svg>
      </div>
      <div id="lineVert2" class="line-positioner">
         <svg width="6" height="1200">
            <use xlink:href="#vertline" />
         </svg>
      </div>
      <div id="lineImage2" class="line-section-image">
         <img src="https://picsum.photos/325/270/?image=1025" alt="cute pug" />
      </div>
      <div class="line-section-content">
         <h2>Making New Products</h2>
         <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Corporis, voluptatem. In repellat enim aliquid beatae iusto exercitationem maiores nobis ducimus eligendi nisi! Cupiditate alias totam laudantium facilis earum qui molestiae!</p>
         <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Corporis, voluptatem. In repellat enim aliquid beatae iusto exercitationem maiores nobis ducimus eligendi nisi! Cupiditate alias totam laudantium facilis earum qui molestiae!</p>
      </div>
   </div>

   
   <div class="line-section line-section3">
      <div class="curve-positioner">
         <svg id="section3-line-curved" viewBox="0 0 360 76" width="360" height="76">
            <path class="line-curved" id="lineCurved3" d="M 357 0 C 357 35 333 73 292 73 L 0 73" />
         </svg>
      </div>
      <div id="lineVert3" class="line-positioner">
         <svg width="6" height="1200">
            <use xlink:href="#vertline" />
         </svg>
      </div>
      <div id="lineImage3" class="line-section-image">
         <img src="https://picsum.photos/325/270/?image=1025" alt="cute pug" />
      </div>
      <div class="line-section-content">
         <h2>Making New Products</h2>
         <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Corporis, voluptatem. In repellat enim aliquid beatae iusto exercitationem maiores nobis ducimus eligendi nisi! Cupiditate alias totam laudantium facilis earum qui molestiae!</p>
         <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Corporis, voluptatem. In repellat enim aliquid beatae iusto exercitationem maiores nobis ducimus eligendi nisi! Cupiditate alias totam laudantium facilis earum qui molestiae!</p>
         <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Corporis, voluptatem. In repellat enim aliquid beatae iusto exercitationem maiores nobis ducimus eligendi nisi! Cupiditate alias totam laudantium facilis earum qui molestiae!</p>
      </div>
   </div>

   
   <div class="line-section line-section4">
      <div class="curve-positioner">
         <svg id="section4-line-curved" viewBox="0 0 360 76" width="360" height="76">
            <path class="line-curved" id="lineCurved4" d="M 3 0 C 3 35 30 73 68 73 L 360 73" />
         </svg>
      </div>
      <div id="lineVert4" class="line-positioner">
         <svg width="6" height="1200">
            <use xlink:href="#vertline" />
         </svg>
      </div>
      <div id="lineImage4" class="line-section-image">
         <img src="https://picsum.photos/325/270/?image=1025" alt="cute pug" />
         <div class="image-sprite"></div>
         <div class="image-sprite"></div>
         <div class="image-sprite"></div>
         <div class="image-sprite"></div>
         <div class="image-sprite"></div>
      </div>
      <div class="line-section-content">
         <h2>Making New Products</h2>
         <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Corporis, voluptatem. In repellat enim aliquid beatae iusto exercitationem maiores nobis ducimus eligendi nisi! Cupiditate alias totam laudantium facilis earum qui molestiae!</p>
         <p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Corporis, voluptatem. In repellat enim aliquid beatae iusto exercitationem maiores nobis ducimus eligendi nisi! Cupiditate alias totam laudantium facilis earum qui molestiae!</p>
      </div>
   </div>
</div>
<div style="width:100%; height:450px;background-color:yellow;"></div>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.3/TweenMax.min.js'></script>
<script src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/16327/DrawSVGPlugin.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.5/ScrollMagic.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.5/plugins/animation.gsap.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/ScrollMagic/2.0.5/plugins/debug.addIndicators.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/SherriAlexander/responsive-follow-the-line-gsapscrollmagic-geeBYG */
/* Colors */
.line-group {
  padding-left: 50px;
  padding-right: 50px;
}

.line-section {
  position: relative;
  padding-top: 44px;
  padding-bottom: 44px;
  max-width: 1050px;
  margin-left: auto;
  margin-right: auto;
  min-height: 350px;
}

.line-positioner {
  position: absolute;
  top: 0;
  left: 50%;
  bottom: 0;
  overflow: hidden;
  -webkit-transform: translateX(-50%);
          transform: translateX(-50%);
}

.curve-positioner {
  position: absolute;
  top: 33%;
  right: 50%;
  -webkit-transform: translateX(3px);
          transform: translateX(3px);
}

.line-section-image {
  position: absolute;
  left: 0;
  top: 33%;
  margin-top: 76px;
  -webkit-transform: translateY(-50%);
          transform: translateY(-50%);
  width: 30%;
  max-width: 325px;
  visibility: hidden;
  opacity: 0;
}
.line-section-image img {
  max-width: 100%;
  height: auto;
}
.line-section-image .image-sprite {
  position: absolute;
  width: 40px;
  height: 40px;
  background-color: #0097cd;
  border-radius: 50%;
  left: 0;
}
.line-section-image .image-sprite:nth-of-type(2n+2) {
  left: 50px;
  background-color: #42beac;
}
.line-section-image .image-sprite:nth-of-type(3n+3) {
  left: 100px;
  background-color: #70be49;
}
.line-section-image .image-sprite:nth-of-type(4n+4) {
  left: 150px;
  background-color: #ffc52f;
}
.line-section-image .image-sprite:nth-of-type(5n+5) {
  left: 200px;
  background-color: #f78d2c;
}

.line-section-content {
  position: relative;
  margin-left: 50%;
  padding-left: 50px;
}

.line-section:nth-child(even) .curve-positioner {
  right: auto;
  left: 50%;
  -webkit-transform: translateX(-3px);
          transform: translateX(-3px);
}
.line-section:nth-child(even) .line-section-content {
  margin-right: 50%;
  margin-left: 0;
  padding-left: 0;
  padding-right: 50px;
}
.line-section:nth-child(even) .line-section-image {
  left: auto;
  right: 0;
}

path {
  stroke-width: 6;
  fill: none;
}

use {
  fill: #0097cd;
}


/*Downloaded from https://www.codeseek.co/SherriAlexander/responsive-follow-the-line-gsapscrollmagic-geeBYG */
// Overall DOM elements
var $lineContainer = jQuery("#line-group");
var $lineSections = $lineContainer.find(".line-section");
var $images = jQuery(".line-section-image");
var $sprites = jQuery(".image-sprite");
var $svgPaths = jQuery("path");
var $svgRects = jQuery("rect");

console.log("Number of svg rects: " + $svgRects.length);

var numberOfSections = $lineSections.length;

var sectionColors = [
   "#0097cd",
   "#70be49",
   "#f78d2c",
   "#8b244f",
   "#f05022",
];

// Animation variables
var timeLong = 1;
var timeShort = 0.5;
var straightEase = "Power0.easeNone";
var curvingEase = "Power1.easeOut";
var imageEase = "Back.easeOut";
var imageSpriteEase = "Elastic.easeOut";
var topicTimelines = [];
var topicScenes = [];
var topicImageScenes = [];

// Setting up the master timeline
var masterTL = new TimelineLite();

// Setting up initial states
masterTL
   .set(".line-curved", {drawSVG:"0%"})
   .set($svgPaths, {stroke: sectionColors[0]})
   .set($svgRects, {fill: sectionColors[0]})
   .set($sprites, {scale:"0"});

// Section In
function sectionIn(num) {
   var lineVertID = "#lineVert" + num;
   var lineCurvedID = "#lineCurved" + num;
   var prevLineVertID = "#lineVert" + (num - 1);
   var thisSection = "section" + num;
   
   var zeroBasedNum = num - 1;
   topicTimelines[zeroBasedNum] = new TimelineLite();

   // If this isn't the first section, we'll animate the rest of the previous section's line first
   if (num != 1) {
      topicTimelines[zeroBasedNum].add(function() { console.log("Start color change!"); changeLineColor(num) });
      topicTimelines[zeroBasedNum].to(prevLineVertID, timeShort, {css: {bottom: "0"}, ease:straightEase, force3D:false});
   }
   
   // Animate:
   // 1. the top of this section's vertical line
   // 2. the curved line
   topicTimelines[zeroBasedNum]
   .fromTo(lineVertID, timeShort, {css:{bottom: "100%"}}, {css: {bottom:"66%"}, ease:straightEase, force3D:false})
   .to(lineCurvedID, timeShort, {drawSVG:"100%", ease:curvingEase, force3D:false})
   
   return topicTimelines[zeroBasedNum];
}

// Line color change
function changeLineColor(num) {
   var sectionIndex = num - 1;
   var sectionColor = sectionColors[sectionIndex];

   TweenLite.to($svgPaths, 1.5, {stroke: sectionColor, ease:straightEase});
   TweenLite.to($svgRects, 1.5, {fill: sectionColor, ease:straightEase});
}

// Image Pop function
function imageIn(num) {
   var tl = new TimelineLite();
   var imageID = "#lineImage" + num;
   tl.to(imageID, timeShort, {autoAlpha: 1, ease:imageEase});
   tl.from(imageID, timeShort, {scale:"0",  ease:imageEase, clearProps:"transform"}, 0);
   
   // Bubble in subsprites, if there are any
   var jQueryString = "#lineImage" + num + " > .image-sprite";
   var $bubbleSprites = jQuery(jQueryString);
   if ($bubbleSprites.length > 0) {
      tl.add(bubbleIn($bubbleSprites));
   }
   
   return tl;
}

// Generate random number within a range
function getRandomRange(min, max) {
  return Math.random() * (max - min) + min;
}

// "Bubble in" a collection of items
function bubbleIn(items) {
   var tl = new TimelineLite();
   items.each(function() {
      tl.to(this, 0.3, {scale:"1", ease:imageEase}, getRandomRange(0.01, 0.5));
   });
   return tl;
}

// Set up ScrollMagic
var controller = new ScrollMagic.Controller();

var $sections = jQuery(".line-section");
var numberOfSections = $sections.length;

$sections.each(function(index) {
   //console.log("Looping through sections!  This is index: " + index);
   var loopIndex = index;
   var sceneNumber = loopIndex + 1;
   var $this = jQuery(this);
   var sceneDuration = $this.outerHeight() / 2;
   //console.log("This scene's duration: " + sceneDuration);
   
   topicScenes[index] = new ScrollMagic.Scene({
      triggerElement: this,
      duration: sceneDuration
   })
   .addIndicators()
   .addTo(controller)
   .setTween(sectionIn(sceneNumber));
   
   var imagePopStart = sceneDuration - 100;
   
   topicImageScenes[index] = 
   // No duration and different offset for the image pops
   topicImageScenes[index] = new ScrollMagic.Scene({
       triggerElement: this,
       offset:imagePopStart
   })
   .addTo(controller)
   .setTween(imageIn(sceneNumber));

});

Comments