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

              
                <main>
  <div class="wrapper">
    <h2>
      Selectable without the bloat of jQuery and jQuery UI
    </h2>
    <small>Click and / or drag to select items. Hold Ctrl to select multiple. aHold Shift to select consecutive groups of items.</small>
    <div id="wrapper">
      <div id="container">
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
        <div class="box"></div>
      </div>
      <div id="threshold">
        <div class="threshold"></div>
        <div class="threshold"></div>
      </div>
    </div>
    <small><a href="https://github.com/Mobius1/Selectable" target="_blank"><i class="fa fa-github"></i><span>View on GitHub</span></a></small>
  </div>
</main>
              
            
!

CSS

              
                $primary: #9C27B0;
$success: #673AB7;
$danger: #E91E63;
$secondary: #aaa;
@import url('https://fonts.googleapis.com/css?family=Roboto+Mono');
@font-face {
	src: url("https://s3-us-west-2.amazonaws.com/s.cdpn.io/86186/gothamrnd-medium.ttf");
	font-family: "Gotham Rounded";
}

@mixin flexCenter {
	display: flex;
	align-items: center;
	justify-content: center;	
}

@mixin clear {
	clear: both;
	display: table;
	content: "";
}

body {
	margin: 0;
	overflow-x: hidden;
	font-family: "Gotham Rounded";
	color: #666;	
}

main {
	margin-top: 50px;
	width: 100vw;
	height: 100vh;
	display: flex;
	align-items: center;
	flex-flow: column;
}

h2 {
	margin: 0;
}

small {
	display: block;
	margin: 10px 0;
}

div {
	&::before, &::after {
		@include clear;
	}	
}

/* DEMO */
.wrapper {
	width: 600px;
}

#container {
	width: 100%;
	height: 60vh;
	padding: 5px;
	overflow-y: auto;
	
	position: relative;
	
	display: flex;
	flex-wrap: wrap;
	justify-content: stretch;
	align-content: flex-start;
	
	&.random {
		overflow-y: visible;
		height: auto;
	}
}

#wrapper {
	position: relative;
}

.threshold {
	width: 120%;
	height: 0px;
	
	position: absolute;
	
	&::after {
		position: absolute;
		width: 100px;
		height: 0px;
		left: 100%;
		top: -6px;
		// width: 100%;
		text-align: center;
		font-size: 10px;
		content: "autoscroll trigger"
	}
	
	&:first-child {
		top: 0;
		border-bottom: 1px dashed rgba(0,0,0,0.4);
	}
	
	&:last-child {
		bottom: 0;
		border-top: 1px dashed rgba(0,0,0,0.4);
	}
}

$margin: 5px;
$size: 96px;
$border: 2px;

.box {
	width: $size - $margin;
	height: $size - $margin;
	color: $secondary;
	// border: $border dotted rgba(0,0,0,0.4);
	border-radius: $border;
	margin: 0 $margin $margin 0;
	position: relative;
	background-color: #eee;
	@include flexCenter;
	transition: all 100ms;
	
	&::after {
			content: "non-selectable";
	}	
	
	&.wide {
		// width: $size * 2 - $margin;
		// flex-grow: 2;
	}
	
	&.ui-selectable, &.selectable {
		color: #fff;
		background-color: #aaa;
	}
	
	&::after {
		font-size: 12px;
		font-family: "Roboto Mono";
	}
	
	&.ui-selecting {
		background-color: $primary;
	}	
	&.ui-selected {
		background-color: $success;
	}
	&.ui-deselecting {
		background-color: $danger;
	}
	&.floated {
		box-shadow: 0 0 10px rgba(0,0,0,0.4);
	}	
}
.ui-selectable::after {
    content: "selectable";
}
.ui-selecting::after {
    content: "selecting";
}
.ui-selected::after {
    content: "selected";
}
.ui-deselecting::after {
    content: "deselecting";
}

.ui-multiple {
	cursor: crosshair;
}

.key {
	@include flexCenter;
	.box {
		padding: 0;
		font-size: 13px;
		flex-grow: 1;
	}
}

a,
a:hover,
a:focus,
a:active {
	color: #333;
	outline: medium none;
	text-decoration: none;
}	

.fa {
	font-size: 21px;
	padding-right: 5px;
	vertical-align: middle;
}

.dg, .dg .c input[type="text"] {
	font: 11px "Roboto Mono", sans-serif;
}

.dg .property-name {
	overflow: visible;
}
              
            
!

JS

              
                "use strict";

var container = document.getElementById("container");
var rect = container.getBoundingClientRect();
var boxes = [].slice.call(container.getElementsByClassName("box"));
var filter = "selectable";
/* INIT SELECTABLE */

var selectable = new Selectable({
  filter: ".box",
  appendTo: container,
  autoScroll: {
    increment: 15,
    threshold: 0
  },
  lasso: {
    borderColor: "rgba(255, 255, 255, 1)",
    backgroundColor: "rgba(255, 255, 255, 0.1)"
  }
});
selectable.on("init", function () {
  boxes.forEach(function (box) {
    box.classList.add("ui-selectable");
  });
  setThreshold();
});








/* DEMO STUFF */

init();

function init() {
  var gui = new dat.GUI();
  var tolerance = {
    name: "tolerance",
    touch: true,
    fit: false
  };
  var direction = {
    name: "shiftDirection",
    normal: true,
    reverse: false
  };
  var lassoSelect = {
    name: "lassoSelect",
    normal: true,
    sequential: false
  };
  var Filter = {
    enabled: false
  };
  var Misc = {
    type: "fixed",
    fixed: function fixed() {
      boxes.forEach(function (el) {
        el.classList.remove("floated");
        el.style.position = "";
        el.style.top = "";
        el.style.left = "";
        el.style.transform = "";
      });
      selectable.update();
    },
    transformed: function transformed() {
      var w = window.innerWidth;
      var h = window.innerHeight;
      var o = 200;
      var t = 250;
      var m = 5;
      toggle(lassoSelect, "normal");
      boxes.forEach(function (el) {
        var r = el.getBoundingClientRect();
        var x = getRandomInt(o, w - o);
        var y = getRandomInt(o, h - o);
        var a = getRandomInt(0, 270);
        var s = getRandomInt(-8, 15) / 10;
        el.classList.add("floated");
        el.style.position = "absolute";
        el.style.top = "".concat(y - rect.top, "px");
        el.style.left = "".concat(x - rect.left, "px");
        el.style.transform = "translate3d(".concat(getRandomInt(-50, 50), "px, ").concat(getRandomInt(-50, 50), "px, 0) rotate(").concat(a, "deg) scale(").concat(s, ")");
      });
      selectable.update();
    },
    random: function random() {
      var w = window.innerWidth;
      var h = window.innerHeight;
      var o = 200;
      var t = 250;
      var m = 5;
      toggle(lassoSelect, "normal");
      boxes.forEach(function (el) {
        var r = el.getBoundingClientRect();
        var x = getRandomInt(o, w - o);
        var y = getRandomInt(o, h - o);
        el.classList.add("floated");
        el.style.position = "absolute";
        el.style.top = "".concat(y - rect.top, "px");
        el.style.left = "".concat(x - rect.left, "px");
        el.style.transform = "translate3d(".concat(r.left - x - m, "px, ").concat(r.top - y - m, "px, 0)");
        el.offsetTop;
        el.style.transform = "translate3d(0px, 0px, 0)";
        el.style.transition = "transform ".concat(t, "ms");
        setTimeout(function () {
          el.style.transform = "";
          el.style.transition = "";
        }, t);
      });
      setTimeout(function () {
        selectable.update();
      }, 2000);
    }
  };
  var select = {
    selectAll: selectable.selectAll.bind(selectable),
    clear: selectable.clear.bind(selectable),
    invert: selectable.invert.bind(selectable),
    getItems: function getItems() {
      console.log(selectable.getItems());
    },
    getNodes: function getNodes() {
      console.log(selectable.getNodes());
    },
    getSelectedItems: function getSelectedItems() {
      console.log(selectable.getSelectedItems());
    },
    getSelectedNodes: function getSelectedNodes() {
      console.log(selectable.getSelectedNodes());
    }
  };
  var autoScroll = {
    enabled: true
  };
  var arr = [];
  var a = gui.addFolder("Methods");
  var enable = a.add(selectable, "enable");
  var disable = a.add(selectable, "disable");
  enable.onChange(function () {
    toggleView();
  });
  disable.onChange(function () {
    toggleView();
  });
  toggleItem(enable, !selectable.enabled);
  arr.push(a.add(select, "selectAll"));
  arr.push(a.add(select, "invert"));
  arr.push(a.add(select, "clear"));
  arr.push(a.add(select, "getItems"));
  arr.push(a.add(select, "getNodes"));
  arr.push(a.add(select, "getSelectedItems"));
  arr.push(a.add(select, "getSelectedNodes"));
  a.open();
  var auto = gui.addFolder("Autoscroll");
  var autoSettings = selectable.config.autoScroll;
  var autoThreshold = auto.add(selectable.config.autoScroll, "threshold").name("threshold (px)").min(-50).step(1).max(50).onChange(function (val) {
    selectable.config.autoScroll.threshold = val;
    autoSettings.threshold = val;
    setThreshold();
  });
  var autoToggle = auto.add(autoScroll, 'enabled').name("Enabled").onChange(function (bool) {
    if (bool) {
      selectable.config.autoScroll = autoSettings;
    } else {
      selectable.config.autoScroll = bool;
    }

    toggleItem(autoThreshold, bool);
    selectable.disable();
    setTimeout(function () {
      setThreshold(!bool);
      selectable.enable();
    }, 30);
  });
  arr.push(autoToggle);
  arr.push(autoThreshold);

  autoToggle.__li.parentNode.appendChild(autoThreshold.__li);

  auto.open();
  var b = gui.addFolder("Tolerance");
  arr.push(b.add(tolerance, 'touch').listen().onChange(function () {
    toggle(tolerance, "touch");
  }));
  arr.push(b.add(tolerance, 'fit').listen().onChange(function () {
    toggle(tolerance, "fit");
  }));
  b.open();
  var b1 = gui.addFolder("lassoSelect");
  arr.push(b1.add(lassoSelect, 'normal').listen().onChange(function (bool) {
    toggle(lassoSelect, "normal");
  }));
  arr.push(b1.add(lassoSelect, 'sequential').listen().onChange(function (bool) {
    toggle(lassoSelect, "sequential");
  }));
  b1.open();
  var m = gui.addFolder("Toggle");
  arr.push(m.add(selectable.config, 'toggle').name("Enabled").onChange(function (bool) {
    selectable.clear();
    selectable.container.classList.toggle(selectable.config.classes.multiple, bool);
  }));

  function toggle(obj, prop) {
    for (var param in obj) {
      if (typeof obj[param] !== "string") obj[param] = false;
    }

    obj[prop] = true;
    selectable.config[obj.name] = prop;
    selectable.update();
  }

  var c = gui.addFolder("Non-selectable Items");
  arr.push(c.add(Filter, 'enabled').onChange(function (bool) {
    toggleFilter(bool);
  }));
  var d = gui.addFolder("Item Positioning");
  arr.push(d.add(Misc, "random").onChange(toggleContainer));
  arr.push(d.add(Misc, "fixed").onChange(toggleContainer));
  arr.push(d.add(Misc, "transformed").onChange(toggleContainer));
  d.open();

  function toggleContainer(bool) {
    var rand = this.property === "random" || this.property === "transformed";
    container.classList.toggle("random", rand);
    selectable.setContainer(rand ? document.body : container);
    boxes.forEach(function (box) {
      selectable.add(box);
      box.style.opacity = 1;
    });
    selectable.lasso.style.borderColor = selectable.config.lasso.borderColor;
    selectable.lasso.style.backgroundColor = selectable.config.lasso.backgroundColor;

    if (rand) {
      selectable.lasso.style.borderColor = "rgba(0,0,0,1)";
      selectable.lasso.style.backgroundColor = "rgba(255,255,255,0.2)";
      new Set([].slice.call({
        length: boxes.length
      }, function () {
        return Math.floor(Math.random() * boxes.length);
      })).forEach(function (n) {
        selectable.remove(boxes[n]);
        boxes[n].style.opacity = 0;
      });
    }

    setTimeout(function () {
      selectable.update();
    }, 500);
  }

  var colors = {
    borderColor: selectable.config.lasso.borderColor.replace(/rgba\((.+)\)/, "$1").split(","),
    backgroundColor: selectable.config.lasso.backgroundColor.replace(/rgba\((.+)\)/, "$1").split(",")
  };
  var e = gui.addFolder("Lasso Styling");
  e.addColor(colors, 'borderColor').onFinishChange(function (a) {
    selectable.lasso.style.borderColor = "rgba(".concat(a.join(","), ")");
  });
  e.addColor(colors, 'backgroundColor').onFinishChange(function (a) {
    selectable.lasso.style.backgroundColor = "rgba(".concat(a.join(","), ")");
  });

  function toggleView(bool) {
    toggleItem(enable, selectable.enabled);
    toggleItem(disable, !selectable.enabled);
    arr.forEach(function (item) {
      toggleItem(item, !selectable.enabled);
    });
  }

  function toggleItem(item, bool) {
    item.__li.style.pointerEvents = bool ? "" : "none";
    item.__li.firstElementChild.style.opacity = bool ? 1 : .2;
  }
}

function toggleFilter(bool) {
  selectable.clear();

  if (bool) {
    // Use Set to ensure unique indexes
    new Set([].slice.call({
      length: boxes.length
    }, function () {
      return Math.floor(Math.random() * boxes.length);
    })).forEach(function (n) {
      return selectable.remove(boxes[n]);
    });
  } else {
    boxes.forEach(function (box) {
      return selectable.add(box);
    });
  }

  selectable.update();
}

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min;
}

function setThreshold(bool) {
  var els = [].slice.call(document.getElementById("threshold").children);

  if (bool) {
    els[0].parentNode.style.display = "none";
  } else {
    els[0].parentNode.style.display = "block";
    els[0].style.top = "".concat(selectable.config.autoScroll.threshold, "px");
    els[1].style.bottom = "".concat(selectable.config.autoScroll.threshold, "px");
  }
}
              
            
!
999px

Console