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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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

button(title="show value", aria-pressed="false")
  svg(viewBox="0 0 640 512" width="100" title="eye-slash")
    path(d="M320 400c-75.85 0-137.25-58.71-142.9-133.11L72.2 185.82c-13.79 17.3-26.48 35.59-36.72 55.59a32.35 32.35 0 0 0 0 29.19C89.71 376.41 197.07 448 320 448c26.91 0 52.87-4 77.89-10.46L346 397.39a144.13 144.13 0 0 1-26 2.61zm313.82 58.1l-110.55-85.44a331.25 331.25 0 0 0 81.25-102.07 32.35 32.35 0 0 0 0-29.19C550.29 135.59 442.93 64 320 64a308.15 308.15 0 0 0-147.32 37.7L45.46 3.37A16 16 0 0 0 23 6.18L3.37 31.45A16 16 0 0 0 6.18 53.9l588.36 454.73a16 16 0 0 0 22.46-2.81l19.64-25.27a16 16 0 0 0-2.82-22.45zm-183.72-142l-39.3-30.38A94.75 94.75 0 0 0 416 256a94.76 94.76 0 0 0-121.31-92.21A47.65 47.65 0 0 1 304 192a46.64 46.64 0 0 1-1.54 10l-73.61-56.89A142.31 142.31 0 0 1 320 112a143.92 143.92 0 0 1 144 144c0 21.63-5.29 41.79-13.9 60.11z")
  svg(viewBox="0 0 576 512" width="100" title="eye")
    path(d="M572.52 241.4C518.29 135.59 410.93 64 288 64S57.68 135.64 3.48 241.41a32.35 32.35 0 0 0 0 29.19C57.71 376.41 165.07 448 288 448s230.32-71.64 284.52-177.41a32.35 32.35 0 0 0 0-29.19zM288 400a144 144 0 1 1 144-144 143.93 143.93 0 0 1-144 144zm0-240a95.31 95.31 0 0 0-25.31 3.79 47.85 47.85 0 0 1-66.9 66.9A95.78 95.78 0 1 0 288 160z")
              
            
!

CSS

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

:root
  --track-width 300px

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

  &__proxy
    height 48px
    width 48px
    position absolute
    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

const KNOCK = new Audio('https://assets.codepen.io/605876/pinball-knock.mp3')

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, {
        overwrite: false,
        x: gsap.utils.mapRange(min, max, radius, width - radius, value),
      })
  }
}
let bumping
const bumpContainer = coeff => {
  if (bumping !== false && coeff === bumping) return
  bumping = coeff
  const vx = TRACKER.get('x')
  const xPercent =
    gsap.utils.clamp(
      0,
      BUMP,
      gsap.utils.mapRange(0, 1000, 0, BUMP, Math.abs(vx)),
    ) * coeff
  const duration = gsap.utils.clamp(
    0.05,
    0.2,
    gsap.utils.mapRange(0, 1000, 0.2, 0.05, Math.abs(vx)),
  )
  const volume =
    gsap.utils.clamp(
      0.1,
      1,
      gsap.utils.mapRange(0, 1000, 0, 1, Math.abs(vx))
    )
  // const xPercent = BUMP * coeff
  gsap.to(CONTAINER, {
    onStart: () => {
      KNOCK.pause()
      KNOCK.currentTime = 0
      KNOCK.volume = volume
      KNOCK.play()
    },
    onComplete: () => {
      bumping = false
    },
    xPercent,
    yoyo: true,
    repeat: 1,
    duration,
  })
}

INPUT.addEventListener('change', syncInput, false)
VALUE_TOGGLE.addEventListener('click', () => {
  gsap.killTweensOf([PROXY, CONTAINER])
  const PRESSED =
    VALUE_TOGGLE.getAttribute('aria-pressed') === 'false' ? 'true' : 'false'
  VALUE_TOGGLE.setAttribute('aria-pressed', PRESSED)
  gsap.set(LABEL, {
    '--opacity': PRESSED === 'true' ? 1 : 0,
  })
})

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
    bumpContainer(1)
    hitting = true
  } else if (x - r < 0) {
    xPos = r
    vx *= friction
    bumpContainer(-1)
    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,
});
document.querySelector(".slider__input").addEventListener("pointerdown", e => dragHandle.startDrag(e, true));

              
            
!
999px

Console