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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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>
  <button id="add">Add Element</button> Add element with a click spin effect initialized using gsFx class and attributes
</div>

<div>
  <button id="fx-play">gsFxPlay</button> Scale all the boxes
</div>

<div>
  <button id="fx-off">gsFxOff</button> Remove fade effect on first box
</div>

<hr>

<div id="container-1" class="container">
  <div class="content"></div>
</div>
<div id="container-2"  class="container">
  <div class="content"></div>
</div>
<div id="container-3"  class="container">
  <div class="content"></div>
</div>



              
            
!

CSS

              
                body {
  padding: 15px;
}

.container {
  position: relative;
  width: 150px;
  height: 150px;
  margin: 10px;
  border: 2px solid #333;
  float: left;
}

.content {
  position: relative;
  width: 100%;
  height: 100%;
  background: lightblue;
}

button {
  margin: 6px;
}
              
            
!

JS

              
                console.clear();
var log = console.log.bind(console);

(function($) {
  
  var effects = {};
  
  $.extend(effects, {
    
    spinRight: function() {

      var effect = {
        enter: { rotation: 360, ease: "easeNoneLinear" },
        leave: { rotation: 0, ease: "easeNoneLinear" },
        duration: 0.5,
        animation: "spinRight"
      };

      return gsFxAnimation(effect);
    },
    spinLeft: function() {

      var effect = {
        enter: { rotation: -360, ease: "easeNoneLinear" },
        leave: { rotation: 0, ease: "easeNoneLinear" },
        duration: 0.5,
        animation: "spinLeft"
      };

      return gsFxAnimation(effect);
    },
    fadeUp: function() {

      var effect = {
        enter: { scale: 0.5, alpha: 0, yPercent: -100, ease: "easeOutPower1" },
        leave: { scale: 1,   alpha: 1, yPercent: 0, ease: "easeInPower1" },
        duration: 0.25,
        animation: "fadeUp"
      };

      return gsFxAnimation(effect);
    },
    inflate: function() {

      var effect = {
        enter: { scale: 1.05, repeat: 1, yoyo: true, ease: "easeOutPower1" },
        leave: { scale: 1, repeat: 1, yoyo: true, ease: "easeInPower1" },
        duration: 0.1,
        animation: "inflate"
      };

      return gsFxAnimation(effect);
    }
  });

  var animations = {};

  $.each(effects, function (name, effect) {
    animations[name] = effect();
  });

  $.fn.extend({
    gsFx: GSFX("on", false),
    gsFxOne: GSFX("one", false),
    gsFxPlay: GSFX("one", true),
    gsFxOff: gsFxOff
  });
  
  var list1 = document.getElementsByClassName("gsFx");
  var list2 = document.getElementsByClassName("gsFxOne");
  var list3 = document.getElementsByClassName("gsFxPlay");
  var list4 = document.getElementsByClassName("gsFxOff");

  TweenLite.ticker.addEventListener("tick", checkLists);
  
  function checkLists() {
    
    if (list1.length) {
      $(".gsFx").removeClass("gsFx").each(function() {
        addEffects($(this), "gsfx", "gsFx");
      });
    }

    if (list2.length) {
      $(".gsFxOne").removeClass("gsFxOne").each(function() {
        addEffects($(this), "gsfx-one", "gsFxOne");
      });
    }

    if (list3.length) {
      $(".gsFxPlay").removeClass("gsFxPlay").each(function() {
        addEffects($(this), "gsfx-play", "gsFxPlay");
      });
    }

    if (list4.length) {
      $(".gsFxOff").removeClass("gsFxOff").each(function() {
        addEffects($(this), "gsfx-off", "gsFxOff");
      });
    }
  }

  function addEffects(element, spinal, camel) {

    var animations = element.data(spinal).split(",");

    animations.forEach(function(animation) {

      var types = element.data(animation + "-events");

      if (camel !== "gsFxPlay") {
        element[camel](types, camelCase(animation));
      } else {
        element[camel](camelCase(animation));
      }    
    });
  }
  
  function getOptions(effect, element, options) {
  
    var name = effect.animation;

    var enterData = element.data(name + "Enter") || {};
    var leaveData = element.data(name + "Leave") || {};
    var duration2 = element.data(name + "Duration");
    var target2 = element.data(name + "Target");

    var enter = $.extend({}, effect.enter, enterData, options.enter || {});
    var leave = $.extend({}, effect.leave, leaveData, options.leave || {});

    var duration = getValue(options.duration, getValue(duration2, effect.duration));
    var target = getValue(options.target, getValue(target2, element));

    return {
      target: $(target),
      duration: duration,
      enter: enter,
      leave: leave
    };
  }
  
  // Just a generic animation factory
  // Not sure about using timelines here...
  function gsFxAnimation(effect) {

    function getAnimation(options) {

      var duration = options.duration;
      var target = options.target;
      var enter = options.enter;
      var leave = options.leave;
            
      var tl = new TimelineMax({ paused: true })
        .set(target, leave)
        .to(target, duration, enter);

      function play() {
        tl.progress() < 1 ? tl.play() : tl.restart();
      }

      function reverse() {
        tl.reverse();
      }

      return {
        play: play,
        reverse: reverse
      };
    }

    return {
      effect: effect,
      getAnimation: getAnimation
    };
  }
  
  function GSFX(attach, immediate) {
  
    var isMulti = attach === "on";

    return function gsFx(events, effect, options) {

      if (immediate) {
        options = effect;
        effect = events;
      }

      var elements = $(this);

      var types = events.split(" ");

      elements.each(function(i, node) {

        var element = $(node);

        var fx = animations[effect];

        var config = getOptions(fx.effect, element, options || {});
        
        var animation = fx.getAnimation(config);

        types.forEach(function (type) {

          if (type === "hover") {
            type = "mouseenter";
            element.on("mouseleave", animation.reverse);
            isMulti && setData(element, "mouseleave", effect, animation.reverse);
          }

          immediate ? animation.play() : element[attach](type, animation.play);

          isMulti && setData(element, type, effect, animation.play);
        });
      });
    }  
  }
  
  function gsFxOff(events, effect) {

    var targets = $(this);

    var types = events.split(" ");

    targets.each(function (i, element) {

      var target = $(element);

      var data = target.data("gsFx");

      if (data) {

        types.forEach(function (type) {

          if (type === "hover") {
            type = "mouseenter";
            if (data["mouseleave"]) {
              var animation = data["mouseleave"][effect];
              target.off("mouseleave", animation);
            }
          }

          if (data[type]) {
            var animation = data[type][effect];
            target.off(type, animation);
          }
        });
      }
    });
  }
  
  function setData(element, type, effect, handler) {
  
    var data = element.data("gsFx");

    if (!data) {
      data = {};
      element.data("gsFx", data);
    }

    data[type] = data[type] || {};
    data[type][effect] = handler;
  }
  
  function camelCase(string) {
    return string.replace(/(\-[a-z])/g, function($1) { return $1.toUpperCase().replace('-',''); });
  }

  function spinalCase(string) {
    return string.replace(/([A-Z])/g, function($1) { return "-" + $1.toLowerCase(); });
  }  
  
  function getValue(a, b) {
    return a !== undefined && a !== null ? a : b;
  }
  
})(jQuery);



var body = $("body");

$(".container").each(function() {
  
  var element = $(this);
  var content = element.children(".content");
  
  element.gsFx("hover", "fadeUp", {
    target: content
  });
});

$("#fx-play").click(function () {
  $(".container").gsFxPlay("inflate");
});

$("#fx-off").click(function () {
  $("#container-1").gsFxOff("hover", "fadeUp");
});

var count = 4;
$("#add").click(function () {
  
  var id = "container-" + count++;
  
  var container = $("<div/>").addClass("container gsFx");
  container.attr("id", id)
  container.attr("data-gsfx", "spin-right")
  container.attr("data-spin-right-events", "click")
  container.attr("data-spin-right-target", "#" + id + " > .content")
  
  var content = $("<div/>").addClass("content");
  
  container.append(content);
  body.append(container);
});







              
            
!
999px

Console