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

              
                .slider
  input.slider__input#range(type='range' min='0' max='100' value='0')
  .slider__proxy
  label.slider__label(for='range') 0
              
            
!

CSS

              
                *
*:after
*:before
  box-sizing border-box

:root
  --track-width 300px

body
  min-height 100vh
  display grid
  place-items center
  
.slider
  position relative
  
  &__input
    width var(--track-width)

  &__proxy
    background hsla(210, 80%, 50%, 0.5)
    height 48px
    width 48px
    position absolute
    border-radius 50%
    top 50%
    left 0
    transform translate(-50%, -50%)
    z-index 2
    
  &__label
    position absolute
    bottom 150%
    font-family sans-serif
    font-weight bold
    font-size 1.25rem
    left 50%
    transform translate(-50%, 0)
    opacity var(--opacity, 0)
    transition opacity 0.2s
    color hsl(0, 0%, 25%)
    
button
  height 48px
  width 48px
  position fixed
  top 1rem
  right 1rem
  appearance none
  border 0
  background none
  transform scale(var(--scale, 1))
  transition transform 0.2s
  
  &:hover
    --scale 1.1
    
  &:active
    --scale 0.9
  
  &[aria-pressed="false"]
    svg:nth-of-type(1)
      display block
    svg:nth-of-type(2)
      display none
  &[aria-pressed="true"]
    svg:nth-of-type(1)
      display none
    svg:nth-of-type(2)
      display block

  svg
    width 100%
    position absolute
    top 50%
    left 50%
    fill hsl(0, 0%, 25%)
    transform translate(-50%, -50%)
              
            
!

JS

              
                import gsap from 'https://cdn.skypack.dev/gsap'

const { InertiaPlugin } = window

gsap.registerPlugin(Draggable)
gsap.registerPlugin(InertiaPlugin)

const friction = -0.5
const CONTAINER = document.querySelector('.slider')
const PROXY = document.querySelector('.slider__proxy')
const LABEL = document.querySelector('.slider__label')
const INPUT = document.querySelector('input')
const VALUE_TOGGLE = document.querySelector('button')
const PROXY_PROPS = gsap.getProperty(PROXY)
const TRACKER = InertiaPlugin.track(PROXY, 'x,y')[0]
const BUMP = 10

let animateBounce

gsap.defaults({
  overwrite: true,
})

const syncInput = e => {
  const range = PROXY_PROPS('x')
  const width = CONTAINER.getBoundingClientRect().width
  const radius = PROXY.getBoundingClientRect().width / 2
  const min = INPUT.min
  const max = INPUT.max
  const value =
    e && e.type === 'change'
      ? e.target.value
      : Math.floor(gsap.utils.mapRange(radius, width - radius, min, max, range))

  if (!isNaN(value)) {
    gsap.set(INPUT, { value })
    gsap.set(LABEL, { innerText: value })
    if (e && e.type === 'change')
      gsap.set(PROXY, {
        x: gsap.utils.mapRange(min, max, radius, width - radius, value),
      })
  }
}

INPUT.addEventListener('change', syncInput, false)

function checkBounds() {
  const radius = PROXY.getBoundingClientRect().width / 2
  let r = radius
  let x = PROXY_PROPS('x')
  let vx = TRACKER.get('x')
  let xPos = x

  let hitting = false

  syncInput()

  if (x + r > CONTAINER.getBoundingClientRect().width) {
    xPos = CONTAINER.getBoundingClientRect().width - r
    vx *= friction
    hitting = true
  } else if (x - r < 0) {
    xPos = r
    vx *= friction
    hitting = true
  }

  if (hitting) {
    animateBounce(xPos, vx)
  }
}

animateBounce = (x = '+=0', vx = 'auto') => {
  gsap.fromTo(
    PROXY,
    { x },
    {
      inertia: {
        x: vx,
      },
      onUpdate: checkBounds,
    }
  )
}

const dragHandle = new Draggable(PROXY, {
  bounds: CONTAINER,
  type: 'x',
  onPress: () => {
    gsap.killTweensOf(PROXY)
    dragHandle.update()
  },
  onDragEndParams: [],
  onDragEnd: animateBounce,
  onDrag: syncInput,
})

              
            
!
999px

Console