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 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.

            
              <!-- Sections -->
<form class="slider-form js-slider-form">
	<!-- Age slider -->
	<fieldset class="section-age">

		<!-- value gets updated on drag end in js -->
		<input type="hidden" name="age" value="0" />

		<div class="age-slider">

			<!-- Container for the gsap draggable -->
			<div class="age-slider-pointer-wrapper">
				<div class="age-slider-pointer">0</div>
			</div>

			<!-- Numbers -->
			<ul class="age-slider-makers">
				<li>0</li>
				<li>1</li>
				<li>2</li>
				<li>3</li>
				<li>4</li>
				<li>5</li>
			</ul>
			
		</div>
	</fieldset>
</form>
            
          
!
            
              html, body{
	height: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
	background: #97d8f9;
	font-family: 'open sans', sans-serif;
}

// Container
.slider-form{
	background: #fff;
	padding: 50px;
	border-radius: 100px;
	box-shadow: 0 2px 0 #B1B1B1, 0 3px 6px rgba(#000, 0.25);
}	

// Age slider
.section-age{
	position: relative;
	top: 10px;
	left: 15px;

	.age-slider{
		position: relative;
		width: 415px;
		margin: auto;
	}

	.age-slider-pointer-wrapper{
		position: relative;
		width: calc(100% - 65px);
		height: 24px;
		padding: 0 0 0 23px;
		background: #f2f2f2;
		border-radius: 12px;
		box-shadow: inset 0 3px 0 #dfdfdf;

		// Grid used for draggable (not visible)
		.pointer-grid{
			height: 100%;
			float: left;

			&:last-child{
				width: 23px !important;
			}
		}

		// Green bar
		&:before{
			content: '';
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);
			width: calc(100% - 12px);
			height: 10px;
			background: #b2d366;
			box-shadow: 0 3px 0 #89b02e;
			margin-top: -1px;
			border-radius: 5px;
		}
	}

	.age-slider-pointer{
		position: absolute;
		top: 50%;
		width: 35px;
		height: 35px;
		margin-top: -4px;
		transform: translate3d(-50%, -50%, 0);
		font-size: 20px;
		font-weight: 500;
		line-height: 38px;
		text-align: center;
		color: #fff;
		border-radius: 50%;
		background-color: #0067A5;
		box-shadow: 0 2px 0 darken(#0067A5, 10%), 0 5px 0 #bababa;
	}

	.age-slider-makers{
		clear: both;
		padding: 0 0 0 23px;
		overflow: hidden;
		text-align: left;
		font-size: 15px;
		font-weight: 700;
		margin-top: 0.75em;

		li{
			float: left;
			width: percentage(1/6);
			color: #bababa;
			text-indent: -0.2em;

			&:last-child{
				text-indent: -0.4em;
			}
		}
	}
}
            
          
!
            
              // Helper to get closest value in array
function arrayClosest(arr, num) {
	var curr = arr[0];
	var diff = Math.abs (num - curr);
	for (var val = 0; val < arr.length; val++) {
			var newdiff = Math.abs (num - arr[val]);
			if (newdiff < diff) {
					diff = newdiff;
					curr = arr[val];
			}
	}
	return curr;
}

// Age slider
(function ageSlider(){

	var $container = $('.age-slider-pointer-wrapper'),
			$wrapper = $('.age-slider'),
			$pointer = $('.age-slider-pointer'),
			$markers = $('.age-slider-makers li'),
			$input = $('.section-age input[name="age"]'),
			gridColumns = $markers.length,
			leftNotches, pointerWidth, i, left;

	// Create slider
	var draggable = Draggable.create($pointer, {
		bounds: $wrapper,
		type: 'x',
		throwProps:true,
		minDuration: 0.25,
		maxDuration: 1.5,
		overshootTolerance: 0,
		onThrowComplete: function(){ // Update values at end of animation
			
			var ageValue = arrayClosest(leftNotches, Math.round(this.endX));
					ageValue = leftNotches.indexOf(ageValue);
					$input.val( ageValue );
					$pointer.text( ageValue );
		},
		onDrag: function(){ // Update slider value during drag

			var ageValue = arrayClosest(leftNotches, Math.round(this.x));
					ageValue = leftNotches.indexOf(ageValue);
					$pointer.text( ageValue );
		}
	});

	// Watch for click on wrapper
	$container.on('click.homepage', function(event){

		var offset = event.offsetX - pointerWidth,
				closest = arrayClosest(leftNotches, Math.round(offset)),
				ageValue = leftNotches.indexOf(closest),
				target = draggable[0].target;

		if( ! $(event.target).is('.age-slider-pointer') ){

			// Move marker
			TweenLite.to(target, 0.5, { x: closest, ease: Power3.easeOut });

				// Update values
				$input.val( ageValue );
				$pointer.text( ageValue );
			}
		});

	// Set dynamic values on init and resize
	function updateSlider(){

		var gridWidth = $markers.first().width(),
				slider = draggable[0];

		leftNotches = [];
		pointerWidth = $pointer.width();

		// Update grid
		for (i = 0; i < gridColumns; i++) {
			left = (i * gridWidth) % (gridColumns * gridWidth);
			leftNotch = left - (pointerWidth/2);
			if (i < gridColumns) {
					leftNotches.push(leftNotch);
			}
		}

		slider.applyBounds();
		slider.vars.snap = leftNotches;
	}
	updateSlider();

	// Resize
	$(window).on('throttledresize.homepage', function(){
		updateSlider();
	});
})();
            
          
!
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