Slider - Documentation | www.HtmlElements.com

Overview

Smart.Slider represents a numeric control used to select a numeric value from a range of values by moving a thumb along a track.

Files

  • CSS files
    • smart.default.css - the CSS file containing the styles for the element.
  • Javascript module
    • smart.slider.js - the JS module which is in the source/modules/ folder and loads all script dependencies.
  • Javascript files
    • smart.element.js - the base class.
    • smart.button.js - additional JS file containing the definition for the value control buttons.
    • smart.numeric.js - this file contains functions used for the processing of numeric objects.
    • smart.math.js - the JS file containing containing many of the mathematical functions used by element.
    • smart.tickintervalhandler.js - the JS file containing functions used for calculating the tick interval of the element.
    • smart.tank.js - the JS file containing the definition for slider's base class - smartTank.
    • smart.slider.js - the JS file containing the definition for the element.

Usage

  • Import a module

    With this approach, we import a module and create the web component by using the Smart function. The #slider is a smart-slider tag.

    import {smartSlider} from "../../source/modules/smart.slider.js";
    
    Smart('#slider', class {
    get properties() {
    	return {
    		orientation: 'horizontal'
    	}
    }
    });
    
    document.readyState === 'complete' ? init() : window.onload = init;
    
    function init() { 
    
    }
    
    Using the Smart function is optional. You can use const slider = document.querySelector("#slider"); and set the properties like that:
    const slider = document.querySelector("#slider");
    
    slider.orientation = 'horizontal';
    

  • Import a module, Init on Demand

    The following imports the web component's module and creates it on demand, when the document is ready. The #slider is a DIV tag.

    import {smartSlider} from "../../source/modules/smart.slider.js";
    
    document.readyState === 'complete' ? init() : window.onload = init;
    
    function init() { 
    const slider = new smartSlider('#slider', {
       	orientation: 'horizontal'
    });
    }
    
  • Load scripts

    The following code adds the custom element to the page.

    <!DOCTYPE html>
    <html lang="en">
    <head>
     <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
     <script type="text/javascript" src="../../source/smart.element.js"></script>
     <script type="text/javascript" src="../../source/smart.button.js"></script>
     <script type="text/javascript" src="../../source/smart.numeric.js"></script>
     <script type="text/javascript" src="../../source/smart.math.js"></script>
     <script type="text/javascript" src="../../source/smart.tickintervalhandler.js"></script>
     <script type="text/javascript" src="../../source/smart.tank.js"></script>
     <script type="text/javascript" src="../../source/smart.slider.js"></script>
    </head>
    <body>
     <smart-slider></smart-slider>
    </body>
    </html>
    

    Note how smart.element.js is declared before everything else. This is mandatory for all custom elements.

    Demo

Create, Append, Remove, Get/Set Property, Invoke Method, Bind to Event


Create a new element:
const slider = document.createElement('smart-slider');

Append it to the DOM:
document.body.appendChild(slider);

Remove it from the DOM:
slider.parentNode.removeChild(slider);

Set a property:
slider.propertyName = propertyValue;

Get a property value:
const propertyValue = slider.propertyName;

Invoke a method:
slider.methodName(argument1, argument2);

Add Event Listener:
const eventHandler = (event) => {
// your code here.
};

toast.addEventListener(eventName, eventHandler);

Remove Event Listener:
toast.removeEventListener(eventName, eventHandler, true);

In order to change the value of the slider the value property has to be applied via javascript:

<!DOCTYPE html>
<html lang="en">
<head>
 <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
 <script type="text/javascript" src="../../source/smart.element.js"></script>
 <script type="text/javascript" src="../../source/smart.button.js"></script>
 <script type="text/javascript" src="../../source/smart.numeric.js"></script>
 <script type="text/javascript" src="../../source/smart.math.js"></script>
 <script type="text/javascript" src="../../source/smart.tickintervalhandler.js"></script>
 <script type="text/javascript" src="../../source/smart.tank.js"></script>
 <script type="text/javascript" src="../../source/smart.slider.js"></script>
 <script>
     window.onload = function () {
         var slider = document.querySelector('smart-slider');
         slider.value = 25;
     }
 </script>
</head>
<body>
 <smart-slider></smart-slider>
</body>
</html>

Demo

or set it as HTML attribute:

<smart-slider value="25"></smart-slider>

Demo

Appearance

Smart.Slider can be either horizontal or vertical. The default orientation is horizontal but that can be changed on initialization in the HTML:

<smart-slider orientation="vertical"></smart-slider>

Demo

or later using javascript:

<!DOCTYPE html>
<html lang="en">
<head>
 <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
 <script type="text/javascript" src="../../source/smart.element.js"></script>
 <script type="text/javascript" src="../../source/smart.button.js"></script>
 <script type="text/javascript" src="../../source/smart.numeric.js"></script>
 <script type="text/javascript" src="../../source/smart.math.js"></script>
 <script type="text/javascript" src="../../source/smart.tickintervalhandler.js"></script>
 <script type="text/javascript" src="../../source/smart.tank.js"></script>
 <script type="text/javascript" src="../../source/smart.slider.js"></script>
 <script>
     window.onload = function () {
         var slider = document.querySelector('smart-slider');
         slider.orientation = 'vertical';
     }
 </script>
</head>
<body>
 <smart-slider></smart-slider>
</body>
</html>

Demo

Smart.Slider uses a scale to indicate the value. The scale can be positioned above, under or on both sides of the element thanks to the scalePosition property. By default it's located over the element for horizontal slider or before the element for vertical slider.

Let's change the position of the scale via javascript:

<smart-slider scale-position="far"></smart-slider>

Demo

The ticks of the scale can also be customized. They can be positioned above the track or inside it by setting the tickPosition property like so:

<smart-slider ticks-position="track"></smart-slider>

Demo


The scale of the Slider contains major and minor ticks which are visible by default. This can be re-configured. The user can display only the major, the minor or none of them if he prefers by setting the ticksVisibility property. This can also be done in the HTML code on initialization like so:

<smart-slider ticks-visibility="major"></smart-slider>

Demo


The scale of the Slider has ticks and labels. Labels are also customizable. The user can control which of them to be visible:

  • none - no labels are visible
  • all - all labels are visible
  • endPoints - only the first and last labels are visible

The text of the label can also be modified thanks to the labelFormatFunction property. It's a format function that takes one argument - the value of the label. The function must return a string representing the new text for the lables. The property can be applied via javascript like so:

                    <!DOCTYPE html>
<html lang="en">
<head>
 <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
 <script type="text/javascript" src="../../source/smart.element.js"></script>
 <script type="text/javascript" src="../../source/smart.button.js"></script>
 <script type="text/javascript" src="../../source/smart.numeric.js"></script>
 <script type="text/javascript" src="../../source/smart.math.js"></script>
 <script type="text/javascript" src="../../source/smart.tickintervalhandler.js"></script>
 <script type="text/javascript" src="../../source/smart.tank.js"></script>
 <script type="text/javascript" src="../../source/smart.slider.js"></script>
 <script>
     window.onload = function () {
             var slider = document.querySelector('smart-slider');
             slider.labelFormatFunction = function (value) {
             return value + ' ' + 'in';
         }
     }
 </script>
</head>
<body>
 <smart-slider></smart-slider>
</body>
</html>

Demo

Smart.Slider has control buttons that are hidden by default. They can be enabled by setting the property showButtons. The buttons are positioned infront and after the track scale and can be used to decrease or increase the value of the slider. The property can be set as an attribute in the HTML tag of the element:

<smart-slider show-buttons></smart-slider>

or using javascript:

<!DOCTYPE html>
<html lang="en">
<head>
 <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
 <script type="text/javascript" src="../../source/smart.element.js"></script>
 <script type="text/javascript" src="../../source/smart.button.js"></script>
 <script type="text/javascript" src="../../source/smart.numeric.js"></script>
 <script type="text/javascript" src="../../source/smart.math.js"></script>
 <script type="text/javascript" src="../../source/smart.tickintervalhandler.js"></script>
 <script type="text/javascript" src="../../source/smart.tank.js"></script>
 <script type="text/javascript" src="../../source/smart.slider.js"></script>
 <script>
     window.onload = function () {
         var slider = document.querySelector('smart-slider');
         slider.showButtons = true;
     }
 </script>
</head>
<body>
 <smart-slider></smart-slider>
</body>
</html>

Demo

Behavior

Smart.Slider allows two types of scales:

  • integer - the values are integers only
  • floatingPoint - the values are floating point numbers

The type of the scale is determined by the scaleType property which can be set on initialization:

<smart-slider scale-type="integer"></smart-slider>

Demo

or changed later when the element is ready using javascript:

<script>
     window.onload = function () {
         var slider = document.querySelector('smart-slider');
         slider.scaleType = 'floatingPoint';
     }
 </script>

Demo

When the scaleType is set to "floatingPoing", the user can adjust the precision of the value via the "precisionDigits" property. This property determines how many numbers will appear after the decimal point of the current value. Can be set like every other property:

<smart-slider precision-digits="2"></smart-slider>

Demo

Smart.Slider allows controlling the number significant digits shown on the scale. The significantDigits property determines how the value will be represented. For example, let's say the user wants to configure the value to contain only 3 significant digits:

<smart-slider significant-digits="3"></smart-slider>

Note: significantDigits and precisionDigits can't be applied at the same time.


Smart.Slider supports big numbers as well. The wordLength property determines the range of numbers the element can display.

The available word lengths are:

  • int8 : from –128 to 127
  • uint8 : from 0 to 255
  • int16 : from –32,768 to 32,767
  • uint16 : from 0 to 65,535
  • int32 : from –2,147,483,648 to 2,147,483,647. Default value.
  • uint32 : from 0 to 4,294,967,295
  • int64 : from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • uint64 : from 0 to 18,446,744,073,709,551,615

Here's how to set it as attribute:

<smart-slider word-length="uint8"></smart-slider>

The mechanicalAction property of the element determines when the value will change. This property determines the behavior of the element. Possible values are:

  • switchUntilReleased - changes the value while the user is dragging the thumb. When the thumb is released the value is returned to it's initial position.
  • switchWhenReleased - changes the value only when the thumb is released. Otherwise the value remains unchaged.
  • switchWhileDragging - changes the value while the user is dragging the thumb and retains the last value when the thumb is released. The default value of the property.
<smart-slider mechanical-action="switchWhenReleased"></smart-slider>

Coerce property determines the value interation. Once enabled the element uses the interval property to determines the next possible selectable value from the scale.

By setting the interval property the user can control the interval between the values. Interval is a property of type number and can be set on initialization or using javascript:

<!DOCTYPE html>
<html lang="en">
<head>
 <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
 <script type="text/javascript" src="../../source/smart.element.js"></script>
 <script type="text/javascript" src="../../source/smart.numeric.js"></script>
 <script type="text/javascript" src="../../source/smart.math.js"></script>
 <script type="text/javascript" src="../../source/smart.tickintervalhandler.js"></script>
 <script type="text/javascript" src="../../source/smart.tank.js"></script>
<script type="text/javascript" src="../../source/smart.slider.js"></script>
 <script>
     window.onload = function () {
         var slider = document.querySelector('smart-slider');
         slider.interval = 10;
         slider.coerce = true;
     }
 </script>
</head>
<body>
 <smart-slider></smart-slider>
</body>
</html>

Demo

If coerce is enabled the thumb will snap to the next value based on the interval.


The slider element can behave like a range selector using two thumbs. The property rangeSlider determines if the second thumb is enabled or not. If applied the user can set a range value via the values property using javascript:

<!DOCTYPE html>
<html lang="en">
<head>
 <link rel="stylesheet" href="../../source/styles/smart.default.css" type="text/css" />
 <script type="text/javascript" src="../../source/smart.element.js"></script>
 <script type="text/javascript" src="../../source/smart.button.js"></script>
 <script type="text/javascript" src="../../source/smart.numeric.js"></script>
 <script type="text/javascript" src="../../source/smart.math.js"></script>
 <script type="text/javascript" src="../../source/smart.tickintervalhandler.js"></script>
 <script type="text/javascript" src="../../source/smart.tank.js"></script>
 <script type="text/javascript" src="../../source/smart.slider.js"></script>
 <script>
     window.onload = function () {
         var slider = document.querySelector('smart-slider');
         slider.values = [50, 80];
     }
 </script>
</head>
<body>
 <smart-slider range-slider></smart-slider>
</body>
</html>

Demo

in the tag of the element during initialization:

<smart-slider range-slider values='[50, 80]'></smart-slider>

Demo

or use the thumbs on the scale to calibrate the values directly.

The property value is not used when rangeSlider is enabled.

Keyboard Support

Smart.Slider implements the following keys:

Key Action
Arrow Up / Arrow Right Increases the value or moves the second thumb forward if rangeSlider is enabled.
Arrow Left / Arrow Down Decreases the value or moves the first thumb backwards if rangeSlider is enabled.
Home Sets the value to min or moves the first thumb to the beggining if rangeSlider is enabled.
End Sets the value to max or moves the second thumb to the end if rangeSlider is enabled.