Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="WRAPPER">
  <div id="ANIMATED_GRADIENT"></div>
  <div id="TOP_BAR1" class="bar">
    
    <div class="ball first">1</div>
    <div class="ball"></div>
    <div class="ball"></div>
    <div class="ball"></div>
    <div class="ball"></div>
    <div class="ball"></div>
    <div class="ball"></div>
    <div class="ball"></div>
    <div class="ball"></div>
    <div class="ball last">9</div>
    
    <div class="btn active">Button1</div>
    <div class="btn">Button2</div>
    <div class="btn">Button3</div>
    <div class="btn">Button4</div>
  </div>
<!--   <div id="TOP_BAR2" class="bar">
    
  </div> -->
</div>
              
            
!

CSS

              
                //colors
$blue10: rgba(48,203,226,0.2);
$blue11: rgba(48,203,226,0.4);
$blue12: rgba(48,203,226,0.8);
$blue13: rgba(48,203,226,1);
$blue21: rgba(65,102,245,0.4);
$blue22: rgba(65,102,245,0.8);
$blue23: rgba(65,102,245,0.1);
$blue31: rgba(25,25,112,0.4);
$blue32: rgba(25,25,112,0.8);
$blue33: rgba(25,25,112,1);

$barBorder: rgba(17,19,20,0.4);
$barBg: rgba(83,104,120,0.6);

//keyframes
@keyframes bgAnimation { 
    0%{background-position:90% 0%}
    50%{background-position:10% 100%}
    100%{background-position:90% 0%}
}


html {
  width: 100%;
  height: 100%;
  font-size: 10px;
  body {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    display: flex;
    flex-flow: column nowrap;
    #WRAPPER {
      position: relative;
      flex: 1 0 auto;
      width: 100%;
      min-width: 50rem;
      height: 100%;
      margin: 0;
      background-image: 
        linear-gradient(150deg,transparent 20%, $blue12 170%),
        linear-gradient(180deg,$blue32 10%, $blue23 100%),
        linear-gradient(120deg,$blue33 10%, $blue23 120%),
        linear-gradient(165deg,$blue32 10%, transparent 120%),
        linear-gradient(135deg,$blue32 10%, transparent 120%),
        linear-gradient(60deg,$blue32 -30%, transparent 50%, $blue33 120%);    
      #ANIMATED_GRADIENT {
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        background: linear-gradient(60deg,$blue32 -30%, $blue10 0%, $blue23 30%, $blue10 60%, $blue22 90%, $blue10 120%, $blue33 150%);
        background-size: 1200% 1200%;
        animation: bgAnimation 30s ease infinite;
      }
      .bar {
        position: relative;
        border: 1px solid $barBorder;
        border-radius: 0.4rem;
        background-color: $barBg;
        width: 100%;
        height: 8rem;
        margin-top: 1rem;
        // margin-left: calc(5% - 1px);
        .ball {
          position: absolute;
          background-color: rgba(191,255,0,1);
          width: 0.8rem;
          height: 0.8em;
          margin: 0.1rem;
          border-radius: 100%;
          &.first {
            background-color: red;
          }
          &.last {
            background-color: black;
          }
        }
        .btn {
          border-radius: inherit;
          box-shadow: 0px 2px 2px -1px $barBorder,
            inset 0 3px 4px -2px rgba(230,230,250,0.2);
          height: 2rem;
          width: 12rem;
          margin-bottom: 0.5rem;
          margin-left: 1rem;
          padding-top: 0.5rem;
          text-align: center;
          font-size: 1.4rem;
          font-weight: bold;
          cursor: pointer;
          &:last-child {
            margin-right: 1rem; 
          }
        }
      }
      #TOP_BAR1 {
        display: flex;
        flex-flow: row nowrap;
        align-items: flex-end;
        justify-content: space-around;
        min-width: 50rem;
      }
      #TOP_BAR2 {
        
      }
    }
  }
}
              
            
!

JS

              
                class Vector2D {
  x;
  y;
  constructor(x,y){
    this.x = x;
    this.y = y;
  }
  add(x?,y?){
    this.x += x;
    this.y += y;
  }
}

class Ball {
  elem;
  index;
  r;
  position;
  timeline;
  constructor(elem,index,r,position){
    this.elem = elem;
    this.index = index;
    this.r = r;
    this.position = position;
    this.timeline = new TimelineMax();
  }
  
  // set tweens props
  // return Object
  setTweenProps( activeBtn, clickedBtn, direction, speed, h) {
    console.log("setTweenProps")
    console.log("this.position: " + JSON.stringify(this.position));
    
    let activeRect = activeBtn.getBoundingClientRect(),
        clickedRect = clickedBtn.getBoundingClientRect();
    
    let dist1 = direction === "right"
              ? ( activeRect.right - this.position.x - this.r ).toFixed(2)
              : ( this.position.x - activeRect.left ).toFixed(2), 
      dist2 = direction === "right"
              ? ( clickedRect.left - activeRect.right ).toFixed(2)
              : ( activeRect.left - clickedRect.right ).toFixed(2),
      dist3 = direction === "right"
        ? ( (this.index + 1) * 10  + 4 ).toFixed(2)
        : ( (clickedRect.width - (this.index + 1) * 10) + 4 ).toFixed(2),
      dur1 = ( dist1/speed ).toFixed(2),
      dur2 = ( dist2/speed ).toFixed(2),
      dur3 = ( dist3/speed ).toFixed(2);
    return {
      dist1, dist2, dist3, dur1, dur2, dur3, h
    };
  }
  
  setTween(direction, props) {
     console.log("setTween " + direction);
     console.log("ball.index:  " + this.index + " color: " + this.elem.innerHTML);
     this.timeline.clear();
    
     this.timeline.eventCallback("onComplete", function(tl, _this) {
      
       _this.position.x += tl._last.target._gsTransform.x;
       _this.position.y += tl._last.target._gsTransform.y - this.r;
       _this.elem.style.top = _this.position.y + "px";
       _this.elem.style.left = _this.position.x + "px";
       TweenMax.set(tl._last.target,{clearProps:"transform"});
       tl.clear();
       // console.table( Object.getOwnPropertyNames(_this._last) );
     },["{self}", this]);
    
     if( direction === "right") {
       this.timeline.to(this.elem, props.dur1, {
         x: props.dist1,
         ease:Linear.easeNone
       })
       .to(this.elem, props.dur2, {
         bezier:[
           {
             x: "+=0",
             y: "+=0"
           },
           {
             x: "+=" + props.dist2/2,
             y: "-=" + props.h
           },
           {
             x: "+=" + props.dist2,
             y: "+=0"
           }
         ],
         ease:Linear.easeNone
       })
       .to(this.elem, props.dur3, {
         x: "+=" + props.dist3,
         ease:Linear.easeNone
       })
     } else {
       this.timeline.to(this.elem, props.dur1, {
         x: -props.dist1,
         ease:Linear.easeNone
       })
       .to(this.elem, props.dur2, {
         bezier:[
           {
             x: "+=0",
             y: "+=0"
           },
           {
             x: "-=" + props.dist2/2,
             y: "-=" + props.h
           },
           {
             x: "-=" + props.dist2,
             y: "+=0"
           }
         ],
         ease:Linear.easeNone
       })
       .to(this.elem, props.dur3, {
         x: "-=" + props.dist3,
         ease:Linear.easeNone
       });   
     }
  }
}

class Animation {
  bar;
  markerClass;
  buttons;
  markerElemsArr;
  markersArr;
  masterTl;
  
  constructor(barID,markerClass){
    this.bar = document.querySelector("#"+barID);
    this.markerClass = markerClass;
    this.buttons = this.bar.querySelectorAll(".btn");
    this.markerElemsArr = this.bar.querySelectorAll("."+markerClass);
    this.markersArr = [];
  }
  btnEventsInit(){
    for(let i=0; i < this.buttons.length; i++) {
      let btn = this.buttons[i];  
      let _this = this;
      btn.addEventListener("click", function(event){
        _this.moveMarker( event.target );
      },[_this]);
    }
  }
  markerInit(){
    console.log("markerInit()");
    let ballR = (this.bar.querySelector(".ball").getBoundingClientRect().width).toFixed();
    
    let activeBtn = this.bar.querySelector(".btn.active");
    if(this.markerClass === "ball"){
      console.log("initBalls()");
      let activeRect = activeBtn.getBoundingClientRect();
      let barRect = this.bar.getBoundingClientRect();
      for(let i=0; i < this.markerElemsArr.length; i++) {
        let ballElem = this.markerElemsArr[i];
        let ball = new Ball(
          ballElem,i,
          ballR,
          new Vector2D(
            activeRect.left + (i)*10 + i/2,
            activeRect.top - ballR*2
          )
        );   
        this.markersArr.push(ball);
        ballElem.style.top = this.markersArr[i].position.y - ballR + "px"; 
        ballElem.style.left = this.markersArr[i].position.x + ballR/2  + "px";
      }
    }
  }
  
  moveMarker( clickedBtn ){
    
    let activeBtn = this.bar.querySelector(".btn.active");
    console.log("moveMarker: {active: " + activeBtn.innerHTML + " clicked: " + clickedBtn.innerHTML + "}");
    
    let _this = this;
    this.bar.querySelector(".btn.active").classList.toggle("active");
    this.masterTl = new TimelineMax({paused: true});
    
    if(this.markerClass === "ball"){
      let ballTlArr = [];
      let activeRect = activeBtn.getBoundingClientRect();
      
      let barRect = this.bar.getBoundingClientRect();
      
      this.markersArr.map((ball) => {
        let direction = activeBtn.offsetLeft < clickedBtn.offsetLeft
                    ? "right"
                    : "left";
        console.log("direction: " + direction);
        let props = ball.setTweenProps( activeBtn, clickedBtn, direction, 100, 10); 
        console.log("props: " + JSON.stringify(props));
        direction === "right" 
                  ? ball.setTween(direction, props)
                  : ball.setTween(direction, props);
        ballTlArr.push(ball.timeline);
      });  
      
      this.masterTl.add(ballTlArr);
      this.masterTl.play();
      this.masterTl.eventCallback("onComplete", function(timeline, _this, clickedBtn) {
        timeline.clear();
      }, ["{self}"], _this, clickedBtn);
      this.masterTl = null;
      clickedBtn.classList.toggle("active");
    }
  }
};

document.addEventListener("DOMContentLoaded", (event) => {
  console.log("DOMContentLoaded");
  let animation = new Animation("TOP_BAR1","ball");
  
  animation.btnEventsInit();
  animation.markerInit();
});
console.log("test");

              
            
!
999px

Console