Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

Save Automatically?

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 class="container"></div>
<div class="container"></div>
<div class="container"></div>
<div class="container"></div>
              
            
!

CSS

              
                html, body {
  height: 100%;
}

body {
  padding: 5px;
  cursor: default;
}

.container {
  position: relative;  
  user-select: none;
  
  width: 300px;
  height: 200px;
  border: 1px solid #aaa;
  margin: 10px 0 0 10px;
  display: inline-block;
}

.ripple,
.ripple-bg,
.wave {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: hidden;
  user-select: none;
}

.ripple {
  border-radius: inherit;
}

.ripple-bg {
  opacity: 0;      
}

.wave {
  border-radius: 50%;
  opacity: 0;
}
              
            
!

JS

              
                console.clear();

var containers = document.querySelectorAll(".container");

attachRipple(containers[0], {
  waveColor: "#FF5722",
  centered: true
});

attachRipple(containers[1], {
  waveColor: "#3F51B5",
  enterTime: 0.3,
  leaveTime: 0.5
});

attachRipple(containers[2], {
  waveColor: "#009688",
  dimBackground: false,
  enterAlpha: 1,
  enterAlphaTo: 1,
  enterTime: 0.3,
  leaveTime: 0.1,
  maxWaves: 1
});

attachRipple(containers[3], {
  waveColor: "#E91E63",
  centered: true,
  enterTime: 0.4,
  leaveTime: 0.3
});

//
// ATTACH RIPPLE
// ===========================================================================
function attachRipple(element, options) {
          
  var waveColor = options.waveColor || window.getComputedStyle(element).color;
  var enterTime = 0.6;
  var leaveTime = 0.4;
  var baseAlpha = 0.25;
  var baseEase  = Power1.easeIn;
  
  // Ripple settings
  var config = {
    maxWaves: -1, 
    throttle: 100, 
    centered: false,
    
    // Ripple enter animation
    enterTime: enterTime,
    enterEase: baseEase,    
    enterAlpha: baseAlpha,
    enterAlphaTo: null,    
    enterColor: waveColor,
    enterColorTo: null,
       
    // Ripple leave animation
    leaveTime: leaveTime,
    leaveEase  : baseEase,
    leaveColorTo: null,
    leaveAlphaTo: 0,
    
    // Dim background animation
    dimBackground: true,
    dimTime: enterTime,
    dimEase: baseEase,
    dimColor: null,
    dimColorTo: null,    
    dimAlpha: 0,
    dimAlphaTo: baseAlpha / 4
  };
  
  // Ripple API
  var ripple = assign({
    waves: [],
    isDown: false,
    fillWave: null,
    downAction: downAction,
    upAction: upAction,
    clearWaves: clearWaves,
    createWave: createWave,
    removeWave: removeWave,
    reverseDim: reverseDim,
    lastDown: Date.now(),
  }, config, options || {});
  
  // Set defaults for missing values
  ripple.enterAlphaTo = getDefined(ripple.enterAlphaTo, ripple.enterAlpha);
  ripple.enterColorTo = getDefined(ripple.enterColorTo, ripple.enterColor);
  ripple.leaveColorTo = getDefined(ripple.leaveColorTo, ripple.enterColorTo);
  ripple.dimColor     = getDefined(ripple.dimColor, ripple.enterColor);
  ripple.dimColorTo   = getDefined(ripple.dimColorTo, ripple.dimColor);
    
  ripple.container = createElement("ripple", element, {
    force3D: true
  });
    
  if (ripple.dimBackground) {
    
    ripple.background = createElement("ripple-bg", ripple.container, {
      backgroundColor: ripple.dimColor,
      alpha: ripple.dimAlpha
    });
    
    ripple.dimAnimation = TweenLite.to(ripple.background, ripple.dimTime, {
      backgroundColor: ripple.dimColorTo,
      alpha: ripple.dimAlphaTo,
      ease: ripple.dimEase,
      paused: true
    });
  }
  
  // Bind events
  element.addEventListener("mousedown", downAction);    
  element.addEventListener("touchstart", downAction);
  element.addEventListener("mouseup", upAction);
  element.addEventListener("mouseleave", upAction);
  element.addEventListener("touchend", upAction);
    
  function downAction(event) {
    
    if (ripple.isDown) return;
    ripple.isDown = true;
                
    if (ripple.maxWaves > 0 && ripple.waves.length >= ripple.maxWaves) return;
        
    if (ripple.throttle > 0) {
            
      var current = Date.now();
      var elapsed = current - ripple.lastDown;
            
      if (elapsed > ripple.throttle) {
        ripple.lastDown = current;
      } else {
        return;
      }
    }       
       
    var pointer = event.targetTouches && event.targetTouches[0] || event;
    
    var rect = element.getBoundingClientRect();
        
    var x = pointer.clientX - rect.left;
    var y = pointer.clientY - rect.top;
    
    var wave = createWave(rect.width, rect.height, x, y);
       
    ripple.waves.push(wave);
        
    if (ripple.dimAnimation && ripple.dimAnimation.progress() < 1) {
      ripple.dimAnimation.duration(ripple.enterTime).play();
    }
  }
  
  function upAction(event) {
    
    if (!ripple.isDown) return;        
    ripple.isDown = false;
    
    clearWaves();    
  }
    
  function clearWaves() {
    
    if (ripple.fillWave) {
      ripple.fillWave.leave.play();
      ripple.fillWave = null;
    }
  }
  
  function removeWave(wave) {
        
    var index = ripple.waves.indexOf(wave);    
        
    if (index > -1) {
      ripple.waves.splice(index, 1);
    } 
    
    if (wave.element) {
      ripple.container.removeChild(wave.element);
    }      
  }
    
  function reverseDim(wave) {
        
    // Get waves that aren't leaving
    var available = ripple.waves.filter(function(wave) {
      return !wave.isLeaving;
    });
    
    var last = available[available.length - 1];
    
    // If this is the last wave, reverse animation
    if (wave === last) {
      // ripple.dimAnimation.reverse();
      ripple.dimAnimation.duration(ripple.leaveTime).reverse();
    }
  }
  
  function createWave(w, h, x, y) {
    
    var cx = w / 2;
    var cy = h / 2;
    
    var dx = x < cx ? w - x : x;
    var dy = y < cy ? h - y : y;

    var size = Math.sqrt(dx * dx + dy * dy) * 2;
        
    var waveElement = createElement("wave", ripple.container, {
      backgroundColor: ripple.enterColor,
      alpha: ripple.enterAlpha,
      width: size,
      height: size,
      scale: 0,
      xPercent: -50,
      yPercent: -50,
      left: x,
      top: y,
      force3D: false,
    });
    
    // Wave object
    var wave = {
      element: waveElement,
      isLeaving: false,      
      enter: new TimelineLite({ onComplete: enterComplete }),
      leave: new TimelineLite({ paused: true, onStart: leaveStart, onComplete: leaveComplete })
    };
    
    // Enter animation
    wave.enter.to(waveElement, ripple.enterTime, { 
      x: ripple.centered ? cx - x : 0,
      y: ripple.centered ? cy - y : 0,
      backgroundColor: ripple.enterColorTo,
      alpha: ripple.enterAlphaTo,
      scale: 1, 
      ease: ripple.enterEase 
    });
    
    // Leave animation
    wave.leave.to(waveElement, ripple.leaveTime, { 
      alpha: ripple.leaveAlphaTo, 
      backgroundColor: ripple.leaveColorTo,
      ease: ripple.leaveEase 
    });
    
    function enterComplete() {
      
      if (!ripple.fillWave && ripple.isDown) {
        
        // Setting the fillWave while isDown is true will allow other leave animations to play
        ripple.fillWave = wave;
      } else {
        
        if (ripple.fillWave && !ripple.isDown) {
          ripple.fillWave = null;
        }
        
        wave.leave.play();
      }
    }
    
    function leaveStart() {
      
      // Try to reverse the dim animation
      if (ripple.dimAnimation && !ripple.isDown) {
        reverseDim(wave);
      }
           
      wave.isLeaving = true;
    }
        
    function leaveComplete() {
      removeWave(wave);
    }
    
    return wave;
  }  
  
  return ripple;  
}

//
// HELPER FUNCTIONS
// ===========================================================================
function createElement(className, parent, config) {
  var element = document.createElement("div");
  element.className = className;
  config && TweenLite.set(element, config);
  parent && parent.appendChild(element);  
  return element;
}

function assign(target) {

  var total = arguments.length;
  for (var i = 1; i < total; i++) {
    var source = arguments[i];
    if (source !== undefined && source !== null) {
      for (var key in source) {
        target[key] = source[key];
      }
    }
  }
  return target;
}

function getDefined(a, b) {
  return a !== undefined && a !== null ? a : b;
}



              
            
!
999px

Console