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

              
                <h1>AngularJS Responsive Multi-range slider</h1>
<div ng-app="slider">
  <div ng-controller="Ctrl" class="main">

    <h2>Here's one with a simple array of numbers.</h2>
    <p>
    model: {{otherProbs}}
    </p>
    <slider model="otherProbs" ></slider>

    <h2>Here's one with an array of objects and a setp size.</h2>
    <p>
    model: {{probs}}<br>
    property: "p"<br>
    step: .05
    </p>
    <slider model="probs" property="p" step=".05"></slider>

    
      
      
  </div>
  
</div>
              
            
!

CSS

              
                .main {
  width: 50%;
}

.slider-control {
  height: 1em;
  padding-top: .5em;
}

.slider {
  height: 2px;
  border: 1px solid black;
}

.slider-handle {
  height: 1em;
  width: 1em;
  border: 1px solid black;
  border-radius: 2px;
  background: #fff;
  cursor: pointer;  
}
.slider-handle:hover {
  background: #ddd;
}
              
            
!

JS

              
                angular.module("slider", [])

angular.module("slider").directive("slider", ($document, $timeout)->
  restrict: "E"
  scope:              
    model: "="
    property: "@"
    step: "@"
  replace: true
  template: """
<div class="slider-control">
<div class="slider">
</div>
</div>
  """
  link: (scope, element, attrs)->
    
    # It's the inner div we're really working with.
    element = element.children()
    element.css('position', 'relative')

    handles = []
    pTotal = 0
    
    step = ()->
      if(scope.step?)
        parseFloat(scope.step)
      else
        0

    getP = (i) -> if scope.property? then scope.model[i][scope.property] else scope.model[i]
    setP = (i, p) ->
      s = step()
      if s > 0
        p = Math.round(p/s) * s
      if scope.property?
        scope.model[i][scope.property] = p
      else
        scope.model[i] = p

        
    updatePositions = ()->
      pTotal = scope.model.reduce (sum, item, i)->
        sum+getP(i)
      , 0
      
      pRunningTotal = 0
      for handle,i in handles
        p = getP(i)
        pRunningTotal += p
        x = pRunningTotal/pTotal * 100 #element.prop("clientWidth")
        handle.css
          left: x + "%"
          top: "-" + handle.prop("clientHeight")/2 + "px"

    for mv,i in scope.model
      do (mv, i)->
        return if i == scope.model.length-1
        handle = angular.element('<div class="slider-handle"></div>')
        handle.css("position", "absolute")
        handles.push(handle)
        element.append(handle)
      
        startX = 0
        startPleft = startPright = 0
        handle.on "mousedown", (event) ->
          mousemove = (event) => scope.$apply ()->
            dp = (event.screenX - startX) / element.prop("clientWidth") * pTotal
            return if dp < -startPleft or dp > startPright
            setP(i, startPleft+dp)
            setP(i+1, startPright-dp)
            updatePositions()

          mouseup = ->
            $document.unbind "mousemove", mousemove
            $document.unbind "mouseup", mouseup

          # Prevent default dragging of selected content
          event.preventDefault()
          startX = event.screenX
          startPleft = getP(i)
          startPright = getP(i+1)
          $document.on "mousemove", mousemove
          $document.on "mouseup", mouseup
        
    scope.$watch "model", updatePositions, true
)

angular.module("slider").controller("Ctrl", ($scope)->
  $scope.probs = [
    p: .1
  ,
    p: .5
  ,
    p: .4
  ]
  
  $scope.otherProbs = [3, 3, 4]
)
  
  
              
            
!
999px

Console