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

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="bounds">
		<div class="static">
			<p>This is a non-draggable element</p>
			<p>the boxes shouldn't be dragging over</p>
		</div>
		<div class="static">
			<p>This is another non-draggable</p>
			<p>element placed randomly</p>
		</div>
		<div class="box">box 1</div>
		<div class="box">box 2</div>
		<div class="box">box 3</div>
		<div class="box">box 4</div>
		<div class="box">box 5</div>
	</div>
</main>
              
            
!

CSS

              
                main {
	position: relative;
	background-color: #CCD1D9;
	width: 100%;
	height: 100%;
	padding: 64px;
	display: flex;
	justify-content: center;
	align-items: center;
}

.bounds {
	background-color: #fff;
	width: 100%;
	max-width: 1000px;
	height: 600px;

	.static {
		position: absolute;
		background-color: #A0D468;
		padding: 12px;
		border-radius: 8px;
		z-index: 1;
		pointer-events: none;
		
		&.overlapping {
			background-color: #ED5565;
		}
	}

	.box {
		position: absolute;
		background-color: #E6E9ED;
		width: 200px;
		padding: 24px;
		border: 1px solid #CCD1D9;
		border-radius: 12px;
		text-align: center;
		z-index: 2;
	}
}

              
            
!

JS

              
                /* just randomly placing the boxes for a more entertaining example */
document.querySelectorAll('.box').forEach(box => {
	gsap.set(box, {
		x: 'random(0, 800, 1)',
		y: 'random(0, 600, 1)'
	});
});

gsap.set(document.querySelectorAll('.static')[1], {
	x: 'random(200, 300, 1)',
	y: 'random(200, 400, 1)'
});

/* makind the boxes draggable */
Draggable.create('.box', {
	bounds: '.bounds',
	type: 'x,y',
	edgeResistance: .8,
	inertia: true,
	onDrag: overlapHandler,
	onDragEnd: overlapHandler,
	onThrowComplete: overlapHandler
});

/* this where we check if any of the boxes is touching a static */
function overlapHandler()
{
	document.querySelectorAll('.static').forEach(static => {
		let isOverlapping = false;
		document.querySelectorAll('.box').forEach(box => {
			if (Draggable.get(box).hitTest(static)) {
				isOverlapping = true;
			}
		});
		
		static.classList[isOverlapping ? 'add' : 'remove']('overlapping');
	});
}

overlapHandler();
              
            
!
999px

Console