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.

            
              <!-- 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
🕑 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