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

              
                <div id="app">
  
  <h1><strong>Scrubber</strong> for Vue.js</h1>
  
  <p>Use your mouse to drag over any scrubber field to change its value, type a value or use arrow up/down to increase/decrease the value.</p>
  
  <p>A scrubber is constrained by minimum & maximum values. Steps define the granularity of change.</p>
  
  <p><strong>Use: </strong><code>{{ demo }}</code></p>

  <div class="parameters">

    <h3>Demo</h3>

    <label for="">
      <span>Value</span>
      <scrubber :value="value" :min="min" :max="max" :steps="steps"></scrubber>           </label>

  </div>


  <div class="parameters">
    <h3>Props</h3>

    <label for="">
      <span>Min</span>
      <scrubber :value.sync="min" :min="0" :max="max" :steps="1">  </scrubber>
    </label>

    <label for="">
      <span>Max</span>
      <scrubber :value.sync="max" :min="0" :steps="2"></scrubber>
    </label>


    <label for="">
      <span>Steps</span>
      <scrubber :value.sync="steps" :min="0" :max="10" :steps="0.1"></scrubber>
    </label>
  </div>

</div>
              
            
!

CSS

              
                .vue-scrubber {
  font-size: 15px;
  padding: 0.5em;
  border: 0;

  cursor: ew-resize;
  background: rgba(0, 0, 0, 0.08);
  color: white;
  -webkit-user-select: none;
  
font-feature-settings:"lnum" 1;
}

.vue-scrubber:hover, .vue-scrubber:focus {
  outline: 0;
  box-shadow: none;
}


/** Demo CSS */

body {
  font-family: "Helvetica Neue", "Arial", sans-serif;
  background: #6247AA;
  color: #E2CFEA;
  -webkit-font-smoothing: antialiased;
  
  padding: 2rem;
  max-width: 800px;
  margin: 0 auto;
}

h1 {
  font-weight: 100;
  letter-spacing: 2px;
}

h1 strong {
  letter-spacing: 0;
}
p {
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  opacity: 0.8;
  font-size: 1rem;
}

.parameters {
  margin-top: 4rem;
  background: #6247AA;
}

label {
  display: block;
  margin-bottom: 2rem;
}

label span {
  display: inline-block;
  width: 100px;
}

code {
  font-family: "Monaco", monospaced;
  font-size: 15px;
}
              
            
!

JS

              
                // Scrubber component
// Lets the user change an input field value by dragging the mouse left/right.
// Manual text input is still possible.

Vue.component('scrubber', {
  data: function() {
    return {
      isMouseDown: false,
      initialMouse: null,
      value: 0
    }
  },
  computed: {
    
    // returns the number of decimals based on the step value
    // e.g. "0.25" returns "2"
    decimals: function() {
      return this.steps.toString().substr((this.steps).toString().indexOf(".")).length - 1;
    },
    
    // every time the value changes, we need to make sure it stays inside the min/max
    constrainedValue: function() {
      return this.constrain(this.value, this.min, this.max, this.decimals);
    }
  },
  
  // props that the scrubber can receive
  // value: initial value
  // min: minimum value
  // max: maximum value
  // steps: increments for each pixel the mouse is moved
  props: ["value", "min", "max", "steps"],
  
  // the template
  template: "<input class='vue-scrubber' v-model='constrainedValue' v-on:mousedown='handleMouseDown' v-on:keypress='handleInput' v-on:keydown.up='handleKeyCodeUp' v-on:keydown.down='handleKeyCodeDown' v-on:change='handleChange' />",

  methods: {
    
    // constrains a number to not exceed the min/max
    // decimals: rounding precision
    constrain: function(value, min, max, decimals) {
      decimals = typeof decimals !== 'undefined' ? decimals : 0;

      if (min != undefined && max != undefined) {
        return this.round(Math.min(Math.max(parseFloat(value), min), max), decimals);
      } else {
        return value;
      }
    },
    
    // method to round a number to given decimals
    round: function(value, decimals) {
      return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
    },
    handleInput: function (event) {
      // only allow numeric keys
      if (event.keyCode < 48 || event.keyCode > 57) {
        event.preventDefault();
      }
    },
    
    handleChange: function (event) {

      this.value = isNaN(parseFloat(event.target.value)) ? 0 : parseFloat(event.target.value);
    },
    
    handleKeyCodeUp: function (event) {
      event.preventDefault();
        this.value += parseFloat(this.steps);
    },
    
    handleKeyCodeDown: function (event) {
      event.preventDefault();
        this.value -= parseFloat(this.steps);
    },
    
    // mouse handler
    handleMouseDown: function(event) {

      // enable scrubbing
      this.mouseDown = true;

      // remember the initial mouse position when the scubbing started
      this.initialMouse = {
        x: event.clientX,
        y: event.clientY
      }

      // remember the initial value
      this.initialValue = this.value;

      // register global event handlers because now we are not bound to the component anymore
      document.addEventListener("mousemove", this.handleMouseMove)

      // global mouse up listener
      document.addEventListener("mouseup", this.handleMouseUp)
      
    },
    handleMouseUp: function($event) {

      // disable scrubbing
      this.mouseDown = false;
      
      document.removeEventListener("mousemove", this.handleMouseMove)
      document.removeEventListener("mouseup", this.handleMouseUp)

    },
    
    // the actual translation of mouse movement to value change…
    handleMouseMove: function(event) {

      // scrub if the mouse is being pressed
      if (this.mouseDown) {
        var newValue = this.initialValue + ((event.clientX - this.initialMouse.x) * this.steps)

        // constrain the value to the min/max
        this.value = this.constrain(newValue, this.min, this.max, this.decimals);

      }
    }
  }
})

// demo app with global data properties that get passed down the a few scrubbers

var app = new Vue({
  el: "#app",
  data: {
    min: 0,
    max: 200,
    steps: 1,
    value: 100,
    demo: "<scrubber :min='10' :max='100' :steps='1' :value='50'></scrubber>"
  }
})
              
            
!
999px

Console