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

              
                -NUM = 32

.container
  h1 Grid Selection

  p Select tiles in the grid by using CMD/CTRL to select multiple tiles, SHIFT to select a range of tiles, or drag to select all tiles within an area.

  ul
    //selected items have class 'focus'
    li.tile.focus 1
    - for (var i = 0; i < (NUM-1); ++i)
      li.tile= i+2

.ghost
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Open+Sans);
$blocksize:50px;
$color:#B6DB23;
$dark:desaturate(darken($color, 40%), 70%);
$light:desaturate(lighten($color, 30%), 30%);

body {
  color:$light;
  background:$dark;
  font-family: 'Open Sans', 'Helvetica Neue', Helvetica, Arial;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.container{
  padding: 25px;
}

ul {
  padding:0;
  list-style:none;
}

li {
  margin:10px 10px 0 0;
  padding:15px;
  width:$blocksize;
  height:$blocksize;
  background:$light;
  cursor:pointer;
  color:$dark;
  float:left;
  transition:background 0.2s;
  box-shadow:0 0 5px rgba(0,0,0,8);
}

li.focus {
  background:$color;
}

.ghost{
  position:fixed;
	z-index:1000;
	background:rgba($color,0.2);
	box-shadow:inset 0 0 0 1px $color;
}
              
            
!

JS

              
                var CMD_CODE = 91;
var CTRL_CODE = 17;
var SHIFT_CODE = 16;
var DRAG_THRESHOLD = 5;
var items = $("li");
var ghost = $(".ghost");

var state = {
  dragging: false	,
  mousedown: false	,
  multiselect: false,
  rangeselect: false,
  last_clicked: null
}


$(document).on('keydown', function(e){
  if (e.keyCode == CMD_CODE || e.keyCode == CTRL_CODE) {
    state.multiselect = true;
  }
  if (e.keyCode == SHIFT_CODE) {
        state.rangeselect = true;
  }
}).on('keyup', function(e){
  if (e.keyCode == CMD_CODE || e.keyCode == CTRL_CODE) {
    state.multiselect = false;
  }
  if (e.keyCode == SHIFT_CODE) {
    state.rangeselect = false;
  }
});


//bind tile click
items.on('click', function() {
    update_focus($(this));
});


function update_focus(el) {
  var count = $('li.focus').length;
  //shift key
  if (!state.multiselect && state.rangeselect) {
    if (!state.last_clicked) {
      items.not(el).removeClass('focus');
      el.toggleClass('focus');
      state.last_clicked = el;
    } else {
      items.removeClass('focus');
      var bounds = el.add(state.last_clicked);
      var range = bounds.first().nextUntil(bounds.last());
      range.addClass('focus');
      bounds.addClass('focus');
    }
  }
  //cmd/ctrl key
  else if (state.multiselect && !state.rangeselect) {
    el.toggleClass('focus');
    state.last_clicked = el;
  }
  //no modifiers
  else {
    items.not(el).removeClass('focus');
    if (count > 1) el.addClass('focus');
    else el.toggleClass('focus');
    state.last_clicked = el;
  }
}


init_drag_selection($(document));

//initialize dragging transparent ghost element 
function init_drag_selection(el){
  var x_start = 0;
  var y_start = 0;
  var w = 0;
  var h = 0;

  el.on('mousedown', function(e){
    x_start = e.pageX;
    y_start = e.pageY;
    state.mousedown = true;
    ghost.css({'left':x_start, 'top':y_start });
  });

  el.on("mouseup", function(e){
    state.mousedown = false;
    ghost.css({ 'width':0, 'height':0 });  
    if(state.dragging){
      select_inside(ghost.css('left'), ghost.css('top'), w, h); 
      state.dragging = false;
    }
    //deselect by clicking outside a tile
    else{
      if(!$(e.target).hasClass('tile')){
      		  items.removeClass('focus');  
      }
    }
  });

  el.on("mousemove", function(e){
    if(state.mousedown){
      w = Math.abs(x_start - e.pageX);
      h = Math.abs(y_start - e.pageY);

      if((w>DRAG_THRESHOLD && h>DRAG_THRESHOLD) && !state.dragging){
        state.dragging = true;
      }
      
      ghost.css({ 'width':w, 'height':h });
      if (e.pageX <= x_start && e.pageY >= y_start) {
        ghost.css({'left': e.pageX});
      } else if (e.pageY <= y_start && e.pageX >= x_start) {
        ghost.css({'top': e.pageY });
      } else if (e.pageY < y_start && e.pageX < x_start) {
        ghost.css({ 'left': e.pageX, "top": e.pageY });
      }
    }
  });
}

//select the files that are inside the
//ghost element when releaseing the mouse
function select_inside(x, y, w, h){
  var box = {
    top:parseInt(y),
    left:parseInt(x),
    bottom:parseInt(y)+parseInt(h),
    right:parseInt(x)+parseInt(w)
  }

  if(!state.multiselect && !state.rangeselect){
    		items.removeClass('focus');  
  }
  items.each(function(){
    var inside = (function(el){
      var tile = el.getBoundingClientRect();
      //check intersection
      return (
        (tile.left <= box.right) &&
        (box.left <= tile.right) &&
        (tile.top <= box.bottom) &&
        (box.top <= tile.bottom)
      );
    })(this);

    if(inside){
      $(this).addClass('focus');
    }
  });
}


              
            
!
999px

Console