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

              
                #app
              
            
!

CSS

              
                *
  box-sizing border-box
  
body
  display grid
  place-items center
  min-height 100vh
  
#app
  display flex
  flex-direction column
  justify-content center
  align-items center
  gap 1rem
  
label
  font-family sans-serif
  font-weight bold
  font-size 2rem
  
input
  width 300px
  
.slider
  display inline-block
  position relative
  width 300px
  
  & > div
    position absolute
    height 1px
    width 1px
              
            
!

JS

              
                import React from "https://cdn.skypack.dev/react";
import ReactDOM from "https://cdn.skypack.dev/react-dom";
import gsap from "https://cdn.skypack.dev/gsap";

gsap.registerPlugin(InertiaPlugin);
gsap.registerPlugin(Observer);

const KNOCK = new Audio('https://assets.codepen.io/605876/pinball-knock.mp3')
const ROOT_NODE = document.querySelector("#app");

const SlideySlider = ({
  id,
  min = 0,
  max = 100,
  step = 1,
  value = 0,
  bump = 10,
}) => {
  const inputRef = React.useRef(null);
  const tweenRef = React.useRef(null);

  React.useEffect(() => {
    const TRACKER = InertiaPlugin.track(inputRef.current, "value")[0];
    const WRAP = gsap.utils.wrapYoyo(min, max);
    Observer.create({
      target: inputRef.current,
      type: "touch,pointer",
      dragMinimum: 3,
      onPress: () => tweenRef.current && tweenRef.current.kill(),
      onDragEnd: () => {
        let lastCycle = 0;
        tweenRef.current = gsap.to(inputRef.current, {
          inertia: {
            resistance: 200,
            value: "auto"
          },
          modifiers: {
            value: (v) => {
              const cycle = Math.floor(v / max);

              if (cycle !== lastCycle) {
                // Bounce!!!
                const vx = TRACKER.get("value");
                const xPercent = gsap.utils.clamp(
                  -bump,
                  bump,
                  gsap.utils.mapRange(-600, 600, -bump, bump, vx)
                );
                const duration = gsap.utils.clamp(
                  0.05,
                  0.2,
                  gsap.utils.mapRange(0, 600, 0.2, 0.05, Math.abs(vx))
                );
                const volume =
                  gsap.utils.clamp(
                    0.1,
                    1,
                    gsap.utils.mapRange(0, 600, 0, 1, Math.abs(vx))
                  )
                gsap.to(inputRef.current, {
                  onStart: () => {
                    KNOCK.pause()
                    KNOCK.currentTime = 0
                    KNOCK.volume = volume
                    KNOCK.play()
                  },
                  xPercent,
                  duration,
                  yoyo: true,
                  repeat: 1
                });
              }
              // Update the cycle count
              lastCycle = cycle;
              return WRAP(v);
            }
          }
        });
      }
    });
  }, []);

  return (
    <input
      id={id}
      ref={inputRef}
      type="range"
      min={min}
      max={max}
      step={step}
      defaultValue={value}
    />
  );
};

const App = () => {
  const [value, setValue] = React.useState(gsap.utils.random(0, 100, 1));

  return (
    <>
      <label htmlFor="slidey">{value}</label>
      <SlideySlider value={value} id="slidey" />
    </>
  );
};

ReactDOM.render(<App />, ROOT_NODE);

              
            
!
999px

Console