Skip to content

Commit

Permalink
Optionally highlight track up to the handle - fixes loopj#7
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenvg committed Apr 11, 2013
1 parent 86caf5a commit 8bb0483
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 49 deletions.
11 changes: 9 additions & 2 deletions css/simple-slider-volume.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
-webkit-radial-gradient( 50% 50%, 200% 50%, hsla(0,0%,90%,1) 5%, hsla(0,0%,85%,1) 30%, hsla(0,0%,60%,1) 100%);
}

.slider-volume > .track {
.slider-volume > .track, .slider-volume > .highlight-track {
height: 11px;

background: #787878;
Expand All @@ -38,4 +38,11 @@
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
}
}

.slider-volume > .highlight-track {
background-color: #c5c5c5;
background: -moz-linear-gradient(top, #c5c5c5, #a2a2a2);
background: -webkit-linear-gradient(top, #c5c5c5, #a2a2a2);
background: linear-gradient(top, #c5c5c5, #a2a2a2);
}
15 changes: 13 additions & 2 deletions css/simple-slider.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
background: -webkit-linear-gradient(top, #8DCA09, #8DCA09);
}

.slider > .track {

.slider > .track, .slider > .highlight-track {
background: #ccc;
background: -webkit-linear-gradient(top, #bbb, #ddd);
background: -moz-linear-gradient(top, #bbb, #ddd);
Expand All @@ -41,4 +42,14 @@

border: 1px solid #aaa;
height: 4px;
}
}

.slider > .highlight-track {
background-color: #8DCA09;
background: -webkit-linear-gradient(top, #8DCA09, #72A307);
background: -moz-linear-gradient(top, #8DCA09, #72A307);
background: linear-gradient(top, #8DCA09, #72A307);

border-color: #496805;
}

66 changes: 44 additions & 22 deletions js/simple-slider.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
classPrefix: null
classSuffix: null
theme: null
highlight: false

@settings = $.extend({}, @defaultOptions, options)
@settings.classSuffix = "-#{@settings.theme}" if @settings.theme
Expand All @@ -39,27 +40,19 @@
boxSizing: "border-box"
.insertBefore @input
@slider.attr("id", @input.attr("id") + "-slider") if @input.attr("id")

# Create the track
@track = $("<div>")
.addClass("track")

@track = @createDivElement("track")
.css
position: "absolute"
top: "50%"
width: "100%"
userSelect: "none"
cursor: "pointer"
.appendTo @slider


if @settings.highlight
# Create the highlighting track on top of the track
@highlightTrack = @createDivElement("highlight-track")
.css
width: "0"

# Create the slider drag target
@dragger = $("<div>")
.addClass("dragger")
.css
position: "absolute"
top: "50%"
userSelect: "none"
cursor: "pointer"
.appendTo @slider
@dragger = @createDivElement("dragger")

# Adjust dimensions now elements are in the DOM
@slider.css
Expand All @@ -69,6 +62,10 @@

@track.css
marginTop: @track.outerHeight()/-2

if @settings.highlight
@highlightTrack.css
marginTop: @track.outerHeight()/-2

@dragger.css
marginTop: @dragger.outerWidth()/-2
Expand All @@ -77,11 +74,12 @@
# Hook up drag/drop mouse events
@track
.mousedown (e) =>
return unless e.which == 1
@trackEvent(e)

@domDrag(e.pageX, e.pageY, true)
@dragging = true
false
if @settings.highlight
@highlightTrack
.mousedown (e) =>
@trackEvent(e)

@dragger
.mousedown (e) =>
Expand Down Expand Up @@ -135,6 +133,19 @@
position: ratio * @slider.outerWidth()
el: @slider

# Create the basis of the track-div(s)
createDivElement: (classname) ->
item = $("<div>")
.addClass(classname)
.css
position: "absolute"
top: "50%"
userSelect: "none"
cursor: "pointer"
.appendTo @slider
return item


# Set the ratio (value between 0 and 1) of the slider.
# Exposed via el.slider("setRatio", ratio)
setRatio: (ratio) ->
Expand Down Expand Up @@ -166,6 +177,14 @@
# Trigger value changed events
@valueChanged(value, ratio, "setValue")

# Respond to an event on a track
trackEvent: (e) ->
return unless e.which == 1

@domDrag(e.pageX, e.pageY, true)
@dragging = true
false

# Respond to a dom drag event
domDrag: (pageX, pageY, animate=false) ->
# Normalize position within allowed range
Expand Down Expand Up @@ -194,8 +213,10 @@
setSliderPosition: (position, animate=false) ->
if animate and @settings.animate
@dragger.animate left: position, 200
@highlightTrack.animate width: position, 200 if @settings.highlight
else
@dragger.css left: position
@highlightTrack.css width: position if @settings.highlight

# Set the slider position given a value
setSliderPositionFromValue: (value, animate=false) ->
Expand Down Expand Up @@ -331,6 +352,7 @@
settings.snap = $el.data("slider-snap")
settings.equalSteps = $el.data("slider-equal-steps")
settings.theme = $el.data("slider-theme") if $el.data("slider-theme")
settings.highlight = $el.data("slider-highlight") if $el.data("slider-highlight")

# Activate the plugin
$el.simpleSlider settings
Expand Down
79 changes: 57 additions & 22 deletions js/simple-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ var __slice = [].slice,
snapMid: false,
classPrefix: null,
classSuffix: null,
theme: null
theme: null,
highlight: false
};
this.settings = $.extend({}, this.defaultOptions, options);
if (this.settings.theme) {
Expand All @@ -37,19 +38,15 @@ var __slice = [].slice,
if (this.input.attr("id")) {
this.slider.attr("id", this.input.attr("id") + "-slider");
}
this.track = $("<div>").addClass("track").css({
position: "absolute",
top: "50%",
width: "100%",
userSelect: "none",
cursor: "pointer"
}).appendTo(this.slider);
this.dragger = $("<div>").addClass("dragger").css({
position: "absolute",
top: "50%",
userSelect: "none",
cursor: "pointer"
}).appendTo(this.slider);
this.track = this.createDivElement("track").css({
width: "100%"
});
if (this.settings.highlight) {
this.highlightTrack = this.createDivElement("highlight-track").css({
width: "0"
});
}
this.dragger = this.createDivElement("dragger");
this.slider.css({
minHeight: this.dragger.outerHeight(),
marginLeft: this.dragger.outerWidth() / 2,
Expand All @@ -58,18 +55,23 @@ var __slice = [].slice,
this.track.css({
marginTop: this.track.outerHeight() / -2
});
if (this.settings.highlight) {
this.highlightTrack.css({
marginTop: this.track.outerHeight() / -2
});
}
this.dragger.css({
marginTop: this.dragger.outerWidth() / -2,
marginLeft: this.dragger.outerWidth() / -2
});
this.track.mousedown(function(e) {
if (e.which !== 1) {
return;
}
_this.domDrag(e.pageX, e.pageY, true);
_this.dragging = true;
return false;
return _this.trackEvent(e);
});
if (this.settings.highlight) {
this.highlightTrack.mousedown(function(e) {
return _this.trackEvent(e);
});
}
this.dragger.mousedown(function(e) {
if (e.which !== 1) {
return;
Expand Down Expand Up @@ -112,6 +114,17 @@ var __slice = [].slice,
});
}

SimpleSlider.prototype.createDivElement = function(classname) {
var item;
item = $("<div>").addClass(classname).css({
position: "absolute",
top: "50%",
userSelect: "none",
cursor: "pointer"
}).appendTo(this.slider);
return item;
};

SimpleSlider.prototype.setRatio = function(ratio) {
var value;
ratio = Math.min(1, ratio);
Expand All @@ -129,6 +142,15 @@ var __slice = [].slice,
return this.valueChanged(value, ratio, "setValue");
};

SimpleSlider.prototype.trackEvent = function(e) {
if (e.which !== 1) {
return;
}
this.domDrag(e.pageX, e.pageY, true);
this.dragging = true;
return false;
};

SimpleSlider.prototype.domDrag = function(pageX, pageY, animate) {
var pagePos, ratio, value;
if (animate == null) {
Expand All @@ -155,13 +177,23 @@ var __slice = [].slice,
animate = false;
}
if (animate && this.settings.animate) {
return this.dragger.animate({
this.dragger.animate({
left: position
}, 200);
if (this.settings.highlight) {
return this.highlightTrack.animate({
width: position
}, 200);
}
} else {
return this.dragger.css({
this.dragger.css({
left: position
});
if (this.settings.highlight) {
return this.highlightTrack.css({
width: position
});
}
}
};

Expand Down Expand Up @@ -319,6 +351,9 @@ var __slice = [].slice,
if ($el.data("slider-theme")) {
settings.theme = $el.data("slider-theme");
}
if ($el.data("slider-highlight")) {
settings.highlight = $el.data("slider-highlight");
}
return $el.simpleSlider(settings);
});
});
Expand Down
Loading

0 comments on commit 8bb0483

Please sign in to comment.