Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getIEVersion() {
- var rv = -1;
- if(navigator.appName == 'Microsoft Internet Explorer') {
- var ua = navigator.userAgent;
- var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
- if (re.exec(ua) != null)
- rv = parseFloat( RegExp.$1 );
- }
- return rv;
- }
- function IESlidesFixer() {
- if(getIEVersion() >= 9) {
- var input = document.getElementsByTagName('input');
- for(var i=0;i<input.length;i++) {
- if(input[i].getAttribute('type') == 'range') {
- var range = input[i];
- var c = range.parentNode;
- var body = document.getElementsByTagName('body')[0];
- var hr = document.createElement('hr');
- var thumb = document.createElement('div');
- var rangeMax = range.getAttribute('max') || 100;
- var rangeMin = range.getAttribute('min') || 0;
- var rangeStep = range.getAttribute('step') || 0.001;
- var rangeValue = range.value || parseFloat(rangeMin) + parseFloat((rangeMax - rangeMin) / 2);
- var rangePaddingLeft = range.style.paddingLeft || 0;
- var rangeMarginLeft = range.style.marginLeft || 0;
- var rangePaddingTop = range.style.paddingTop || 0;
- var rangeMarginTop = range.style.marginTop || 0;
- if(rangeValue < rangeMin) rangeValue = min;
- else if(rangeValue > rangeMax) rangeValue = rangeMin + ~~((rangeMax - rangeMin) / rangeStep) * rangeStep;
- range.value = rangeValue;
- var bodyPaddingLeft = body.style.paddingLeft || 0;
- var bodyMarginLeft = body.style.marginLeft || 0;
- var bodyPaddingTop = body.style.paddingTop || 0;
- var bodyMarginTop = body.style.marginTop || 0;
- hr.style.width= range.style.width || "130px";
- hr.style.display="inline-block";
- hr.style.position="relative";
- hr.style.marginBottom="5px";
- hr.style.textAlign="right";
- var posX = range.offsetLeft + rangePaddingLeft + rangeMarginLeft + bodyPaddingLeft + bodyMarginLeft;
- var posY = range.offsetTop + rangePaddingTop + rangeMarginTop + bodyPaddingTop + bodyMarginTop;
- thumb.className="thumb";
- thumb.style.position="absolute";
- thumb.style.width="5px";
- thumb.style.height="20px";
- thumb.style.background="#D6D6D6";
- thumb.style.border="1px solid #707070";
- thumb.style.borderRadius="3px 3px 5px 5px / 3px 3px 25px 25px";
- thumb.style.boxShadow="0 10px 2px #EEEEEE inset, 1px 1px 2px #AAAAAA";
- thumb.style.left = (((rangeValue - rangeMin) / (rangeMax - rangeMin)) * parseFloat(hr.style.width)) + posX + parseFloat(thumb.style.width) / 2 + 'px';
- thumb.style.top = posY + "px";
- thumb.setAttribute('data-clicked', 'false');
- thumb.setAttribute('data-posx', posX);
- thumb.setAttribute('data-posy', posY);
- thumb.setAttribute('data-rangepx', parseFloat(hr.style.width));
- thumb.setAttribute('data-max', rangeMax);
- thumb.setAttribute('data-min', rangeMin);
- thumb.setAttribute('data-step', rangeStep);
- thumb.setAttribute('data-rangeid', 'range'+i);
- thumb.setAttribute('data-focused', 'false');
- thumb.onmouseover = function(e) {
- this.setAttribute('data-focused', 'true');
- if(this.getAttribute('data-clicked') == 'false') {
- this.style.background="#B6E2FB";
- this.style.border="1px solid #3C7FB1";
- this.style.boxShadow="0 10px 2px #DEF2FC inset, 1px 1px 2px #AAAAAA";
- }
- if(range.onmouseover()) range.onmouseover(e);
- };
- thumb.onmouseout = function(e) {
- if(this.getAttribute('data-clicked') == 'false') {
- this.style.background="#D6D6D6";
- this.style.border="1px solid #707070";
- this.style.boxShadow="0 10px 2px #EEEEEE inset, 1px 1px 2px #AAAAAA";
- }
- if(range.onmouseout()) range.onmouseout(e);
- };
- thumb.onmousedown = function(e) {
- this.style.background="#63B3DE";
- this.style.border="1px solid #2C628B";
- this.style.boxShadow="0 10px 2px #A6D9F4 inset, 1px 1px 2px #AAAAAA";
- this.setAttribute('data-clicked', e.clientX);
- if(range.onmousedown()) range.onmousedown(e);
- };
- thumb.onmouseup = function(e) {
- this.style.background="#B6E2FB";
- this.style.border="1px solid #3C7FB1";
- this.style.boxShadow="0 10px 2px #DEF2FC inset, 1px 1px 2px #AAAAAA";
- this.setAttribute('data-clicked', 'false');
- if(range.onmouseup()) range.onmouseup(e);
- };
- thumb.onclick = function(e) {
- if(range.onclick()) range.onclick(e);
- };
- thumb.onfocus = function(e) {
- this.setAttribute('data-focused', 'true');
- if(range.onfocus()) range.onfocus(e);
- };
- thumb.onblur = function(e) {
- this.setAttribute('data-focused', 'false');
- this.style.background="#D6D6D6";
- this.style.border="1px solid #707070";
- this.style.boxShadow="0 10px 2px #EEEEEE inset, 1px 1px 2px #AAAAAA";
- this.setAttribute('data-clicked', 'false');
- if(range.onblur()) range.onblur(e);
- };
- hr.onmousedown = function(e) {
- var thumb = this.nextSibling;
- var range = document.getElementById(thumb.getAttribute('data-rangeid'));
- thumb.style.background="#63B3DE";
- thumb.style.border="1px solid #2C628B";
- thumb.style.boxShadow="0 10px 2px #A6D9F4 inset, 1px 1px 2px #AAAAAA";
- thumb.setAttribute('data-clicked', e.clientX);
- var posX = parseFloat(thumb.getAttribute('data-posx'));
- var w = parseFloat(thumb.getAttribute('data-rangepx'));
- var max = parseFloat(thumb.getAttribute('data-max'));
- var min = parseFloat(thumb.getAttribute('data-min'));
- var step = parseFloat(thumb.getAttribute('data-step'));
- var rangeVal = max - min;
- var x = (e.clientX - posX) - parseFloat(thumb.style.width);
- var value = ((x / w) * max) + min;
- value = Math.round((value - min) / step) * step + min;
- if(value < min) {
- value = min;
- }
- else if(value > max) {
- value = min + ~~(rangeVal / step) * step;
- }
- thumb.style.left = (((value - min) / max) * w) + posX + parseFloat(thumb.style.width) / 2 + 'px';
- thumb.setAttribute('data-clicked', e.clientX);
- range.value = value;
- if(range.onchange()) range.onchange(e);
- if(range.onmousedown()) range.onmousedown(e);
- }
- range.setAttribute('type', 'hidden');
- range.setAttribute('id', ((range.getAttribute('id')) ? range.getAttribute('id') + ' ' : '') + 'range'+i);
- c.insertBefore(hr, range);
- c.insertBefore(thumb, range);
- }
- }
- document.onmousemove = function(e) {
- var thumbs = document.getElementsByClassName('thumb');
- for(var i=0;i<thumbs.length;i++) {
- if(thumbs[i].getAttribute('data-clicked') != 'false') {
- var thumb = thumbs[i];
- var pos_init = thumb.getAttribute('data-clicked');
- var posX = parseFloat(thumb.getAttribute('data-posx'));
- var posY = parseFloat(thumb.getAttribute('data-posy'));
- var w = parseFloat(thumb.getAttribute('data-rangepx'));
- var max = parseFloat(thumb.getAttribute('data-max'));
- var min = parseFloat(thumb.getAttribute('data-min'));
- var step = parseFloat(thumb.getAttribute('data-step'));
- var rangeVal = max - min;
- var range = document.getElementById(thumb.getAttribute('data-rangeid'));
- if(e.clientX > pos_init) {
- if(range.value >= max) return;
- var x = (e.clientX - posX) - parseFloat(thumb.style.width);
- var value = ((x / w) * (max - min)) + min;
- value = Math.round((value - min) / step) * step + min;
- if(value < min) {
- value = min;
- }
- else if(value > max) {
- value = min + ~~(rangeVal / step) * step;
- }
- thumb.style.left = (((value - min) / (max - min)) * w) + posX + parseFloat(thumb.style.width) / 2 + 'px';
- thumb.setAttribute('data-clicked', e.clientX);
- range.value = value;
- if(range.onchange()) range.onchange(e);
- }
- else if(e.clientX < pos_init) {
- if(range.value <= min) return;
- var x = (e.clientX - posX) - parseFloat(thumb.style.width);
- var value = ((x / w) * (max - min)) + min;
- value = Math.round((value - min) / step) * step + min;
- if(value < min) {
- value = min;
- }
- else if(value > max) {
- value = min + ~~(rangeVal / step) * step;
- }
- thumb.style.left = (((value - min) / (max - min)) * w) + posX + parseFloat(thumb.style.width) / 2 + 'px';
- thumb.setAttribute('data-clicked', e.clientX);
- range.value = value;
- if(range.onchange()) range.onchange(e);
- }
- else return;
- }
- }
- };
- document.onmouseup = function(e) {
- var thumbs = document.getElementsByClassName('thumb');
- for(var i=0;i<thumbs.length;i++) {
- if(thumbs[i].getAttribute('data-clicked') != 'false') {
- thumbs[i].setAttribute('data-clicked', 'false');
- thumbs[i].onblur(e);
- }
- }
- };
- window.onkeydown = function(e) {
- if(e.keyCode == 39) {
- var thumbs = document.getElementsByClassName('thumb');
- for(var i=0;i<thumbs.length;i++) {
- if(thumbs[i].getAttribute('data-focused').toString() == 'true') {
- var thumb = thumbs[i];
- var range = document.getElementById(thumb.getAttribute('data-rangeid'));
- var posX = parseFloat(thumb.getAttribute('data-posx'));
- var w = parseFloat(thumb.getAttribute('data-rangepx'));
- var max = parseFloat(thumb.getAttribute('data-max'));
- var min = parseFloat(thumb.getAttribute('data-min'));
- var step = parseFloat(thumb.getAttribute('data-step'));
- var rangeVal = max - min;
- var value = (range.value != step) ? parseFloat(range.value) + parseFloat((step < 1) ? 1 : step) : step * 2;
- if(value < min) {
- value = min;
- }
- else if(value > max) {
- value = min + ~~(rangeVal / step) * step;
- }
- thumb.style.left = (((value - min) / (max - min)) * w) + posX + parseFloat(thumb.style.width) / 2 + 'px';
- thumb.setAttribute('data-clicked', e.clientX);
- range.value = value;
- if(range.onchange()) range.onchange(e);
- }
- }
- }
- else if(e.keyCode == 37) {
- var thumbs = document.getElementsByClassName('thumb');
- for(var i=0;i<thumbs.length;i++) {
- if(thumbs[i].getAttribute('data-focused').toString() == 'true') {
- var thumb = thumbs[i];
- var range = document.getElementById(thumb.getAttribute('data-rangeid'));
- var posX = parseFloat(thumb.getAttribute('data-posx'));
- var w = parseFloat(thumb.getAttribute('data-rangepx'));
- var max = parseFloat(thumb.getAttribute('data-max'));
- var min = parseFloat(thumb.getAttribute('data-min'));
- var step = parseFloat(thumb.getAttribute('data-step'));
- var rangeVal = max - min;
- var value = (range.value != step) ? range.value - ((step < 1) ? 1 : step) : 0;
- if(value < min) {
- value = min;
- }
- else if(value > max) {
- value = min + ~~(rangeVal / step) * step;
- }
- thumb.style.left = (((value - min) / (max - min)) * w) + posX + parseFloat(thumb.style.width) / 2 + 'px';
- thumb.setAttribute('data-clicked', e.clientX);
- range.value = value;
- if(range.onchange()) range.onchange(e);
- }
- }
- }
- };
- }
- };
- // Call IESlidesFixer() each time you add a slider using JavaScript !
- document.addEventListener('DOMContentLoaded', IESlidesFixer, false);
- /*
- html5slider - a JS implementation of <input type=range> for Firefox 16 and up
- https://github.com/fryn/html5slider
- Copyright (c) 2010-2012 Frank Yan, <http://frankyan.com>
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- */
- (function() {
- // test for native support
- var test = document.createElement('input');
- try {
- test.type = 'range';
- if (test.type == 'range')
- return;
- } catch (e) {
- return;
- }
- // test for required property support
- test.style.background = 'linear-gradient(red, red)';
- if (!test.style.backgroundImage || !('MozAppearance' in test.style) ||
- !document.mozSetImageElement || !this.MutationObserver)
- return;
- var scale;
- var isMac = navigator.platform == 'MacIntel';
- var thumb = {
- radius: isMac ? 9 : 6,
- width: isMac ? 22 : 12,
- height: isMac ? 16 : 20
- };
- var track = 'linear-gradient(transparent ' + (isMac ?
- '6px, #999 6px, #999 7px, #ccc 8px, #bbb 9px, #bbb 10px, transparent 10px' :
- '9px, #999 9px, #bbb 10px, #fff 11px, transparent 11px') +
- ', transparent)';
- var styles = {
- 'min-width': thumb.width + 'px',
- 'min-height': thumb.height + 'px',
- 'max-height': thumb.height + 'px',
- padding: '0 0 ' + (isMac ? '2px' : '1px'),
- border: 0,
- 'border-radius': 0,
- cursor: 'default',
- 'text-indent': '-999999px' // -moz-user-select: none; breaks mouse capture
- };
- var options = {
- attributes: true,
- attributeFilter: ['min', 'max', 'step', 'value']
- };
- var forEach = Array.prototype.forEach;
- var onChange = document.createEvent('HTMLEvents');
- onChange.initEvent('change', true, false);
- if (document.readyState == 'loading')
- document.addEventListener('DOMContentLoaded', initialize, true);
- else
- initialize();
- function initialize() {
- // create initial sliders
- forEach.call(document.querySelectorAll('input[type=range]'), transform);
- // create sliders on-the-fly
- new MutationObserver(function(mutations) {
- mutations.forEach(function(mutation) {
- if (mutation.addedNodes)
- forEach.call(mutation.addedNodes, function(node) {
- check(node);
- if (node.childElementCount)
- forEach.call(node.querySelectorAll('input'), check);
- });
- });
- }).observe(document, { childList: true, subtree: true });
- }
- function check(input) {
- if (input.localName == 'input' && input.type != 'range' &&
- input.getAttribute('type') == 'range')
- transform(input);
- }
- function transform(slider) {
- var isValueSet, areAttrsSet, isChanged, isClick, prevValue, rawValue, prevX;
- var min, max, step, range, value = slider.value;
- // lazily create shared slider affordance
- if (!scale) {
- scale = document.body.appendChild(document.createElement('hr'));
- style(scale, {
- '-moz-appearance': isMac ? 'scale-horizontal' : 'scalethumb-horizontal',
- display: 'block',
- visibility: 'visible',
- opacity: 1,
- position: 'fixed',
- top: '-999999px'
- });
- document.mozSetImageElement('__sliderthumb__', scale);
- }
- // reimplement value and type properties
- var getValue = function() { return '' + value; };
- var setValue = function setValue(val) {
- value = '' + val;
- isValueSet = true;
- draw();
- delete slider.value;
- slider.value = value;
- slider.__defineGetter__('value', getValue);
- slider.__defineSetter__('value', setValue);
- };
- slider.__defineGetter__('value', getValue);
- slider.__defineSetter__('value', setValue);
- slider.__defineGetter__('type', function() { return 'range'; });
- // sync properties with attributes
- ['min', 'max', 'step'].forEach(function(prop) {
- if (slider.hasAttribute(prop))
- areAttrsSet = true;
- slider.__defineGetter__(prop, function() {
- return this.hasAttribute(prop) ? this.getAttribute(prop) : '';
- });
- slider.__defineSetter__(prop, function(val) {
- val === null ? this.removeAttribute(prop) : this.setAttribute(prop, val);
- });
- });
- // initialize slider
- slider.readOnly = true;
- style(slider, styles);
- update();
- new MutationObserver(function(mutations) {
- mutations.forEach(function(mutation) {
- if (mutation.attributeName != 'value') {
- update();
- areAttrsSet = true;
- }
- // note that value attribute only sets initial value
- else if (!isValueSet) {
- value = slider.getAttribute('value');
- draw();
- }
- });
- }).observe(slider, options);
- slider.addEventListener('mousedown', onDragStart, true);
- slider.addEventListener('keydown', onKeyDown, true);
- slider.addEventListener('focus', onFocus, true);
- slider.addEventListener('blur', onBlur, true);
- function onDragStart(e) {
- isClick = true;
- setTimeout(function() { isClick = false; }, 0);
- if (e.button || !range)
- return;
- var width = parseFloat(getComputedStyle(this, 0).width);
- var multiplier = (width - thumb.width) / range;
- if (!multiplier)
- return;
- // distance between click and center of thumb
- var dev = e.clientX - this.getBoundingClientRect().left - thumb.width / 2 -
- (value - min) * multiplier;
- // if click was not on thumb, move thumb to click location
- if (Math.abs(dev) > thumb.radius) {
- isChanged = true;
- this.value -= -dev / multiplier;
- }
- rawValue = value;
- prevX = e.clientX;
- this.addEventListener('mousemove', onDrag, true);
- this.addEventListener('mouseup', onDragEnd, true);
- }
- function onDrag(e) {
- var width = parseFloat(getComputedStyle(this, 0).width);
- var multiplier = (width - thumb.width) / range;
- if (!multiplier)
- return;
- rawValue += (e.clientX - prevX) / multiplier;
- prevX = e.clientX;
- isChanged = true;
- this.value = rawValue;
- }
- function onDragEnd() {
- this.removeEventListener('mousemove', onDrag, true);
- this.removeEventListener('mouseup', onDragEnd, true);
- }
- function onKeyDown(e) {
- if (e.keyCode > 36 && e.keyCode < 41) { // 37-40: left, up, right, down
- onFocus.call(this);
- isChanged = true;
- this.value = value + (e.keyCode == 38 || e.keyCode == 39 ? step : -step);
- }
- }
- function onFocus() {
- if (!isClick)
- this.style.boxShadow = !isMac ? '0 0 0 2px #fb0' :
- 'inset 0 0 20px rgba(0,127,255,.1), 0 0 1px rgba(0,127,255,.4)';
- }
- function onBlur() {
- this.style.boxShadow = '';
- }
- // determines whether value is valid number in attribute form
- function isAttrNum(value) {
- return !isNaN(value) && +value == parseFloat(value);
- }
- // validates min, max, and step attributes and redraws
- function update() {
- min = isAttrNum(slider.min) ? +slider.min : 0;
- max = isAttrNum(slider.max) ? +slider.max : 100;
- if (max < min)
- max = min > 100 ? min : 100;
- step = isAttrNum(slider.step) && slider.step > 0 ? +slider.step : 1;
- range = max - min;
- draw(true);
- }
- // recalculates value property
- function calc() {
- if (!isValueSet && !areAttrsSet)
- value = slider.getAttribute('value');
- if (!isAttrNum(value))
- value = (min + max) / 2;;
- // snap to step intervals (WebKit sometimes does not - bug?)
- value = Math.round((value - min) / step) * step + min;
- if (value < min)
- value = min;
- else if (value > max)
- value = min + ~~(range / step) * step;
- }
- // renders slider using CSS background ;)
- function draw(attrsModified) {
- calc();
- if (isChanged && value != prevValue)
- slider.dispatchEvent(onChange);
- isChanged = false;
- if (!attrsModified && value == prevValue)
- return;
- prevValue = value;
- var position = range ? (value - min) / range * 100 : 0;
- var bg = '-moz-element(#__sliderthumb__) ' + position + '% no-repeat, ';
- style(slider, { background: bg + track });
- }
- }
- function style(element, styles) {
- for (var prop in styles)
- element.style.setProperty(prop, styles[prop], 'important');
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment