cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

            
              <form action="" method="post">

<div class="form-input">

<label for="myinput">Select a Value</label>
<select name="myinput" id="myinput">
	<option value="1">This</option>
	<option value="2" selected>That</option>
	<option value="3">The other</option>
	<option value="4">Another</option>
	<option value="5">Neither</option>
</select>

</div>

</form>
            
          
!
            
              .form-input {
	position: relative;
	max-width: 400px;
	margin: 50px auto;
	font-family: sans-serif;
}

.form-input label {
	display: block;
	padding: .5em 0;
}

.slider {
	position: relative;
	width: 100%;
	margin: 20px 0 20px 0;
}

.slider-container {
	position: relative;
	box-sizing: border-box;
	width: 100%;
	height: 48px;
	padding: 6px;
	background: #ddd;
	border-radius: 4px;
}

.slider-channel {
	box-sizing: border-box;
	margin: 0;
	height: 36px;
	background: #f8f8f8;
	border: 1px solid #467;
	border-radius: 4px;
	box-shadow: 0 0 2px 1px rgba(255,255,255,.9);
}

.slider-slider {
	position: absolute;
	top: 7px;
	left: 0;
	z-index: 10;
	width: 58px;
	height: 32px;
}

.slider-slider:after {
	display: block;
	position: absolute;
	top: 0px;
	left: 7px;
	width: 42px;
	height: 32px;
	content: '';
	background: #467;
	border: 1px solid #fff;
	border-radius: 4px;
	box-shadow: 0 0 1px rgba(0,0,0,.9);
}

.slider-min {
	position: relative;
	margin-top: 20px;
}

.slider-max {
	position: absolute;
	right: 0;
	bottom: 0;
}

.slider-plus,
.slider-minus {
	position: absolute;
	top: 0;
	right: 6px;
	padding: .5em;
	height: 1em;
	width: 1em;
	color: #467;
	line-height: 1em;
	text-align: center;
	background: #f8f8f8;
	border: 1px solid #467;
	border-radius: 4px;
	box-shadow: 0 0 0 6px #ddd;
}

.slider-minus {
	right: 3em;
}
            
          
!
            
              var sliders = [];

var Slider = function( input ) {

	var that = this;

	this.input = input;
	this.minText;
	this.maxText;
	this.min;
	this.max;
	this.increment;
	this.options;
	this.currentValue;
	this.val;
	this.slider;
	this.plus;
	this.minus;

	this.init = function() {
		this.currentValue = 0;
		if ( this.input.type == 'select-one' ) {
			this.setOptions();
		}
		else {
			this.setDataAttr();
		}
		
		this.build();
		this.setSliderPosition();
		this.hideInput();
		this.setEvents();
	}

	this.setOptions = function() {
		var options = this.input.options;
		var val = 0;
		
		this.options = {};
		for ( var i = 0; i < options.length; i++ ) {
			this.options[i] = {
				'value': options[i].value,
				'text': options[i].text
			};
			if ( this.input.value == options[i].value ) {
				this.currentValue = i;
			}
		}
		
		this.min = 0;
		this.minText = this.options[0].text;
		this.max = options.length - 1;
		this.maxText = this.options[options.length - 1].text;
		this.increment = 1;
	}

	this.setDataAttr = function() {
		this.min 			= this.input.getAttribute( 'data-min' );
		this.minText 	= this.input.getAttribute( 'data-min-text' );
		this.max 			= this.input.getAttribute( 'data-max' );
		this.maxText 	= this.input.getAttribute( 'data-max-text' );
		this.increment 	= this.input.getAttribute( 'data-increment' );

		this.currentValue = +this.input.value;
	}

	this.build = function() {

		var d = this.create( 'div', 'slider', this.input.parentNode );
		var dc = this.create( 'div', 'slider-container', d );
		var sc = this.create( 'div', 'slider-channel', dc );
		this.slider = this.create( 'div', 'slider-slider', dc );
		var dmin = this.create( 'div', 'slider-min', d );
		dmin.textContent = this.minText;
		var dmax = this.create( 'div', 'slider-max', d );
		dmax.textContent = this.maxText;

		var l = this.input.parentNode.getElementsByTagName('label')[0];
		l.textContent = l.textContent + ': ';

		this.val = this.create( 'span', 'slider-value', l );
		this.setVal( this.currentValue );
		
		this.plus = this.create( 'div', 'slider-plus', input.parentNode );
		this.plus.textContent = '+';

		this.minus = this.create( 'div', 'slider-minus', input.parentNode );
		this.minus.textContent = '-';
	}

	this.create = function( el, cl, par ) {
		var n = document.createElement( el );
		n.className = cl;
		par.appendChild( n );
		return n;
	}

	this.hideInput = function() {
		this.input.style.display = 'none';
	}

	this.setEvents = function() {
		addEvent( 'touchstart', this.slider, this.move );
		addEvent( 'touchmove', this.slider, this.move );
		addEvent( 'click', this.plus, this.add );
		addEvent( 'click', this.minus, this.subtract );
	}

	this.move = function(e) {

		if ( typeof e == 'undefined' ) e = window.event;
		e.preventDefault();

		var s = e.target;
		var tX = e.changedTouches[0].pageX;
		var sRect = s.getBoundingClientRect();
		var parentRect = s.parentNode.getBoundingClientRect();
		var minX = parentRect.left;
		var maxX = parentRect.right - s.offsetWidth;

		var newX = tX - minX - ( s.offsetWidth / 2 );

		if ( newX < 0 ) {
			newX = 0;
		}
		else if ( newX > s.parentNode.offsetWidth - s.offsetWidth ) {
			newX = s.parentNode.offsetWidth - s.offsetWidth;
		}

		s.style.left = newX + 'px';

		var percent = newX / ( maxX - minX );
		var range = that.max - that.min;
		var val = +that.min + range*percent;
		val = Math.floor( val / that.increment ) * that.increment;
		that.setVal( val );
	}

	this.add = function(e) {
		if ( typeof e == 'undefined' ) e = window.event;
		e.preventDefault()
		var val = +that.currentValue + +that.increment;
		if ( val > that.max ) val = that.max;
		that.setVal( val );
		that.setSliderPosition();
	}

	this.subtract = function(e) {
		if ( typeof e == 'undefined' ) e = window.event;
		e.preventDefault();
		var val = +that.currentValue - +that.increment;
		if ( val < that.min ) val = that.min;
		that.setVal( val );
		that.setSliderPosition();
	}

	this.setSliderPosition = function() {
		var rangeX = this.slider.parentNode.offsetWidth - this.slider.offsetWidth;
		var percent = ( +this.currentValue - this.min ) / ( this.max - this.min );
		var newX = Math.floor( rangeX * percent );
	
		this.slider.style.left = newX + "px";	}

	this.setVal = function( val ) {
		this.currentValue = val;
		if ( typeof this.options == 'object' ) {
			this.setSelectVal( val );
		}
		else {
			this.setInputVal( val );
		}
	}

	this.setInputVal = function( val ) {
		this.val.textContent = val.toFixed();
		this.input.value = val.toFixed();
	}

	this.setSelectVal = function( val ) {
		this.val.textContent = this.options[val].text;
		this.input.value = this.options[val].value;
	}
}

var addEvent = function( type, obj, func ) {
	if ( obj && obj.addEventListener ) {
		obj.addEventListener( type, func, false );
	}
	else if ( obj && obj.attachEvent ) {
		obj.attachEvent( 'on' + type, func );
	}
}

var init = function() {
	var forms = document.forms;
	var inputs = forms[0].elements;
	var count = inputs.length;
	for ( var i = 0; i < count; i++ ) {
		sliders[i] = new Slider( inputs[i] );
		sliders[i].init();
	}
}

init();

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console