Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaks and improvements #2

Open
wants to merge 66 commits into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
8d7928c
Simplified and extended example html
Jun 13, 2013
f6c8ced
Update README.md
stormtek Jun 13, 2013
564a301
Update README.md
stormtek Jun 13, 2013
30e59e8
Split css for the slider itself into a separate file
Jun 18, 2013
abc760c
Update README.md
stormtek Jun 18, 2013
ee56665
Fixed bug in slider width that showed up in Chrome
stormtek Jun 19, 2013
f4292ac
Update index.html
stormtek Jun 19, 2013
521c726
Update README.md
stormtek Jun 20, 2013
61ba550
Update README.md
stormtek Jun 21, 2013
fac7db9
Now displays min and max values if no options are passed to range()
stormtek Jun 21, 2013
7b8a93e
Update README.md
stormtek Jun 21, 2013
9de3bf1
Update README.md
stormtek Jun 21, 2013
b8a30f7
Updated to allow parameter to be passed to input to allow hiding of l…
Jun 24, 2013
179198f
Merge branch 'gh-pages' of https://github.com/stormtek/zepto-range in…
Jun 24, 2013
410934e
Update README.md
stormtek Jun 24, 2013
3a337d4
Improved styling, still doesnt handle extreme cases perfectly though
Jun 24, 2013
eb285eb
Update README.md
stormtek Jun 24, 2013
97a6dbb
Added method to allow us to change the value of the slider dynamicall…
Jun 24, 2013
c948182
Update README.md
stormtek Jun 24, 2013
7bfa44d
Update README.md
stormtek Jun 24, 2013
36ade92
Restyled page to be centred again
Jun 24, 2013
da5a7f5
Fixed styling for mobile (which I had broken)
Jun 24, 2013
35074ec
If value is not specified then we now set default value to min
Jun 25, 2013
ab39939
Update README.md
stormtek Jun 25, 2013
65f2db3
Fixed jitter bug where it was possible to get the handle on the slide…
Jun 25, 2013
e17c2b3
Fixed glitch where changing value would have handle lag a long way be…
Jun 25, 2013
da71888
Fixed bug with width being set to NaN if not specified which broke sl…
Jun 26, 2013
28ecc1e
Tweak to make the fill work properly with the handle
stormtek Jun 26, 2013
78b2641
Updated styling for handling fill better with handle
stormtek Jun 26, 2013
415f64f
Wrote algorithm to handle adjusting label widths for sub pixel values…
Jun 27, 2013
cbe2a00
Added minified js for zepto library
Jun 27, 2013
9aea5af
Update README.md
stormtek Jun 27, 2013
bd0dc43
Update index.html
stormtek Jun 27, 2013
d5d9b58
Update README.md
stormtek Jun 27, 2013
07cc2b0
Updated zepto.min.js to include the touch module
Jun 27, 2013
6bf43c5
Update README.md
stormtek Jun 27, 2013
de8189d
Switched back to zepto.min.js now that I have included the touch module
stormtek Jun 27, 2013
f14a94e
Fixed small layout issue in creation of labels where widths were slig…
Jun 27, 2013
ce311ad
Merge branch 'gh-pages' of https://github.com/stormtek/zepto-range in…
Jun 27, 2013
52960ac
Removed wait for transition stuff, since overall it was not looking q…
Jun 27, 2013
179faa5
Added the ability to easily specfiy a custom fill colour for the slid…
Jun 27, 2013
bd8d4e9
Adjusted layout of slider slightly to include a 10px buffer at either…
Jun 28, 2013
0c5b33d
Fixed bug in setValue() where we were setting the value to the index,…
Jun 28, 2013
c01b022
Update README.md
stormtek Jun 28, 2013
c75988c
remove unneccessary css
Jun 30, 2013
88da9df
Merge branch 'gh-pages' of https://github.com/stormtek/zepto-range in…
Jun 30, 2013
45f28ea
Fix handling of click just off ends of slider to work with touch
Jun 30, 2013
8093d89
Styling tweak to improve performance of clicking off end of slider wh…
Jun 30, 2013
91cb4d4
Small error check addition
Jul 1, 2013
24803bc
Added a function to set the width of a slider to a specified value on…
Jul 1, 2013
0328279
Update README.md
stormtek Jul 1, 2013
8890f71
Tweaked algorithm for setting label widths, it should be a bit more r…
Jul 1, 2013
73d90fb
Merge branch 'gh-pages' of https://github.com/stormtek/zepto-range in…
Jul 1, 2013
e3c538e
Update README.md
stormtek Jul 1, 2013
0a400b7
Added function to get value from range. Fixed bug where clicking off …
Jul 2, 2013
1bb49f8
Merge branch 'gh-pages' of https://github.com/stormtek/zepto-range in…
Jul 2, 2013
e846495
Update README.md
stormtek Jul 2, 2013
889f744
Added output of current value for one of the sliders based on event h…
Jul 2, 2013
8565ab9
Update README.md
stormtek Jul 2, 2013
4b13796
Tweaked positioning of some buttons
Jul 2, 2013
bed5cfc
Fixed bug in set width
Jul 3, 2013
00c6e69
Fixed algorithm for setting widths of labels
Jul 5, 2013
726eff9
Fixed small bug in removeRange() where i was being used rather than i…
Jul 7, 2013
dfba823
Set input to hidden rather than offscreen
stormtek Apr 4, 2014
083ab9f
Added support for two new parameters
stormtek Aug 3, 2015
d8faa0c
Update README.md
stormtek Aug 3, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 156 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
<link rel="stylesheet" href="zepto-range.css"/>
<script src="zepto.js"></script>
<script src="zepto-range.js"></script>
```

**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 ```<script>``` tags for the javascript at the end of the body, rather than in the header. Just make sure that they are above the javascript that sets up the sliders.

----------------------------------------------------------
### Input fields that the slider is built around

``` html
<input type="range" min="0" max="4" value="0">
<input type="range" min="0" max="4">
<input type="range" class="inside" min="-5" max="5" value="0">
<input type="range" min="0" max="2" width="300">
```

#### Required Parameters

- type="range"
- Identifies this as a range slider
- min="some_integer"
- The minimum value this slider has
- max="some_integer_greater_than_min"
- The maximum value this slider has

**Note:** The number of integers between min and max determine the number of options that the slider has.

#### Optional Parameters

- value="some_integer_between_min_and_max"
- The initial value that is set for the slider
- This will default to min if it is not set
- **Note:** This is not an index point but one of the values that lies between max and min
- width="some_integer"
- The width that you want the slider to be
- class="inside" or class="normal"
- This determines whether the outside option lines sit inside the slider or even with the visible ends of the slider
- If nothing is specified then this defaults to class="normal"
- showEmptyLabels="true" or showEmptyLabels="false"
- By setting this to false you can choose to show nothing for those empty labels, hiding them from the user
- If nothing is specified then this defaults to showEmptyLabels="true"
- fillColor="desired_colour"
- The color that you want the fill bar to be
- If nothing is specified then this defaults to the predefined color
- This takes a css color property (either a hex value or an acceptable color name)
- showFill="true" or showFill="false"
- By setting this to false you can choose to hide the coloured fill element of the slider
- If nothing is specified then this defaults to showing the coloured fill element
- rtl="true" or rtl="false"
- By setting this to true you can choose to use an RTL layout for the slider. This reverses the order of the labels that have been specified (or auto-generated) and fills from right to left rather than left to right. This is useful when using the slider with an RTL language such as Arabic or Hebrew.

**Note:** If a width is not specified then the width of an ancestor object will be grabbed. If no ancestor has it's width specified then this will end up being the width of the page. To guarantee that width and layout work correctly it is recommended to specify a width for the input field. One thing to note is that if the width you specify for the slider is greater than the width of a wrapping container then the slider is likely to have issues displaying correctly - the options will probably wrap in strange ways.

**Tip:** If you use a large number of values (e.g. min="0", max="200") and hide the labels (using showEmptyLabels="false") then it is possible to simulate a smooth continuous slider.

**Note:** The combination of width of the slider and number of options that the slider has is used to calculate the width that each label should be. The slider does it's best to make sure that the labels are evenly spaced between min and max, but this gets difficult with high numbers of values - particularly with a smaller width. This is because we are needing to handle the case where the width of an individual label is less than 1 pixel. If the labels are not quite lining up, try fiddling with the width of the slider and the min / max values to alleviate the problem. The layout should work well in most scenarios, but there are unfortunately no guarantees for extreme cases (100's of options with a width of only a few hundred pixels is likely to look a little weird).

----------------------------------------------------------
### Gaining access to the input field

``` javascript
$('#wrapper_id input[type="range"]')
$('#input_field_id')
$('input[type="range"]')
```

- The parameter to $() is a DOM selector for the input field
- The first example will return all input fields inside the field with #wrapper_id
- The second example will return the field with the specified id (it is up to you to make sure that this is actually an input field, not something like a div)
- The final example will return all input fields on the page (useful if you want to construct all sliders at once with default labels)

**Tip:** The safest option is to wrap each input field in a div which has an id set on it. That way you can use the first example selector and know that you are interacting with a slider.

----------------------------------------------------------
### Javascript call to construct slider

This needs to be added inside script tags at the end of the html. This guarantees that all the html is added to the DOM correctly before we start modifying it.

``` javascript
$('input[type="range"]').range();
$('input[type="range"]').range('worst', 'best');
$('dom_selector').range();
$('dom_selector').range('worst', 'best');
$('dom_selector').range(['1', '2', '3']);
```

## Modes
- The function range() can take 3 types of parameter (as shown above):
1. No parameters (the min and max values set on the input field will be shown)
2. A list of strings to be shown as options
3. An array of strings to be shown as options

* `normal`
* `inside`
- If there is less than a one-to-one mapping of parameters to options then gaps will be added intelligently
- 1 parameter will cause things to break
- 2 parameters will map to the first and last option
- 3 parameters will map to the first, middle, and last option (provided there are an odd number of options ...)
- If equal sized gaps cannot be made then it will default back to simply showing lines
- Things work best for gaps if there are an odd number of options

## Events
----------------------------------------------------------
### Javascript calls to interact with value

``` javascript
$('dom_selector').setValue(3);
```

- Make sure that the value passed in is an integer that is between min and max
- min <= x <= max

``` javascript
$('#wrapper_id input[type="range"]').getValue();
```

- This returns the current value that the slider has set

----------------------------------------------------------
### Javascript calls to modify fill colour

``` javascript
$('dom_selector').setFillColor('#332299');
$('dom_selector').setFillColor('blue');
```

- This needs to be passed a valid css color
- It can be either a hex value or a predefined color

``` javascript
$('dom_selector').resetFillColor();
```

- This will reset the fill colour to the default value specified in the css

----------------------------------------------------------
### Javascript call to adjust width of slider

``` javascript
$('dom_selector').setWidth(300);
```

- This function expects an integer value for the width you want

----------------------------------------------------------
### Events

- `init`
- `move`
- `change`

You can set up a listener for any of these events using the javascript below

``` javascript
$('dom_selector').on('change', function(event, current, range) {
//handle event here
});
```

* `init`
* `move`
* `change`
- 'event' is the name of the event that trigged the handler
- 'current' is the current **index** the slider has set, rather than the current **value**
- 'range' is the slider itself
- To get the current value the slider has set use range.input[0].value
- For some reason range.getValue() does not work inside the event handler
152 changes: 111 additions & 41 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,122 @@
<meta charset="UTF-8">
<title>Zepto Range</title>
<meta name="viewport" content="width=600">
<link rel="stylesheet" href="styles.css">
<link href="//fonts.googleapis.com/css?family=Ubuntu:300,700" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="styles.css"/>
<link rel="stylesheet" href="zepto-range.css"/>
<link href="//fonts.googleapis.com/css?family=Ubuntu:300,700" rel="stylesheet" type="text/css"/>
<script src="zepto.min.js"></script>
<script src="zepto-range.js"></script>
</head>
<body>
<div id="main">
<div class="wrapper">
<h1>Zepto Range</h1>
<p>Input range implementation to works with touch using Zepto.</p>
<p><a href="//github.com/jcemer/zepto-range">github.com/jcemer/zepto-range</a></p>

<div id="samples-01">
<input type="range" min="0" max="4" value="0">
<input type="range" class="inside" min="0" max="4" value="0">
</div>
<div class="wrapper">
<h1>Zepto Range</h1>
<p>Input range implementation to works with touch using Zepto.</p>
<p><a href="https://github.com/stormtek/zepto-range">github.com/stormtek/zepto-range</a></p>
<br/><br/>
<!-- two sliders nested inside the same div -->
<!-- one uses 'inside' mode to contrast the two -->
<div id="samples">
<h2>Normal mode</h2>
<input type="range" min="0" max="600" value="0" showemptylabels="false">
<h2>Inside mode</h2>
<input type="range" class="inside" min="0" max="4">
</div>

<div id="samples-02">
<input type="range" class="inside" min="0" max="6" value="0">
</div>
<h2>Some Labels</h2>
<div id="some-labels">
<button onclick="changeWidth('some-labels', 300)">Set width to 300px</button>
<button onclick="changeWidth('some-labels', 700)">Set width to 700px</button>
<input type="range" class="inside" min="-4" max="4" value="-2" showEmptyLabels="false">
</div>

<div id="samples-03">
<input type="range" min="-4" max="4" value="0">
</div>
<h2>First and Last Label</h2>
<div id="first-and-last">
<input type="range" min="0" max="14" value="0">
</div>

<div id="samples-04">
<h2>Events</h2>
<input type="range" class="inside" min="0" max="2" value="1">
</div>

<script src="zepto.js"></script>
<script src="zepto-range.js"></script>
<script>
$('#samples-01 input[type="range"]').range();
$('#samples-02 input[type="range"]').range('0', '3', '6');
$('#samples-03 input[type="range"]').range('worst', 'best');

// SAMPLE 04
var sample04 = $('#samples-04 input[type="range"]');
sample04.on('change init', function (event, current, range) {
var labels = range.legend.children();
labels.removeClass('active');
labels.eq(current).addClass('active');
});
sample04.range('worst', 'normal', 'best');
</script>
<div id="user-defined-color">
<h2>User Defined Fill Color</h2>
<input type="range" class="inside" min="0" max="2" value="1" fillColor="#339922">
</div>

<!-- Free standing input field -->
<h2>Free standing</h2>
<button onclick="changeFreestandingValue()">Set value to 2</button>
<button onclick="getFreestandingValue()">Set Output to Current Value</button>
<div>
<p>Current Value: <span id="freestandingValue">Unknown</span></p>
<p>Output: <span id="freestandingOutput">Unknown</span></p>
</div>
<a href="//github.com/jcemer/zepto-range"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub"></a>
<button onclick="changeFreestandingColor()">Set colour to blue</button>
<button onclick="resetFreestandingColor()">Reset colour</button>
<input id="freestanding" class="inside" type="range" min="-5" max="3" value="0" width="300">

</div>
<h2>Outside Wrapper div</h2>
<div id="outside">
<input class="inside" type="range" min="-2" max="1" value="0">
</div>

<a href="https://github.com/stormtek/zepto-range">
<img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png" alt="Fork me on GitHub">
</a>
<!--
This script call needs to happen after the input fields have been added to
the html. If not then the addition of the slider divs does not happen.
-->
<script>
// No values to be shown
$('#samples input[type="range"]').range();
// Show value value for first, middle, last
$('#some-labels input[type="range"]').range('10', '3', '26');
// Show first and last values, sent through as an array
$('#first-and-last input[type="range"]').range(['worst', 'best']);

// Custom styling on the active label
var userDefinedColor = $('#user-defined-color input[type="range"]');
userDefinedColor.on('change init', function (event, current, range) {
var labels = range.legend.children();
labels.removeClass('active');
labels.eq(current).addClass('active');
});
userDefinedColor.range('worst', 'normal', 'best');

// This entry is outside the wrapper div, it deduces the width it should have
$('#outside input[type="range"]').range();

// This entry is not wrapped by a div
// it also has a lot of interactions defined based on button clicks
var freestanding = $('#freestanding');
freestanding.on('init change', function(event, current, range) {
$('#freestandingValue')[0].innerHTML = range.input[0].value;
});
freestanding.range();

function setFreestandingOutput(value) {
$('#freestandingOutput')[0].innerHTML = value;
}

// Methods to interact with sliders
function changeFreestandingValue() {
freestanding.setValue(2);
}

function getFreestandingValue() {
var value = freestanding.getValue();
setFreestandingOutput(value);
}

function changeFreestandingColor() {
freestanding.setFillColor('blue');
}

function resetFreestandingColor() {
freestanding.resetFillColor();
}

function changeWidth(id, newWidth) {
$('#' + id + ' input[type="range"]').setWidth(newWidth);
}
</script>
</body>
</html>
</html>
Loading