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

              
                <h1><span id="vanillaDotAnchor">VanillaJS</span> Info Dots!</h1>
<div data-info-dot data-target="vanillaDotAnchor"
     data-message="Look, ma! No frameworks! Well, except the one built into every browser....">
</div>

<div class="container">

  <p>Info Dots are UI Components you can use to annotate bits of your web site
     or web app interface. They appear as small, pulsing anchors that you can attach
     to practically any DOM Element.</p>
  
  <h2>Just Declare It</h2>
  
  <p>I'm a huge fan of UI components that can be declared in HTML with minimal
    configuration in JavaScript. I'm <em>especially</em> fanatical about the
    structure of the required HTML mark-up.</p>
  
  <p><mark>Component mark-up should be simple.</mark> It should easy to learn how to
    use a component. The mark-up should also be "semantic." That is, it should explain
    itself, so that it's hard to forget how it works. And so these are a few of the goals
    of this component.</p>

  <h3>Example</h3>

  <p>The element you want to annotate does not need to know about the Info Dot
    that will be attached to it. All you have to do is to set an
    <mark><code class="html">id</code></mark> attribute on the element you
    want to target, so that our Info Dot can find it.</p>
  
  <pre><code class="html">&lt;!-- Include the script --&gt;
&lt;script src="info-dots.js"&gt;&lt;/script&gt;

&lt;!-- Here's the element we want to annotate --&gt;
&lt;div id="importantText"&gt;&lt;p&gt;This is important&lt;/p&gt;&lt;/div&gt;

&lt;!-- Place this Info Dot mark-up anywhere else... --&gt;
&lt;!-- Just make sure to set the `data-target` --&gt;
&lt;div data-info-dot data-target="importantText"
     <span id="interestingCode">data-message="Here's something informative for ye."&gt;</span>
&lt;/div&gt;

&lt;!-- Initialize the plugin --&gt;
&lt;script&gt;InfoDots.init();&lt;/script&gt;</code></pre>

  <div data-info-dot data-target="interestingCode"
       data-message="Info Dots can even work here!">
  </div>
  
  <h3>Result</h3>
  
  <!-- Here's the element we want to annotate -->
  <div id="importantText"><p>This is important</p></div>

  <!-- Place this Info Dot mark-up anywhere else... -->
  <!-- Just make sure to set the `data-target` -->
  <div data-info-dot data-target="importantText"
       data-message="Here's something informative for ye.">
  </div>
  
  <p>That's all there is to it!</p>

  <p><a href="http://williamhuster.com" target="_blank">William Huster made this ↗</a></p>
  
  <h2>Browser Support (probably)</h2>
  
  <p>Info Dots has been thoroughly tested in Chrome for Desktop, but not so much
     other browsers. These browsers will most likely support it, though:</p>
  
  <ul>
    <li>IE 10+</li>
    <li>Firefox 4+</li>
    <li>Safari</li>
  </ul>
  
  <p><mark>Info Dots is HTML5-first.</mark> While I am very interested in making this component
     compliant with modern web standards, I have no interest in hacking it up to
     provide support for older browsers. If your needs differ, you are of course welcome
     to fork this code and perform whatever unholy rituals upon it.</p>

</div>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Abril+Fatface);
@import url(https://fonts.googleapis.com/css?family=Signika);

/**
 * Demo Page Styles
 **/
body {
  font-family: Signika, Arial, Helvetica, sans-serif;
  font-weight: 400;
  font-size: 18px;
  line-height: 1.40;
  background: rgb(234, 234, 242);
}

h1, h2 {
  font-family: Abril Fatface, Georgia, serif;
  font-weight: 400;
}
h1 {
  font-size: 50px;
  text-align: center;
}
h2 {
  font-size: 37px;
}

code {
  font-size: 16px;
}

.container {
  max-width: 600px;
  margin: 0 auto;
}

#importantText {
  background: rgb(72, 126, 195);
  border-bottom: 3px solid rgb(50, 91, 150);
  color: white;
  border-radius: 4px;
  padding: 6px;
  width: 250px;
}

/**
 * Info Dot
 **/
.info-dot {
  position: absolute;
  width: 25px;
  height: 25px;
  border-radius: 50%;
  background: gray;
  background-color: rgba(0, 0, 0, 0.35);
  border: 4px solid rgba(255, 255, 255, 0.5);
  z-index: 1000;
  transition: width linear 0.2s,
              height linear 0.2s;
}

.pulse {  
  animation-name: pulse;
  animation-duration: 3s;
  animation-iteration-count: infinite;
  animation-timing-function: ease-in-out;
}

.fade-in {
	animation-name: fade-in;
	animation-duration: 1.5s;	
  animation-iteration-count: 1;
	visibility: visible !important;	
}

.fade-in-up {
	animation-name: fade-in-up;
	animation-duration: 0.4s;	
  animation-iteration-count: 1;
	visibility: visible !important;	
}

.fade-out-down {
	animation-name: fade-out-down;
	animation-duration: 0.4s;	
  animation-iteration-count: 1;
	visibility: visible !important;	
}

/**
 * Popover
 **/
.popover {
  position: absolute;
  background: white;
  min-width: 150px;
  max-width: 300px;
  padding: 6px 12px;
  margin: 8px;
  border: 1px solid rgba(0, 0, 0, 0.35);
  box-shadow: 0 0 18px rgba(0, 0, 0, 0.45);
  border-radius: 6px;
  z-index: 1001;
}

/**
 * Animations
 **/
@keyframes pulse {
  from { transform: scale(1.0, 1.0); }
  65%  { transform: scale(0.8, 0.8); }
  to   { transform: scale(1.0, 1.0); }
}

@keyframes fade-in {
	0% {
		transform: scale(0);
		opacity: 0.0;		
	}
	60% {
		transform: scale(1.1);	
	}
	80% {
		transform: scale(0.9);
		opacity: 1;	
	}	
	100% {
		transform: scale(1);
		opacity: 1;	
	}		
}

@keyframes fade-in-up {
	from {
		transform: translateY(20px);
		opacity: 0.0;		
	}
	to {
		transform: translateY(0);
		opacity: 1;	
	}		
}

@keyframes fade-out-down {
  from {
		transform: translateY(0);
		opacity: 1;	
  }
  to {
		transform: translateY(20px);
		opacity: 0.0;	
  }
}
              
            
!

JS

              
                // Utilities
function render (item) {
  item.render();
}

function show (item) {
  item.show();
}

function hide (item) {
  item.hide();
}

// Debounce function from David Walsh's blog
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
function debounce(func, wait, immediate) {
	var timeout;
	return function() {
		var context = this, args = arguments;
		var later = function() {
			timeout = null;
			if (!immediate) func.apply(context, args);
		};
		var callNow = immediate && !timeout;
		clearTimeout(timeout);
		timeout = setTimeout(later, wait);
		if (callNow) func.apply(context, args);
	};
};

// Library of common DOM Element operations
var IdElement = (function () {
  
  var exports = {
    outerWidth: outerWidth,
    outerHeight: outerHeight,
    animationDuration: animationDuration,
  };
  
  return exports;
  
  ///
  
  function outerWidth (element) {
    /** Get the width of an element including margins */
    var elemCSS = window.getComputedStyle(element);
    return element.offsetWidth
      + (parseFloat(elemCSS.marginLeft, 10) || 0)
      + (parseFloat(elemCSS.marginRight, 10) || 0);
  }

  function outerHeight (element) {
    /** Get the height of an element including margins */
    var elemCSS = window.getComputedStyle(element);
    return element.offsetHeight 
      + (parseFloat(elemCSS.marginTop, 10) || 0)
      + (parseFloat(elemCSS.marginBottom, 10) || 0);      
  }
  
  function animationDuration (element) {
    /* Get the computed animation duration on the element */
    var elementCSS = window.getComputedStyle(element);
    return parseFloat(elementCSS['animation-duration'], 10);
  }
  
})();

//
var Position = (function () {

  var exports = {
    above: above,
    below: below,
    centerOffset: centerOffset,
  };
  
  return exports;
  
  ///

  function above (parent, child) {
    /* Get child position that centers it above parent */
  }
  
  function below (parent, child) {
    /* Get child position that centers it below parent */
    
    var childLeft = centerOffset(parent).left - (IdElement.outerWidth(child) / 2);
    var childTop = parent.offsetTop + parent.offsetHeight;

    // Align the child element to the left side of the screen
    // in edge cases where it would overflow the left side
    // or where the screen is skinnier than the child element.
    if (childLeft < 0 || window.innerWidth <= IdElement.outerWidth(child)) {
      childLeft = 0;
    } else if (childLeft + IdElement.outerWidth(child) > window.innerWidth) {
      // Ensure the popover doesn't overflow the right side of the screen.
      childLeft = window.innerWidth - IdElement.outerWidth(child);
    }

    return {
      top: childTop,
      left: childLeft,
    };
    
  }
  
  function centerOffset (element) {
    /* Get the left offset (in pixels) of the center of an element */
    return {
      left: element.offsetLeft + element.offsetWidth / 2,
      top: element.offsetTop + element.offsetHeight / 2,
    };
  }

})();

//
var Animate = (function () {
  
  var exports = {
    css: css,
  };
  
  return exports;
  
  ///
  
  function css (element, animClass, callback) {
    element.classList.add(animClass);
    setTimeout(function () {
      element.classList.remove(animClass);
      callback();
    }, IdElement.animationDuration(element) * 1000);
  }
  
  // TODO: Implement an `enter` method similar to AngularJS's
  // TODO: Implement a `leave` method similar to AngularJS's
  
})();

// Depends on `Animate` and `IdElement`
function Popover (options) {
  this.parent = options.parent || null;
  this.position = options.position || {
    top: 0,
    left: 0,
  };
  this.message = options.message || 'A popover message!';
  this.element = options.element || document.createElement('div');
  this.element.classList.add('popover');
  this.element.innerHTML = '<p>' + this.message + '</p>';
  this.showing = false;
  this.animating = false;
}

Popover.create = function (options) {
  return new Popover(options);
};

Popover.prototype.show = function () {
  if (this.showing || this.animating) return;

  this.parent.parentNode.insertBefore(this.element, this.parent.nextSibling);
  this.updatePosition(Position.below(this.parent, this.element));

  this.animating = true;
  Animate.css(this.element, 'fade-in-up', function () {
    this.showing = true;
    this.animating = false;
  }.bind(this));
};

Popover.prototype.hide = function  () {
  if (!this.showing || this.animating) return;

  this.animating = true;
  Animate.css(this.element, 'fade-out-down', function () {
    this.element.parentNode.removeChild(this.element);
    this.animating = false;
    this.showing = false;
  }.bind(this));
};

Popover.prototype.toggle = function () {
  if (this.showing) {
    this.hide();
  } else {
    this.show();
  }
}

Popover.prototype.updatePosition = function (position) {
  if (position) this.position = position;
  this.element.style.top = this.position.top + 'px';
  this.element.style.left = this.position.left + 'px';
};

// Depends on `Popover` and `Animate`
function InfoDot (options) {
  this.position = options.position || {
    top: 0,
    left: 0,
  };
  this.showing = true;
  this.enableHover = typeof(options.enableHover) === 'undefined' ? true : options.enableHover;
  this.popover = options.popover || null;
  this.element = options.element || null;
  this.parentElement = options.parentElement || document.body;
}

InfoDot.create = function (options) {
  return new InfoDot(options);
}

InfoDot.prototype.render = function () {
  this.makeElement();
  this.alignToTarget();
  Animate.css(this.element, 'fade-in', function () {
    this.element.classList.add('pulse');
  }.bind(this));
  this.attachPopover();
};

InfoDot.prototype.makeElement = function () {
  if (!this.element) {
    this.element = document.createElement('div');
  }
  this.element.classList.add('info-dot');
  this.parentElement.appendChild(this.element);
};

InfoDot.prototype.updatePosition = function (position) {
  if (position) { this.position = position; }
  this.element.style.top = this.position.top + 'px';
  this.element.style.left = this.position.left + 'px';
};

InfoDot.prototype.attachPopover = function () {
  if (!this.popover) {
    this.popover = Popover.create({
      parent: this.element,
      message: this.element.getAttribute('data-message'),
    });
  }
  this.attachClickListener();
  if (this.enableHover) this.attachHoverListeners();
}

InfoDot.prototype.attachHoverListeners = function () {
  this.element.addEventListener('mouseenter', function () {
    this.popover.show();
  }.bind(this));
  this.element.addEventListener('mouseleave', function () {
    this.popover.hide();
  }.bind(this));
  
  // 
  document.addEventListener('mousemove', debounce(function (event) {
    if (!this.element.contains(event.target) && !this.popover.element.contains(event.target)) {
      if (this.popover.showing) { this.popover.hide(); }
    }
  }.bind(this), 150, true));
}

InfoDot.prototype.attachClickListener = function () {
  this.element.addEventListener('click', function () {
    this.popover.toggle();
  }.bind(this));
}

InfoDot.prototype.alignToTarget = function () {
  this.updatePosition(this.getTargetCenter());
}

InfoDot.prototype.getTargetCenter = function () {
  var targetElem = this.getTargetElem();
  return Position.centerOffset(targetElem);
}

InfoDot.prototype.getTargetElem = function () {
  var targetElemId = this.element.getAttribute('data-target');
  return document.getElementById(targetElemId);
}

InfoDot.prototype.hide = function () {
  this.element.style.display = 'none';
  this.showing = false;
}

InfoDot.prototype.show = function () {
  this.element.style.display = 'block';
  this.showing = true;
}

// Bootstrap and manage a collection of InfoDots
function InfoDots (options) {
  this.dots = [];
}

InfoDots.init = function (options) {
  var newInfoDots = new InfoDots(options);
  newInfoDots.link();
  return newInfoDots;
}

InfoDots.prototype.link = function () {
  var infoDotElems = document.querySelectorAll('[data-info-dot]');
  for (var i = 0; i < infoDotElems.length; ++i) {
    var infoDotElem = infoDotElems[i];
    var newDot = InfoDot.create({element: infoDotElem,});
    this.dots.push(newDot);
    this.dots.map(render);
  }
  
  // Hide dots while the browser resizes and then
  // re-render dots after the browser has been resized,
  // in case their target elements have been reflowed.
  window.addEventListener('resize', debounce(function () {
    this.dots.map(hide);
  }.bind(this), 250, true));
  window.addEventListener('resize', debounce(function () {
    this.dots.map(render);
    this.dots.map(show);
  }.bind(this), 250));
}

InfoDots.prototype.refresh = function () {
  this.dots.map(render);
}

// Calling code!

// Kick off HighlightJS
hljs.initHighlightingOnLoad();

// Init InfoDots
document.addEventListener("DOMContentLoaded", function() {
  // `InfoDots.init()` sets up InfoDots for all DOM Elements with 'data-info-dot' attribute.
  // TODO: We have a race condition with highlightJS's DOM manipulation.
  //       We have to wait until it's done working, or the InfoDot
  //       will not have the correct offset. This will also be a problem
  //       with other plug-ins that affect the size / flow of InfoDot targets.
  setTimeout(InfoDots.init, 1000);
});
              
            
!
999px

Console