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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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 Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

              
                <!-- Inspired by the challenge posted in Reddit to make the worst possible UX for controlling the volume of a video.

https://uxdesign.cc/the-worst-volume-control-ui-in-the-world-60713dc86950 -->
              
            
!

CSS

              
                body {
  font-family: helvetica-neue, arial, sans-serif;
  background: #c1f4f7;
}

 .bubble {
  font-size: 15px;
  display: inline-block;
  cursor: pointer;
  margin-left: 5px;
  position: relative;
  width: 40px;
  height: 40px;
  text-align: center;
  line-height: 40px;
  padding: 2px;
  border-radius: 50%;
  box-shadow: inset 0 0 10px #727272;
 }

 .important {
  color: #CD0000;
  font-weight: bold;
 }

 .volumeIcon {
  width: 30px;
 }
              
            
!

JS

              
                const plusOrMinus = () => Math.random() < 0.5 ? -1 : 1;

class VolumeBubbles {
  constructor(size = 101) {
    // General
    this.selectedVolume = 0;
    this.animating = false;
    this.mouseBind = this.mouseHandler.bind(this)
    this.displaying = false;

    // Wrapper div
    this.wrapper = document.createElement('div');
    this.wrapper.classList.add('bubbleWrapper');
    this.wrapper.style.display = 'none';

    // Volume Icon
    this.icon = document.createElement('img');
    this.icon.src = 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Speaker_Icon.svg/500px-Speaker_Icon.svg.png';
    this.icon.classList.add('volumeIcon');
    this.icon.style.cursor = 'pointer';
    this.icon.addEventListener('click', this.displaySelect());

    // Volume indicator
    this.indicator = document.createElement('div');
    this.indicator.innerHTML = `Volume: ${this.selectedVolume}`;


    // Volume selectors (bubbles)
    this.elements = Array(size).fill('a').map((element, index) => {
      const number = size > 1 ? 100/(size-1) * index : 0;
      element = document.createElement('div');
      element.volumeValue = number;
      element.innerHTML = number;
      element.id = `bubble-${number}`;
      element.classList.add('bubble');
      element.style.cursor = 'pointer';
      element.addEventListener('click', this.selectVolume(number));
      element.addEventListener('click', this.displaySelect());
      if (number % 10 === 0) element.classList.add('important');
      this.wrapper.appendChild(element);
      return element;
    });

    // Volume selector speeds
    this.resetSpeeds();
  }

  animate() {
    this.elements.forEach((element, index) => {
      if (!element.position) this.initPositions();
      if (element.style.position !== 'fixed') element.style.position = 'fixed';
      const { position: {x, y}, speed } = element;
      element.position = {
        x: x+speed.x,
        y: y+speed.y,
      };   

      if (x < 0 || x > window.innerWidth) {
        element.speed.x = -1*speed.x;
        element.position.x = x < 0 ? 0 : window.window.innerWidth;
      }
      if (y < 0 || y > window.innerHeight) {
        element.speed.y = -1*speed.y;
        element.position.y = y < 0 ? 0 : window.window.innerHeight;
      }
    });

    this.drawPositions();
    this.animating = false;
  }

  selectVolume(number) {
    return () => {
      this.selectedVolume = number;
      this.indicator.innerHTML = `Volume: ${this.selectedVolume}`;
    }
  }

  // Randomize the positions for the bubbles -- not used atm
  randomPositions() {
    this.elements.forEach((element)=>{
      element.position = {
        x: Math.random()*(window.innerWidth-20),
        y: Math.random()*(window.innerHeight-20),
      };
      element.style.position = 'fixed';
    });

    this.drawPositions();
  }

  // Initialize the positions for the animations based on the current location of the bubble;
  initPositions() {
    this.elements.forEach((element) => {
      element.position = {
        x: element.getBoundingClientRect().left + window.scrollX,
        y: element.getBoundingClientRect().top + window.scrollY,
      };
    });
  }


  // Return the bubbles to their natural position
  resetPositions() {
    this.elements.forEach((element) => {
      element.style.position = 'relative';
      element.position = null;
      element.style.top = null;
      element.style.left = null;
    });
  }

  // Re-generate values for the speeds of the bubble
  resetSpeeds() {
    this.elements.forEach((element) => {
      element.speed = {
        x: plusOrMinus() * (Math.random()*(3-1) + 1),
        y: plusOrMinus() * (Math.random()*(3-1) + 1),
      }
    });
  }

  // Re-draw the position of the bubbles on the DOM
  drawPositions() {
    this.elements = this.elements.map((element) => {
      element.style.left = `${element.position.x}px`;
      element.style.top = `${element.position.y}px`;
      return element;
    });
  }

  mouseHandler(e) {
    if (!this.animating) {
      this.animating = true;
      requestAnimationFrame(this.animate.bind(this));
    }
  }

  displaySelect() {
    return () => {
      this.displaying = !this.displaying;
      this.wrapper.style.display = this.displaying ? 'block' : 'none';
      if (this.displaying) {
        this.resetPositions();
        this.resetSpeeds();
        document.addEventListener('mousemove', this.mouseBind);
      }
      else document.removeEventListener('mousemove', this.mouseBind);
    }
  }

  init() {
    document.body.appendChild(this.icon);
    document.body.appendChild(this.indicator);
    document.body.appendChild(this.wrapper);
  }
}

const volumeControl = new VolumeBubbles(21);
volumeControl.init();

              
            
!
999px

Console