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

              
                <div class="custom-slider">
  <div class="custom-slider-track">
    <div class="custom-slider-button">
    </div>
  </div>
</div>
              
            
!

CSS

              
                html { background: #eee; }

.custom-slider { width: 100%; height: 7em; padding: 1em; box-sizing: border-box; }
.custom-slider-track { width: 100%; height: 100%; background: #2d89ef; box-sizing: border-box; position:relative; }
.custom-slider-button { width: 5em; height: 5em; background: #2b5797; box-sizing: border-box; position:absolute; top:0; left:0; }
.custom-slider-button.active { background:  #ff0097; }

.custom-slider-button {	user-select: none; -moz-user-select: none; -khtml-user-select: none; -webkit-user-select: none; }
              
            
!

JS

              
                /* to make the mouse slider work for touch, we need to add a boatload of
   new logic - add all event listeners, add conditional handling inside
   our existing functions. one thing we don't need for touch is binding
   to document, since touch events have implicit capture */

var t = document.querySelector(".custom-slider-button");

t.addEventListener('mousedown', sliderdown, true);
/* double-up event listeners */
t.addEventListener('touchstart', sliderdown, true);
/* and because touch events have implicit capture, bind events directly to element */
t.addEventListener('touchmove', slidermove, true);
t.addEventListener('touchend', sliderup, true);
t.addEventListener('touchcancel', sliderup, true);


function sliderdown(e) {
	t.classList.add("active");
	// bind late
	if (e.type == 'mousedown') {
		document.addEventListener('mouseup', sliderup, true);
		document.addEventListener('mousemove', slidermove, true);
	} else {
    /* prevent scrolling, zooming, etc */
    e.preventDefault();
	}
}

function sliderup(e) {
	t.classList.remove("active");
	// unbind
	if (e.type == 'mouseup') {
		document.removeEventListener('mousemove', slidermove, true);
		document.removeEventListener('mouseup', sliderup, true);
	}
}

function slidermove(e) {
	var newpos;
  /* handle the different way in which mouse and touch events give us coordinates */
	if (e.type == 'mousemove') {
		newpos = e.clientX - t.parentElement.offsetLeft - (t.offsetWidth/2);
	} else {
		newpos = e.targetTouches[0].clientX - t.parentElement.offsetLeft - (t.offsetWidth/2);
	}
	
	if (newpos < 0) {
		newpos = 0;
	} else if ( newpos > (t.parentElement.offsetWidth - t.offsetWidth)) {
		newpos = t.parentElement.offsetWidth - t.offsetWidth;
	}
	t.style.left = newpos +'px';
}
              
            
!
999px

Console