cssAudio - ActiveCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <div id="dragArea" class="row dragArea">
	<div id="dragDiv" class="draggable dragDiv"></div>
  <p id="message">
    hip hip hurray!
  </p>
</div>

<div class="container">
  <div class="row attribute-row">
	  <div id="attribute-left" class="col-xs-3 attribute left">left</div>
	  <div id="attribute-right" class="col-xs-3 attribute right">right</div>
	  <div id="attribute-up" class="col-xs-3 attribute up">up</div>
	  <div id="attribute-down" class="col-xs-3 attribute down">down</div>
  </div>
  <br />
  <br />
  <div class="row">
    <div id="distanceMeter" class="col-xs-12 attribute distanceMeter"> Distance: 0 px</div>
  </div>
</div>
            
          
!
            
              @import "lesshat";

.dragDiv {
	width: 100px;
	height: 100px;
	background-color: red;
}

.attribute {
	text-align: center;
  line-height: 14px;
  font-size: 14px;
}

.dragArea{
  width: 100%;
	height: 300px;
}


.attribute-row {
  //height: 50px;
}

#message {
  display: none;
  text-align: center;  
}

            
          
!
            
              //global variables
var globalDistance,
    globalDirection;

var maxDistance = 250;

function increaseFontSize ( _selector, _fontSize) {
	'use strict';
  var maxFontSize = 28;
  
  if(_fontSize > maxFontSize){
    _fontSize = maxFontSize;
  }
  
	$(_selector).css({
		fontSize : _fontSize + 'px',
		lineHeight: _fontSize + 'px'
	});
}

// parses a string containing a size ('number+px') to an integer
function parseSizeString (_fontSize) {
	'use strict';
	var fontSize = _fontSize.split('px')[0];
	var fontFloat = parseFloat(fontSize);
	return fontFloat;
}

function decreaseFontSize (_selector) {
	'use strict';
	$(_selector).animate({
		fontSize: 14 + 'px',
		lineHeight: 14 + 'px'
	},
	{
		duration: 'slow',
		easing: 'easeOutElastic'
	});
}

function getDirection (_ui) {
	'use strict';
	// could be left right up down
	var direction = [];
	// horizontal
	if (Math.abs(_ui.originalPosition.left - _ui.position.left) > Math.abs(_ui.originalPosition.top - _ui.position.top)){
		direction.push('x');
		// left
		if (_ui.originalPosition.left > _ui.position.left) {
			direction.push('left');
		}
		// right
		else{
			direction.push('right');
		}
	}
	//vertikal
	else{
		direction.push('y');
		// up
		if (_ui.originalPosition.top > _ui.position.top) {
			direction.push('up');
		}
		// down
		else{
			direction.push('down');
		}
	}
	return direction;
}

var getXDistance = function(_ui, _dragObj) {
  'use strict';
   return Math.abs(_ui.originalPosition.left - parseSizeString($(_dragObj).css('left')));
};

var getYDistance = function(_ui, _dragObj) {
  'use strict';
   return Math.abs(_ui.originalPosition.top - parseSizeString($(_dragObj).css('top')));
};

function printDistance (_distance){
  'use strict';
  $('#distanceMeter').html('Distance: ' + _distance + ' px');
}

$(document).ready(function() {
	'use strict';

	var direction;
  
  $('#message').position({
    my: 'center center',
    at: 'center center',
    of: '#dragArea'
  });

	$('#dragDiv').position({
		my: 'center center',
		at: 'center center',
		of: '#dragArea'
	})
	.draggable({
		containment: 'parent',
		cursor: 'hand',
		start: function (event, ui) {
		},
		drag: function (event, ui) {
			var distance;
			direction = getDirection(ui);
			$(this).draggable( "option", "axis", direction[0]);
			if(direction[0] === 'x'){
				distance = getXDistance(ui, this);
			}
			else{
				distance = getYDistance(ui, this);
			}
			if(distance > maxDistance){
				$(this).css({
					backgroundColor: 'black'
				});
			}
      else{
				$(this).css({
					backgroundColor: 'red'
				});
      }
			increaseFontSize('.' + direction[1], distance/2);
      printDistance(distance);
      globalDistance = distance;
      globalDirection = direction;
		},
		stop: function (event, ui) {
      if (globalDistance <= maxDistance ){
      
			  $(this).animate({
				  top: ui.originalPosition.top,
				  left: ui.originalPosition.left,
				  backgroundColor: 'red'
			  },
			  {
				  duration: 'slow',
				  easing: 'easeOutElastic'
			  });
			  decreaseFontSize('.' + direction[1]);
			  direction = null;
      }
      else{
        switch(globalDirection[1]){
          case 'left':
            $(this).animate({
              left: -100
            },
            {
              duration: 'slow',
                easing: 'easeOutElastic'
            });
            break;
          case 'right':
            $(this).animate({
              left: $(document).width() + 100
            },
            {
              duration: 'slow',
                easing: 'easeOutElastic'
            });
            break;
          case 'up':
            $(this).animate({
              top: -100
            },
            {
              duration: 'slow',
                easing: 'easeOutElastic'
            });
            break;
          case 'down':
            $(this).animate({
              top: $(document).height() + 100
            },
            {
              duration: 'slow',
                easing: 'easeOutElastic'
            });
            break;
        }
        $('#message').show();
      }
			$('.attribute').removeAttr('style');
      printDistance(0);
      globalDistance = 0;
		}
	});
});

$(document).keydown(function(e){
	switch (e.keyCode){
		case 37 || 65:
			console.log( 'left pressed' );
			break;
		case 39:
			console.log( 'right pressed' );
			break;
		case 38:
			console.log( 'up pressed' );
			break;
		case 40:
      console.log( 'down pressed' );
			break;
    case 65:
			console.log( 'left pressed' );
			break;
		case 68:
			console.log( 'right pressed' );
			break;
		case 87:
			console.log( 'up pressed' );
			break;
		case 83:
			console.log( 'down pressed' );
			break;
	}
});
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console