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

              
                <div class="container">
<section class="clear">
			<header class="list-header">
				<div class="circles">
					<div></div>
					<div></div>
					<div></div>
				</div>
					
				<h1>Things To Do</h1>

				<div class="menu">
					<div></div>
					<div></div>
					<div></div>
				</div>
			</header>
				
			<ul class="task-list">
				<li class="task">
					Click and drag this item right to mark it as complete
				</li>
				<li class="task">
					Drag left to delete an item
				</li>
				<li class="task">
					Don't edit anything in the HTML or CSS
				</li>
				<li class="task">
					Try to use only JavaScript, no third-party libraries
				</li>
			</ul>
		</section>
  
  <br>
  
  <p><a href="https://gallery.mailchimp.com/282459d63a019df7bb8ca2e92/images/800d93e4-bfd6-4749-8dd0-f2c4fb784c17.gif">See Demo</a></p>

	</div>
              
            
!

CSS

              
                /* RESETTING */

html { box-sizing: border-box; font-size: 62.5%;  }
*, *:before, *:after { box-sizing: inherit; }
body { font-size: 1.6rem; font-weight: 300; line-height: 1.6; font-family: 'Open Sans', sans-serif; }
h1 { font-size: 2rem; }
a { color: inherit; }


/* LAYOUT */

.container { width: 90%; max-width: 500px; margin: 100px auto; }

/* CLEAR ELEMENT */
.clear { margin: 30px 0; }

.list-header { display: flex; align-items: center; background-color: #e0dee0; border-top-left-radius: 5px; border-top-right-radius: 5px; padding: 10px 20px; }

.list-header div { width: 20%; }

.list-header .circles div { height: 15px; width: 15px; border-radius: 50%; display: inline-block; margin-right: 5px; }

.list-header .circles div:nth-child(1) { background-color: #fc615d; }

.list-header .circles div:nth-child(2) { background-color: #fec342; }

.list-header .circles div:nth-child(3) { background-color: #34c749; }

.list-header .menu { display: flex; align-items: flex-end; flex-direction: column; }

.list-header .menu div { width: 25px; height: 3px; background-color: #a9a9a9; border-radius: 3px; }

.list-header .menu div:nth-child(2) { margin: 4px 0; }

.list-header h1 { width: 60%; text-align: center; }

/* Task List */
.task-list { background-color: #272822; min-height: 400px; overflow: hidden; }

/* Individual Task */
.task { color: #fff; width: 100%; height: auto; min-height: 50px; line-height: 2; position: relative; padding: 8px 20px; height: auto; transition: background-color 1s; }

.task:before, .task:after { position: absolute; top: 0; width: 40px; height: 50px; line-height: 50px; text-align: center; }

.task:before { content: "\2714"; left: -40px; }

.task:after { content: "\2718"; right: -40px; }

.task:not(.completed):not(.completing) { border-bottom: 2px solid #d44a29; }

.task:nth-child(1) { background-color: #e3252c; }

.task:nth-child(2) { background-color: #dc342a; }

.task:nth-child(3) { background-color: #e7522e; }

.task:nth-child(4) { background-color: #e86d2f; }

.task:nth-child(5) { background-color: #e86d2f; }

.task.completing { text-decoration: line-through; background-color: green; }

.task.completed { opacity: 0.5; text-decoration: line-through; background-color: #323232; }

.task.completed:before { content: ""; }

.task.deleting { background-color: #323232; }

              
            
!

JS

              
                /*

  THINGS TO NOTE
		- Each task list item has a class of .task
		- When the task is being swiped right, try adding the class of .completing
		- When the task is being swiped left, try adding the class of .deleting
		- A completed task should have a class of .completed
    
    - The check and crosses are added as :before and :after pseudo-elements of the .task li so you don't need to add them
    
      GOOD LUCK!

*/


var tasks = document.getElementsByClassName("task");


/* ***************

	HELPER FUNCTIONS

*************** */

/* FUNCTION TO GET CURRENT MARGIN OF ELEMENT 
   (ALSO CONVERT TO % MARGIN IF IN PIXELS) */
function getCurrentMargin(element) {

	var currentMargin = element.style.marginLeft;

	if ( currentMargin.indexOf("px") > -1 ) {
		// Convert px margin to percentage 
		currentMargin = currentMargin.split("px")[0];
		var marginInPercentage = ( currentMargin / element.offsetWidth ) * 100;
		currentMargin = marginInPercentage;

	} else if ( currentMargin.indexOf("%") > -1 ) {
		currentMargin = currentMargin.split("%")[0];

	} else {
		currentMargin = 0;
	}

	return parseInt(currentMargin);
}



/* FUNCTION TO HANDLE SWIPING OF LIST ELEMENTS  */
function swipeElement(elementID, swipingRight, swipingIn) {

	var element = document.getElementById(elementID);

	// Swiping an element out - marking as completed or done
	function swipeElementOut() {
		var currentMargin = getCurrentMargin(element),
			currentMargin = swipingRight ? currentMargin += 1 : currentMargin -= 1;
	
		element.style.marginLeft = currentMargin + '%';

		if ( currentMargin == 100 | currentMargin == -100 ) {
			
			clearInterval(interval);

			// Either append completed item to bottom or delete item
			swipingRight ? appendCompletedItem(element) : element.remove();
		}
	}

	// Swiping an element in - appending completed item to list
	function swipeElementIn() {
		var currentMargin = getCurrentMargin(element);
			currentMargin -= 1;

		element.style.marginLeft = currentMargin + '%';

		if ( currentMargin == 0 ) { clearInterval(interval); }
	}

	var interval = swipingIn ? setInterval(swipeElementIn, 5) : setInterval(swipeElementOut, 5);
}


/* FUNCTION TO HANDLE ADDING COMPLETED ITEM TO BOTTOM OF LIST */
function appendCompletedItem(element) {
	element.classList.remove('completing');
	element.classList.add('completed');

	document.getElementsByClassName("task-list")[0].appendChild(element);

	swipeElement(element.id , false, true);
}




/* ***************

	EVENT LISTENER HANDLERS

*************** */

/* HANDLE WHEN MOUSE MOVES */
var handleMouseMove = function(event) {

	if ( cursorXPosition === 0 ) { cursorXPosition = event.x; }

	// Set the margin-left of the list item to move with the mouse
	cursorXPositionDiff = event.x - cursorXPosition;
	event.target.style.marginLeft = cursorXPositionDiff + 'px';

	var taskIsNotCompleted = !(event.target.className.indexOf("completed") > -1);

	// ADD CLASS IF THE cursorXPositionDiff GETS TO A CERTAIN AMOUNT
	if ( cursorXPositionDiff > 40 && taskIsNotCompleted ) {
		event.target.classList.add('completing');

	} else if ( cursorXPositionDiff < -40 ) {
		event.target.classList.add('deleting');

	} else {
		event.target.classList.remove('completing');
		event.target.classList.remove('deleting');
	} 
}


/* HANDLE WHEN MOUSE IS RELEASED */
var handleMouseUp = function(event) {

	var className = event.target.className;

	if ( className.indexOf('completing') > -1 ) {
		// SWIPE AWAY CURRENT LIST ITEM (RIGHT)
		swipeElement(event.target.id, true);

	} else if ( className.indexOf('deleting') > -1 ) {
		// SWIPE AWAY CURRENT LIST ITEM (LEFT)
		swipeElement(event.target.id, false);

	} else {
		// REPOSITION LIST ITEM BACK TO NORMAL POSITION
		event.target.style.marginLeft = "";
	}

	this.removeEventListener("mousemove", handleMouseMove, false);
	addMouseDownEventListener();
}




/* ***************

	ADD EVENT LISTENERS

*************** */

function addMouseDownEventListener() {

	// Reset values to use in handleMouseMove()
	cursorXPosition = 0;
	cursorXPositionDiff = 0;

	// Loop through all tasks and add the event listener to each
	for ( var i = 0; i < tasks.length; i++ ) {
		tasks[i].addEventListener("mousedown", function(event) {
			this.addEventListener("mousemove", handleMouseMove, false);
			this.addEventListener("mouseup", handleMouseUp, false);
		})
	}

}



/* ***************

	INIT

*************** */

var cursorXPosition = 0;
var cursorXPositionDiff = 0;

// Add IDs to all Task Items
for ( var i = 0; i < tasks.length; i++ ) {
	tasks[i].id = "task_" + i;
}

addMouseDownEventListener();


              
            
!
999px

Console