diff --git a/README.md b/README.md index 5ffc69f..313bec1 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,170 @@ # Zepto Range -http://jcemer.com/zepto-range +Input range slider to work with touch using Zepto -Input range implementation to works with touch using Zepto +http://stormtek.github.io/zepto-range/ + +---------------------------------------------------------- +### File imports to add to header of html + +``` html + + + +``` + +**Tip:** For quicker load times use ```zepto.min.js``` rather than ```zepto.js``` as it is the minified version of the javascript for the main zepto library. Both files are included in this repository for your convenience. If you are building ```zepto.js``` and ```zepto.min.js``` from the source make sure to include the touch module if you are planning on supporting mobile / tablets. + +**Note:** If performance (loading time) is a big issue then feel free to place the ``` + -
-
-

Zepto Range

-

Input range implementation to works with touch using Zepto.

-

github.com/jcemer/zepto-range

- -
- - -
+
+

Zepto Range

+

Input range implementation to works with touch using Zepto.

+

github.com/stormtek/zepto-range

+

+ + +
+

Normal mode

+ +

Inside mode

+ +
-
- -
+

Some Labels

+
+ + + +
-
- -
+

First and Last Label

+
+ +
-
-

Events

- -
- - - - +
+

User Defined Fill Color

+ +
+ + +

Free standing

+ + +
+

Current Value: Unknown

+

Output: Unknown

- Fork me on GitHub + + + +
+

Outside Wrapper div

+
+ +
+ + + Fork me on GitHub + + + - \ No newline at end of file + diff --git a/styles.css b/styles.css index dafc824..9e1678a 100644 --- a/styles.css +++ b/styles.css @@ -1,32 +1,10 @@ html, body { - height: 100%; margin: 0; padding: 0; -} - -#main { - position: relative; - background-color: #141414; - background-image: -webkit-linear-gradient(top, #CCC, #EEE 100%); - background-image: -moz-linear-gradient(top, #CCC, #EEE 100%); - background-image: -ms-linear-gradient(top, #CCC, #EEE 100%); - background-image: -o-linear-gradient(top, #CCC, #EEE 100%); - background-image: linear-gradient(top, #CCC, #EEE 100%); - height: 100%; - min-height: 800px; + background-color: #ccc; color: #333; font: 300 15px/1 Verdana; } -#main:before { - background-image: -webkit-radial-gradient(center, circle, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0) 100%); - background-image: -moz-radial-gradient(center, circle, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0) 100%); - background-image: -ms-radial-gradient(center, circle, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0) 100%); - background-image: -o-radial-gradient(center, circle, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0) 100%); - background-image: radial-gradient(center, circle, rgba(255, 255, 255, 0.3), rgba(255, 255, 255, 0) 100%); - position: absolute; - content: ''; - top: 0; right: 0; bottom: 0; left: 0; -} /* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */ @@ -35,13 +13,10 @@ body { } .wrapper { + width: 600px; position: relative; - width: 500px; - height: 900px; - position: absolute; - top: 50%; left: 50%; - margin-left: -250px; - margin-top: -450px; + left: 50%; + margin-left: -300px; /* half the width */ } h1, h2 { @@ -54,161 +29,4 @@ h2 { font-size: 30px; letter-spacing: -2px; text-shadow: 1px 2px 3px #DDD, 0 0 0 #222, 1px 2px 3px #DDD; -} - -/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */ - -input[type="range"] { - width: 100%; -} - -.range { - position: relative; -} -.range input { - position: absolute; - text-align: left; - direction: ltr; - left: -9999px; -} - -/* legend */ -.range .legend { - font-size: 16px; - position: absolute; - top: -25px; - left: 0; - right: 0; - z-index: 1; -} -.range .legend .label { - padding-top: 55px; - float: left; - text-align: center; - height: 30px; - line-height: 30px; -} -.range .legend .label:first-child { - text-align: left; -} -.range .legend .label:last-child { - text-align: right; -} - -.range .legend .label:empty { - position: relative; -} -.range .legend .label:empty:before { - content: ''; - position: absolute; - width: 1px; - top: 62px; left: 50%; bottom: 7px; - background: #CCC; - border-left: 1px solid white; -} -.range .legend .label:empty:first-child:before { - left: 20px; /* btn half size */ -} -.range .legend .label:empty:last-child:before { - left: auto; - right: 20px; /* btn half size */ -} - -.range .legend .label.active { - -webkit-transition: 0.2s all ease-out; - -moz-transition: 0.2s all ease-out; - -o-transition: 0.2s all ease-out; - transition: 0.2s all ease-out; - - color: #8D8DAC; - text-shadow: 1px 1px 1px #CCC; -} - -/* bar, fill, btn */ -.range .bar, -.range .fill { - -webkit-border-radius: 4px; - -moz-border-radius: 4px; - border-radius: 4px; - height: 10px; -} -.range .bar { - position: relative; - margin-top: 50px; - margin-bottom: 100px; - margin-left: 20px; /* btn half size */ - margin-right: 20px; /* btn half size */ - background: #999; - border-bottom: 1px solid rgba(255, 255, 255, .9); - -webkit-box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.3), 0 1px 5px rgba(0, 0, 0, 0.05); - -moz-box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.3), 0 1px 5px rgba(0, 0, 0, 0.05); - -o-box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.3), 0 1px 5px rgba(0, 0, 0, 0.05); - box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.3), 0 1px 5px rgba(0, 0, 0, 0.05); -} -.range .fill { - background: rgba(100, 100, 255, .2); - position: absolute; - top: 0; left: 0; -} -.range .btn { - margin-top: -16px; - position: absolute; - top: 0; left: -20px; /* btn half size */ - width: 38px; - height: 38px; - z-index: 2; - - -webkit-border-radius: 50%; - -moz-border-radius: 50%; - border-radius: 50%; - - border: 2px solid rgba(255, 255, 255, .8); - - background-image: -webkit-radial-gradient(10px 10px, circle cover, #FFF, #DDD); - background-image: -moz-radial-gradient(10px 10px, circle cover, #FFF, #DDD); - background-image: -o-radial-gradient(10px 10px, circle cover, #FFF, #DDD); - background-image: radial-gradient(10px 10px, circle cover, #FFF, #DDD); - - -webkit-box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.1), 0 2px 5px rgba(0, 0, 0, 0.3); - -moz-box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.1), 0 2px 5px rgba(0, 0, 0, 0.3); - -o-box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.1), 0 2px 5px rgba(0, 0, 0, 0.3); - box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.1), 0 2px 5px rgba(0, 0, 0, 0.3); -} -.range .btn:active { - border-color: rgba(255, 255, 255, .5); - top: 1px; - -webkit-box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.1), 0px 1px 4px rgba(0, 0, 0, 0.3); - -moz-box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.1), 0px 1px 4px rgba(0, 0, 0, 0.3); - -o-box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.1), 0px 1px 4px rgba(0, 0, 0, 0.3); - box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.1), 0px 1px 4px rgba(0, 0, 0, 0.3); -} - -/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */ - -.range.inside .bar{ - margin-left: 0; - margin-right: 0; -} -.range.inside .btn { - left: 0; -} -.range.inside .fill { - padding-right: 20px; /* btn half size */ -} - -/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */ - -.range .fill, -.range .btn { - -webkit-transition: 0.2s all ease-out; - -moz-transition: 0.2s all ease-out; - -o-transition: 0.2s all ease-out; - transition: 0.2s all ease-out; -} -.range.moving .fill, -.range.moving .btn { - -webkit-transition: none; - -moz-transition: none; - -o-transition: none; - transition: none; -} +} \ No newline at end of file diff --git a/zepto-range.css b/zepto-range.css new file mode 100644 index 0000000..ad5d5f4 --- /dev/null +++ b/zepto-range.css @@ -0,0 +1,181 @@ +/* makes sure width works if not specified on input */ +input[type="range"] { + width: 95%; +} + +.range { + position: relative; + padding: 0px 20px; +} +/* hide the input value offscreen */ +.range input { + display: none; +} + +/* legend */ +.range .legend { + font-size: 16px; + position: absolute; + top: -25px; + right: 0; + z-index: 1; + /* position tweak to allow us to click just off each end of the slider */ + left: 20px; + margin-left: -20px; + padding-left: 20px; +} + +.range .legend .label { + padding-top: 55px; + float: left; + text-align: center; + height: 30px; + line-height: 30px; + +} +.range .legend .label:first-child { + text-align: left; +} +.range .legend .label:last-child { + text-align: right; +} + +.range .legend .label-last-child { + margin-left: -30px; +} + +.range .legend .singleCharLabel:first-child { + position: relative; + left: 15px; /* 3/8 btn size */ +} +.range .legend .singleCharLabel:last-child { + position: relative; + left: 15px; /* 3/8 btn size */ +} + +.range .legend .doubleCharLabel:first-child { + position: relative; + left: 10px; /* 1/4 btn size */ +} +.range .legend .doubleCharLabel:last-child { + position: relative; + left: 20px; /* 1/2 btn size */ +} + +.range .legend .label-empty { + position: relative; +} +.range .legend .label-empty:before { + content: ''; + position: absolute; + width: 1px; + top: 62px; left: 50%; bottom: 7px; + background: #CCC; + border-left: 1px solid white; +} +.range .legend .label-empty:first-child:before { + left: 20px; /* btn half size */ +} +.range .legend .label-empty:last-child:before { + left: auto; + right: 20px; /* btn half size */ +} + +.range .legend .label.active { + -webkit-transition: 0.2s all ease-out; + -moz-transition: 0.2s all ease-out; + -o-transition: 0.2s all ease-out; + transition: 0.2s all ease-out; + + color: #8D8DAC; + text-shadow: 1px 1px 1px #CCC; +} + +/* bar, fill, btn */ +.range .bar, +.range .fill { + -webkit-border-radius: 4px; + -moz-border-radius: 4px; + border-radius: 4px; + height: 10px; +} +.range .bar { + position: relative; + margin-top: 50px; + margin-bottom: 100px; + margin-left: 20px; /* btn half size */ + margin-right: 20px; /* btn half size */ + background: #999; + border-bottom: 1px solid rgba(255, 255, 255, .9); + -webkit-box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.3), 0 1px 5px rgba(0, 0, 0, 0.05); + -moz-box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.3), 0 1px 5px rgba(0, 0, 0, 0.05); + -o-box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.3), 0 1px 5px rgba(0, 0, 0, 0.05); + box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.3), 0 1px 5px rgba(0, 0, 0, 0.05); +} +.range .fill { + background: #60B9B4; + position: absolute; + top: 0; left: 0; +} +.range .btn { + margin-top: -16px; + position: absolute; + top: 0; left: -20px; /* btn half size */ + width: 38px; + height: 38px; + z-index: 2; + + -webkit-border-radius: 50%; + -moz-border-radius: 50%; + border-radius: 50%; + + border: 2px solid rgba(255, 255, 255, .8); + + background-image: -webkit-radial-gradient(10px 10px, circle cover, #FFF, #DDD); + background-image: -moz-radial-gradient(10px 10px, circle cover, #FFF, #DDD); + background-image: -o-radial-gradient(10px 10px, circle cover, #FFF, #DDD); + background-image: radial-gradient(10px 10px, circle cover, #FFF, #DDD); + + -webkit-box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.1), 0 2px 5px rgba(0, 0, 0, 0.3); + -moz-box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.1), 0 2px 5px rgba(0, 0, 0, 0.3); + -o-box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.1), 0 2px 5px rgba(0, 0, 0, 0.3); + box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.1), 0 2px 5px rgba(0, 0, 0, 0.3); +} +.range .btn:active { + border-color: rgba(255, 255, 255, .5); + top: 1px; + -webkit-box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.1), 0px 1px 4px rgba(0, 0, 0, 0.3); + -moz-box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.1), 0px 1px 4px rgba(0, 0, 0, 0.3); + -o-box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.1), 0px 1px 4px rgba(0, 0, 0, 0.3); + box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.1), 0px 1px 4px rgba(0, 0, 0, 0.3); +} + +/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */ + +.range.inside .bar{ + margin-left: 0; + margin-right: 0; +} +.range.inside .btn { + left: 0; +} +.range.inside .fill { + padding-right: 20px; /* btn half size */ +} + +/* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. */ + +.range .fill, +.range .btn { + -webkit-transition: 0.2s all ease-out; + -moz-transition: 0.2s all ease-out; + -o-transition: 0.2s all ease-out; + transition: 0.2s all ease-out; +} +.range.moving .fill, +.range.moving .btn { + -webkit-transition: none; + -moz-transition: none; + -o-transition: none; + transition: none; +} diff --git a/zepto-range.js b/zepto-range.js index cfaa1bb..2773272 100644 --- a/zepto-range.js +++ b/zepto-range.js @@ -64,26 +64,54 @@ function Range(input, labels) { this.input = $(input); - this.container = this.input.wrap('
').parent(); - this.container.addClass(defaults.name + ' ' + this.input[0].className); + this.labels = labels; // number this.min = parseInt(this.input.attr('min'), 10); this.max = parseInt(this.input.attr('max'), 10); this.amount = (this.max - this.min) + 1; - this.current = parseInt(this.input.val(), 10) - this.min; + var initValue = parseInt(this.input.val(), 10); + if(isNaN(initValue)) this.current = this.min; + else this.current = initValue - this.min; + + // default behaviour is to show lines for empty labels + var showEmptyLabels = this.input.attr('showEmptyLabels'); + if(showEmptyLabels == 'false') this.showEmptyLabels = false; + else this.showEmptyLabels = true; + + // find a user defined color for the fill + var fillColor = this.input.attr('fillColor'); + var showFill = this.input.attr('showFill'); + + //check for rtl layout + var rtl = this.input.attr('rtl'); + if(rtl == "True" || rtl == "true") this.rtl = true; + else this.rtl = false; // html this.btn = $('
'); - this.fill = $('
'); + if(showFill == "False" || showFill == "false") this.fill = $('
'); + else if(fillColor != undefined) this.fill = $('
'); + else this.fill = $('
'); this.bar = $('
').append(this.btn, this.fill); + this.btn.size = this.btn.width(); + + var setWidthValue = parseInt(this.input.attr('width'), 10); + if(isNaN(setWidthValue) || setWidthValue <= 0) setWidthValue = this.input.width(); + var width = setWidthValue - this.btn.size; + this.container = this.input.wrap('
').parent(); + this.container.addClass(defaults.name + ' ' + this.input[0].className); this.container.append(this.bar); - this.btn.size = this.btn.width(); - this.size = this.input.width() - this.btn.size; + this.size = width; this.gap = this.size / (this.amount - 1); // legend + if(!labels.length) { + labels = new Array(2); + labels[0] = this.min + ""; + labels[1] = this.max + ""; + } this.legend = legend(labels, this); this.container.append(this.legend); @@ -95,7 +123,7 @@ function legend(labels, range) { var diff = range.amount - labels.length, gaps = labels.length - 1, - size = Math.floor(range.size / (range.amount - 1)), + size = (range.size - labels.length) / (range.amount - 1), container, tmp, i; // labels @@ -111,18 +139,91 @@ } } } + + // reverse labels if using an rtl layout + if(range.rtl) { + var numberOfLabels = labels.length; + var reverse = new Array(numberOfLabels); + for(var i=0; i