Welcome

Hi, and welcome. I'm glad you're here.
You're probably here because you're looking for a slider to compare images. Very good. Because you've come to the right place.
If you don’t want to read this welcome text "Blafasel", you can jump directly to the Quickstart or to the Examples.

The any Image Comparison Slider (I know, it's a bulky name) on the one hand is super slim (only a few KB) and does not need any additional libraries (even without jQuery).
On the other hand it is super flexible, versatile and highly customizable. With just a few simple steps, the Slider is ready to use very quickly and runs on almost any device and with any browser (this includes ie8 and other oldschool friends).

In short, it is the last slider you’ll ever need. Trust me! 😉
Now have fun.

Quickstart Setup Introdution Configuration Examples

1.

Quickstart

Let's go

If you are in a hurry, this is the perfect place to start. Below you will find an example of the default slider and everything you need to get this thing running.
But if you want to customize your slider and learn more about the possibilities and areas of application, please have a look at the Introduction, the Configuration and the Examples.

This is the default slider. If you want to use it directly as shown, just follow the steps below and you will be done in no time.

First of all we embed the JavaScript file into the page:

HTML

<script src='https://cdn.jsdelivr.net/npm/jquery-anyimagecomparisonslider-plugin'></script>
                        


Then insert the following code at the position of your page where you want to have the slider. Of course, you have to adjust the image paths accordingly. This is done with the data-src attribute.

HTML

<div id="aics-autostart">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
                        


You did it. Congrats, you have successfully added your first any Image Comparison slider to your site.

The special feature of this example is that we don't have to initialize the slider in JavaScript/jQuery. If you include the words aics and autostart in the id, the initialization will be done autoamitically.

For a better understanding and to make it easier for you to get started, you can also find this Quickstart example on CodePen and JSFiddle.

2.

Setup

Download

First, the old school method. Just download the files.

The minified file:
jquery.anyimagecomparisonslider.min.js

And for those who are interested in the non-minified version:
jquery.anyimagecomparisonslider.js

Just click on the links. You will be directed to the corresponding file. Now click on file in your browser menu and then click on "save as..." or on "save page as..." and save the file in your project folder.
Or simply use the key combination CTRL + S to save the file.

Install

Install it via Package Managers:

NPM

npm install jquery-anyimagecomparisonslider-plugin --save
                        
YARN

yarn add jquery-anyimagecomparisonslider-plugin
                        
Embed

After you have downloaded the file and placed it in your project, you just have to embed it in your page:


<script src='./path/jquery.anyimagecomparisonslider.min.js'></script>
                        


Please note, if you want to use the slider together with jQuery, then implement the slider script behind jQuery:


<script src='./path/jquery-3.4.1.min.js'></script>
<script src='./path/jquery.anyimagecomparisonslider.min.js'></script>
                        


jquery.anyimagecomparisonslider.min.js & jquery.anyimagecomparisonslider.js are also available on jsDelivr via npm and can be embedded per CDN.


<script src='https://cdn.jsdelivr.net/npm/jquery-anyimagecomparisonslider-plugin@1.0.4/js/jquery.anyimagecomparisonslider.min.js'></script>
                        


Or the short form:


<script src='https://cdn.jsdelivr.net/npm/jquery-anyimagecomparisonslider-plugin'></script>
                        
3.

Introduction

Let's continue

After we learned how to integrate the default slider into your site as fast as possible in the Quickstart section, we now also know how to download and install the files. Let’s go a little deeper into detail and deviate from the standard configuration before we look at all configuration options in detail.

This is almost the same slider as in the Quickstart. However, it is configured in such a way that the behavior has changed almost completely.
In addition, this and all subsequent sliders in the examples are initialized by JavaScript/jQuery.

To understand how to configure the slider with attributes or parameters, let's first look at the complete HTML code.
Then we will look at all possible parameters in detail.

First we insert the HTML code for our slider again at the desired position in our page.

HTML

<div id="aics-2" data-orientation="vertical" data-background-color="#ffffff" data-cursor="ns-resize" data-follow-easing-factor="10" data-group="g2">
    <div class="images">
        <div class="image-btm" data-src="./path/image-btm.jpg"></div>
        <div class="image-top" data-src="./path/image-top.jpg"></div>
    </div>
    <div class="ui">
        <a class="button-btm" alt="full image btm" target="_blank">2020</a>
        <a class="button-top" alt="full image top" target="_blank">2019</a>
    </div>
</div>
                        


At first glance, the HTML code looks almost the same as in the default example. But if we take a closer look we can see two differences. First, we pass various data attributes with parameters for the slider. And second we added another div to set the buttons in the slider.

Here you can see the data attributes used for this slider in detail.

HTML

data-orientation="vertical" //default value: "horizontal"
data-background-color="#ffffff" //default value: "#000000"
data-cursor="ns-resize" //default value: "ew-resize"
data-follow-easing-factor="10" //default value: "0"
data-group="g2" //default value: ""
                        


The exact configuration of the slider with attributes and parameters is described in the chapter Configuration.

The following code creates the two buttons in the slider. With these buttons the user can easily download the images used in the slider.

HTML

<div class="ui">
    <a class="button-rgt" alt="full image rgt" target="_blank">2020</a>
    <a class="button-lft" alt="full image lft" target="_blank">2019</a>
</div>
                        


The buttons must be placed inside a div with the class ui. In addition, the buttons themselves should always use one of the following classes so that they are correctly recognized by JavaScript.

CSS

button-rgt
button-lft
button-top
button-btm
                        


Let's now continue with the initialization.
Besides the automatic initialization without a single line of JavaScript/jQuery code and using the words aics and autostart in the id of the slider, there is the ability to initialize the slider yourself.
There are two possibilities depending on whether you want to work with pure JavaScript or jQuery.

JavaScript

new AnyImageComparisonSlider( document.getElementById( 'aics-2' ) );
                        
jQuery

$( '#aics-2' ).anyImageComparisonSlider();
                        


Good job! You have successfully added and initialized your first self-configured Any-Image-Comparison slider to your site.

Please note that if you do not want the slider to initialize automatically, don't use a combination of the words aics and autostart in the slider id.
In the following examples we always use the non-automatic initialization as shown here.

4.

Configuration

Let's go into detail

In this chapter we will take a close look at the slider in all its facets, its settings and public methods.

HTML:

We already know the basic structure from the Quickstart chapter and the short introduction. Now, let's have a closer look at all possible variations.

Let's start with the easiest one:


<div id="aics">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
                        


At this point we create the div element for our slider and set the id (aics) for this element so that we can access it in JavaScript/jQuery to initialize the slider.
Furthermore, in our slider div element we create another div element with the class images.
And in this images div we create two more divs for the images of the slider:


<div class="images">
    <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
    <div class="image-lft" data-src="./path/image-lft.jpg"></div>
</div>
                        


Each of the two div elements for the images must have one of the following classes:


class="image-rgt"
class="image-lft"
class="image-top"
class="image-btm"
                        


And with the attribute data-src you define the file path:


data-src="./path/image-lft.jpg"
                        


Optional: We can even add two more attributes to the div elements for the images:


data-width="1200"
data-height="900"
                        


1200 and 900 are example values only. At this point you simply pass the values for the original width and height of the images.
For example, if your original image is 900 * 600 pixels in size, unlike the images used here, you would pass the following values:


data-width="900"
data-height="600"
                        


A standard slider with these optional attributes would look like this:


<div id="aics">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg" data-width="1200" data-height="900"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg" data-width="1200" data-height="900"></div>
    </div>
</div>
                        


But why should you assign these optional attributes at all?
Because this way the slider element can already be displayed in its final size without the slider having been completely loaded or initialized.
This way you avoid unpleasant jumping effects on the page when your sliders have to be loaded first and can only take on their final size afterwards.

As we have already seen in the second example, we can add buttons to the slider to download the images used in the slider:


<div id="aics">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg" data-width="1200" data-height="900"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg" data-width="1200" data-height="900"></div>
    </div>
    <div class="ui">
        <a class="button-rgt" alt="full image rgt" target="_blank">2020</a>
        <a class="button-lft" alt="full image lft" target="_blank">2019</a>
    </div>
</div>
                        


Therefore, we need a div with the class ui which we place inside the slider div:


<div class="ui">
    <a class="button-rgt" alt="full image rgt" target="_blank">2020</a>
    <a class="button-lft" alt="full image lft" target="_blank">2019</a>
</div>
                        


In this ui div we now create our buttons. The buttons themselves should always use one of the following classes so that they are correctly recognized by JavaScript:


button-rgt
button-lft
button-top
button-btm
                        


Of course you can also specify the button label, set an alt text and set the target so that a click on the button opens a new browser tab.

But we can do even more in the ui div:


<div class="ui">
    <a class="button-rgt" alt="full image rgt" target="_blank">2020</a>
    <a class="button-lft" alt="full image lft" target="_blank">2019</a>
    <img class="dragger" src="./path/dragger-h.png" alt="dragger" width="84" height="48">
</div>
                        


We can add an img element with the class dragger. If this element is present, the slider automatically places it in the middle of the separator. At this point you can use any image you like. Don't forget to specify the width and height of your image:


<img class="dragger" src="./path/dragger-h.png" alt="dragger" width="84" height="48">
                        


And this is the complete markup for a horizontal slider:


<div id="aics">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg" data-width="1200" data-height="900"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg" data-width="1200" data-height="900"></div>
    </div>
    <div class="ui">
        <a class="button-rgt" alt="full image rgt" target="_blank">2020</a>
        <a class="button-lft" alt="full image lft" target="_blank">2019</a>
        <img class="dragger" src="./path/dragger-h.png" alt="dragger" width="84" height="48">
    </div>
</div>
                        


And here - for comparison - the markup for a vertical slider:


<div id="aics" data-orientation="vertical">
    <div class="images">
        <div class="image-btm" data-src="./path/image-btm.jpg" data-width="1200" data-height="900"></div>
        <div class="image-top" data-src="./path/image-top.jpg" data-width="1200" data-height="900"></div>
    </div>
    <div class="ui">
        <a class="button-btm" alt="full image btm" target="_blank">2020</a>
        <a class="button-top" alt="full image top" target="_blank">2019</a>
        <img class="dragger" src="./path/dragger-v.png" alt="dragger" width="48" height="84">
    </div>
</div>
                        


Now let's again look at the markup for a slider that works without initialization in JavaScript/jQuery:


<div id="aics-autostart">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
                        


The naming of the slider id plays the decisive role here:


<div id="aics-autostart">
                        


If you want your slider to be initialized automatically, you have to make sure that the name of the slider id contains the words aics and autostart.
The order or additional other words in the slider id are irrelevant.
The following Slider id for example also works so that the slider is automatically initialized:


<div id="aics-1-autostart">
                        


On the other hand, if you want to avoid that the initialization takes place automatically just avoid the word autostart in the slider id.

Enough slider markup! In the next step we will look at how to configure the slider.

Data attributes:

And now we have a closer look at how you can configure the slider using data attributes. In the Introduction chapter we could already get to know some attributes. But now it´s time to get to know them all.

These are all available data attributes with their default values:


data-orientation="horizontal"
data-initial-position="0"
data-width="100%"
data-background-color="none"
data-on-pointer-down="false"
data-cursor="ew-resize"
data-dividing-line="solid 1px rgba(255, 255, 255, .5)"
data-follow-easing-factor="0"
data-interactive="true"
data-auto-animation="true"
data-auto-animation-speed="8"
data-auto-animation-pause="1"
data-auto-animation-easing="inOutCubic"
data-control-others="false"
data-controlled-by-others="false"
data-controlled-reverse="false"
data-group=""
data-group-sync="false"
data-loading="lazy"
data-viewport-offset="100px"
data-sleep-mode="true"
                        


Let's take a closer look at the data attributes and how they work:

data-orientation Specify either horizontal or vertical. With this you can control whether your slider slides horizontally or vertically.
data-initial-position At this point you define the starting position of the slider's dividing line. The value must be a percentage and must be between 0 and 1.
data-width Enter either a percentage value such as 100% or 75%, or a fixed pixel value such as 500px or 728px. It depends on whether your slider should behave responsively. In addition, the following length dimensions are permitted: pt, em and vw.
data-background-color Here you set the background color of the slider. You can see this color until the images are loaded. You can enter none (if you don't want a background), or hex (#ffffff), rgb (rgb(255, 255, 255)) or rgba (rgba(255, 255, 255, 1.0)) values. Default value is none.
data-on-pointer-down Specify true or false here. If you select true here, your slider will only be active when you hold down the mouse button.
data-cursor Here you define which mouse cursor should be displayed when you move the mouse over the slider. You have the following options: ew-resize, ns-resize, grab, w-resize, s-resize, e-resize, n-resize, row-resize, col-resize, all-scroll, move, crosshair, pointer, default, auto, inherit, initial and unset. But wait, there is more. With the following snipped you can load any cursor: url("http://yourcursor.cur") 0 0, auto
data-dividing-line Here you specify how the dividing line of the slider should look. If you do not want a dividing line, then none will be specified. Otherwise you can use the default value: solid 1px rgba(255, 255, 255, .5)
data-follow-easing-factor If you want the slider to follow you without easing, then enter the value 0 here. Otherwise you can enter values from 0 to 100 to control the easing.
data-interactive If you want your slider to be non-interactive then enter false here. So the slider can not be controlled and runs only automatically.
data-auto-animation With true or false you can determine whether your slider should automatically play an animation in case there is no user interaction.
data-auto-animation-speed Here you define the speed of the automatic animation in seconds.
data-auto-animation-pause Here you define the time in seconds for the pause between the animations and the time that should pass after a user interaction until the automatic animation starts again.
data-auto-animation-easing Here you determine which easing function should be used for the automatic animation. The following functions are available: linear, inQuad, outQuad, inOutQuad, inCubic, outCubic, inOutCubic, inQuart, outQuart, inOutQuart, inQuint, outQuint, inOutQuint, inSine, outSine, inOutSine
data-control-others If you want this slider to control other sliders on the page, then enter true at this point. Otherwise take the default value false.
data-controlled-by-others If you want this slider to be controlled by other sliders on the page, then enter true here. Otherwise take the default value false.
data-controlled-reverse If you want this slider to be controlled in reverse by other sliders on this page, then enter true here. Otherwise take the default value false. Please note that this property only works in conjunction with data-controlled-by-others.
data-group You can group your sliders. If you use more than one slider on your site, you can group them by entering a group name. This is especially important if you only want to control certain other sliders on your site with your active slider.
data-group-sync You can also synchronize your sliders. If you want your slider to start only after the other sliders of its group are loaded and initialized, then enter true here. The default value is false.
data-loading Here you can choose either lazy or eager loading. If you take the default value of lazy, the slider will not be loaded until it is visible in the viewport. If you choose eager, the slider will be loaded and initialized immediately.
data-viewport-offset If lazy loading has been activated, then you can use this value to determine how much px earlier before entering the viewport the loading should start. The default value is 100px. If sleepMode is activated, you can also use viewportOffset to define how much px earlier before entering the viewport the rendering should start.
data-sleep-mode Here you can determine whether your slider should only be active/rendered if it is in the viewport of the browser considering the viewportOffset parameter. The default value is true.
This feature actually always makes sense. But especially if you want to include many sliders on one page you should make sure that true is selected here.

Example: A slider with all possible data attributes and their default values.

HTML

<div id="aics" data-orientation="horizontal" data-initial-position="0" data-width="100%" data-background-color="#000000" data-on-pointer-down="false" data-cursor="ew-resize" data-dividing-line="solid 1px rgba(255, 255, 255, .5)" data-follow-easing-factor="0" data-auto-animation="true" data-auto-animation-speed="8" data-auto-animation-pause="1" data-auto-animation-easing="inOutCubic" data-control-others="false" data-controlled-by-others="false" data-controlled-reverse="false" data-group="" data-group-sync="false" data-loading="lazy" data-viewport-offset="100px" data-sleep-mode="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
                        
Settings object:

But there is another way to configure your slider. This is done with a settings object that you pass during initialization in JavaScript or jQuery.

These are all available object properties with their default values:


var settings = {

    orientation: 'horizontal',
    initialPosition: 0.00,
    width: '100%',
    backgroundColor: 'none',
    onPointerDown: 'false',
    cursor: 'ew-resize',
    dividingLine: 'solid 1px rgba(255, 255, 255, .5)',
    followEasingFactor: 0,
    interactive: true,
    autoAnimation: true,
    autoAnimationSpeed: 8,
    autoAnimationPause: 1,
    autoAnimationEasing: 'inOutCubic',
    controlOthers: false,
    controlledByOthers: false,
    controlledReverse: false,
    group: '',
    groupSync: false,
    loading: 'lazy',
    viewportOffset: '100px',
    sleepMode: true,
    onReady: function(){}

};
                        


As you have seen, the object properties correspond exactly to the data attributes.
With one exception (onReady). onReady can only be used in conjunction with the settings object and its properties.
But let's take a closer look at the object properties as well:

orientation Specify either horizontal or vertical. With this you can control whether your slider slides horizontally or vertically.
initialPosition At this point you define the starting position of the slider's dividing line. The value must be a percentage and must be between 0 and 1.
width Enter either a percentage value such as 100% or 75%, or a fixed pixel value such as 500px or 728px. It depends on whether your slider should behave responsively. In addition, the following length dimensions are permitted: pt, em and vw.
backgroundColor Here you set the background color of the slider. You can see this color until the images are loaded. You can enter none (if you don't want a background), or hex (#ffffff), rgb (rgb(255, 255, 255)) or rgba (rgba(255, 255, 255, 1.0)) values. Default value is none.
onPointerDown Specify true or false here. If you select true here, your slider will only be active when you hold down the mouse button.
cursor Here you define which mouse cursor should be displayed when you move the mouse over the slider. You have the following options: ew-resize, ns-resize, grab, w-resize, s-resize, e-resize, n-resize, row-resize, col-resize, all-scroll, move, crosshair, pointer, default, auto, inherit, initial and unset. But wait, there is more. With the following snipped you can load any cursor: url("http://yourcursor.cur") 0 0, auto
dividingLine Here you specify how the dividing line of the slider should look. If you do not want a dividing line, then none will be specified. Otherwise you can use the default value: solid 1px rgba(255, 255, 255, .5)
followEasingFactor If you want the slider to follow you without easing, then enter the value 0 here. Otherwise you can enter values from 0 to 100 to control the easing.
interactive If you want your slider to be non-interactive then enter false here. So the slider can not be controlled and runs only automatically.
autoAnimation With true or false you can determine whether your slider should automatically play an animation in case there is no user interaction.
autoAnimationSpeed Here you define the speed of the automatic animation in seconds.
autoAnimationPause Here you define the time in seconds for the pause between the animations and the time that should pass after a user interaction until the automatic animation starts again.
autoAnimationEasing Here you determine which easing function should be used for the automatic animation. The following functions are available: linear, inQuad, outQuad, inOutQuad, inCubic, outCubic, inOutCubic, inQuart, outQuart, inOutQuart, inQuint, outQuint, inOutQuint, inSine, outSine, inOutSine
controlOthers If you want this slider to control other sliders on the page, then enter true at this point. Otherwise take the default value false.
controlledByOthers If you want this slider to be controlled by other sliders on the page, then enter true here. Otherwise take the default value false.
controlledReverse If you want this slider to be controlled in reverse by other sliders on this page, then enter true here. Otherwise take the default value false. Please note that this property only works in conjunction with data-controlled-by-others.
group You can group your sliders. If you use more than one slider on your site, you can group them by entering a group name. This is especially important if you only want to control certain other sliders on your site with your active slider.
groupSync You can also synchronize your sliders. If you want your slider to start only after the other sliders of its group are loaded and initialized, then enter true here. The default value is false.
loading Here you can choose either lazy or eager loading. If you take the default value of lazy, the slider will not be loaded until it is visible in the viewport. If you choose eager, the slider will be loaded and initialized immediately.
viewportOffset If lazy loading has been activated, then you can use this value to determine how much px earlier before entering the viewport the loading should start. The default value is 100px. If sleepMode is activated, you can also use viewportOffset to define how much px earlier before entering the viewport the rendering should start.
sleepMode Here you can determine whether your slider should only be active/rendered if it is in the viewport of the browser considering the viewportOffset parameter. The default value is true.
This feature actually always makes sense. But especially if you want to include many sliders on one page you should make sure that true is selected here.
onReady Here you can pass a callback function which will be called when the slider is fully loaded and initialized.


Now you know all properties of the settings object and can create your own settings object. The next step is to pass the settings object you just created:

JavaScript

new AnyImageComparisonSlider( document.getElementById( 'aics' ), settings );
                        
jQuery

$( '#aics' ).anyImageComparisonSlider( settings );
                        


Since in this example we configure everything via settings object, we do not need any data attributes.

HTML

<div id="aics">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
                        


Just a hint. The data attributes take precedence over the settings object properties. That means, if you want to use both attributes and object properties (which makes no sense in my opinion), the properties will be overwritten by the attributes with the same name.

No matter if you use the data attributes or the settings object properties. Thanks to an extensive error handling the any Image Comparison Slider helps you in case you have passed a parameter incorrectly. If the slider does not work as desired, it is worth checking the browser console to see if there is an error message that can help you to analyze the error.

Public methods:

Let us now continue with the slider's public methods and take a closer look:

pause() Pauses the slider.
unpause() Cancels the pause.
controlByExternalSource() If you want to control the slider with another element like an input slider, then this method is exactly what you are looking for.

The method takes three parameters:
  • The first parameter indicates whether the remote control starts, is in progress, or ends. So here you pass either true or false.
  • With the second parameter you pass the position for the slider. These values are percentages and can range from 0 to 1, 0 to 100, or 0 to 1000.
  • The third parameter tells the method the maximum value. For example, if your range input slider goes from 0 to 100, then you pass 100 at this point.
intersectsViewport() With this method you can find out if your slider is visible in the viewport of the browser considering the viewportOffset parameter.
Please note that this method only works if sleepMode = true.
getAllOthers() Returns an array of all sliders on the page that are in the same group except the one from which the method was called.
getOrientation() Returns a string that indicates whether the slider is horizontal or vertical.
getId() Returns a string with the id of the slider.
getPos() Returns a number that represents the percentage position of the slider's separator line.

Note: Please note that some of the methods will not work correctly until the slider is fully loaded and initialized.
In case you want to use certain methods it is recommended to use the onReady callback function to wait until the slider is fully loaded and initialized.


And here are the examples how you can call the methods in JavaScript and jQuery.

JavaScript

//init
var aics = new AnyImageComparisonSlider( document.getElementById( 'aics' ) );

//pause/unpause
aics.pause();
aics.unpause();

//start/in progress (50 = 50% of 100 = 100%):
aics.controlByExternalSource( true, 50, 100 );
//stop/end:
aics.controlByExternalSource( false );

//slider intersects with viewport
console.log( aics.intersectsViewport() );

//getter
console.log( aics.getAllOthers() );
console.log( aics.getOrientation() );
console.log( aics.getId() );
console.log( aics.getPos() );
                        
jQuery

//init
$( '#aics' ).anyImageComparisonSlider();

//pause/unpause
$( '#aics' ).anyImageComparisonSlider( 'pause' );
$( '#aics' ).anyImageComparisonSlider( 'unpause' );

//start/in progress (50 = 50% of 100 = 100%):
$( '#aics' ).anyImageComparisonSlider( 'controlByExternalSource', true, 50, 100 );
//stop/end:
$( '#aics' ).anyImageComparisonSlider( 'controlByExternalSource', false );

//slider intersects with viewport
$( '#aics' ).anyImageComparisonSlider( 'intersectsViewport', function( v ) { console.log( v ) } );

//getter
$( '#aics' ).anyImageComparisonSlider( 'getAllOthers', function( v ) { console.log( v ) } );
$( '#aics' ).anyImageComparisonSlider( 'getOrientation', function( v ) { console.log( v ) } );
$( '#aics' ).anyImageComparisonSlider( 'getId', function( v ) { console.log( v ) } );
$( '#aics' ).anyImageComparisonSlider( 'getPos', function( v ) { console.log( v ) } );
                        


There is so much more to discover. But we have the most important points now. Everything else in the Examples chapter.

5.

Examples

Example 1

In this example we go a step further and create 2 sliders that control each other.

Here we will focus our attention especially on 3 setting options.
The first thing we have to do is make sure that the two sliders are in one group (As a practical example we choose the value g3 as group name).
We do this either by data attribute:


data-group="g3"
                        


Or we can do it using the settings object and the property group:


group: 'g3'
                        


Next we need to make sure that the sliders are allowed to control other sliders in their group and let other sliders in their group control them. As always there are 2 possibilities for this, which I will not mention every time from now on:


data-control-others="true" 
data-controlled-by-others="true"
                        

controlOthers: true
controlledByOthers: true
                        


The following is optional. However, it is generally useful for grouped sliders. We synchronize the sliders with each other so that all sliders in the group start at the same time:


data-group-sync="true"
                        

groupSync: true
                        


And here is the complete code for this example:

HTML

<div id="aics-3" data-follow-easing-factor="10" data-control-others="true" data-controlled-by-others="true" data-group="g3" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
<div id="aics-4" data-follow-easing-factor="10" data-control-others="true" data-controlled-by-others="true" data-group="g3" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
                        
JavaScript

new AnyImageComparisonSlider( document.getElementById( 'aics-3' ) );
new AnyImageComparisonSlider( document.getElementById( 'aics-4' ) );
                        
jQuery

$( '#aics-3' ).anyImageComparisonSlider();
$( '#aics-4' ).anyImageComparisonSlider();
                        
Example 2

Let's go ahead and increase the number of controlling sliders to four. Another new feature is that two of the sliders are now vertically aligned and receive a special mouse cursor.

As in the previous example we have to make sure that the sliders are in the same group and can control each other:


data-group="g4">
                        

group: 'g4'
                        

data-control-others="true" 
data-controlled-by-others="true"
                        

controlOthers: true
controlledByOthers: true
                        


In addition to this, we now make sure that two of the sliders are vertically aligned:


data-orientation="vertical"
                        

orientation: 'vertical'
                        


Now we define a different mouse cursor for the vertical sliders:


data-cursor="ns-resize"
                        

cursor: 'ns-resize'
                        


Also in this example we want all sliders from this group to be synchronized:


data-group-sync="true"
                        

groupSync: true
                        


So much for the special features. Here's the complete code:

HTML

<div id="aics-5" data-orientation="horizontal" data-cursor="ew-resize" data-follow-easing-factor="5" data-control-others="true" data-controlled-by-others="true" data-group="g4" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
<div id="aics-6" data-orientation="vertical" data-cursor="ns-resize" data-follow-easing-factor="5" data-control-others="true" data-controlled-by-others="true" data-group="g4" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
<div id="aics-7" data-orientation="vertical" data-cursor="ns-resize" data-follow-easing-factor="5" data-control-others="true" data-controlled-by-others="true" data-group="g4" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
<div id="aics-8" data-orientation="horizontal" data-cursor="ew-resize" data-follow-easing-factor="5" data-control-others="true" data-controlled-by-others="true" data-group="g4" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
                        
JavaScript

new AnyImageComparisonSlider( document.getElementById( 'aics-5' ) );
new AnyImageComparisonSlider( document.getElementById( 'aics-6' ) );
new AnyImageComparisonSlider( document.getElementById( 'aics-7' ) );
new AnyImageComparisonSlider( document.getElementById( 'aics-8' ) );
                        
jQuery

$( '#aics-5' ).anyImageComparisonSlider();
$( '#aics-6' ).anyImageComparisonSlider();
$( '#aics-7' ).anyImageComparisonSlider();
$( '#aics-8' ).anyImageComparisonSlider();
                        
Example 3

Now things are getting a little crazy. We take it to the extreme and combine nine sliders into one group. Some of the sliders are aligned horizontally, others vertically. The slider in the middle, i.e. slider number 5, may control all other sliders, but cannot be controlled by other sliders. All other sliders are synchronized.

Everything we need for this example we already know from the previous two examples.
Also here we have to make sure that the sliders are all in one group:


data-group="g5">
                        

group: 'g5'
                        


We are making sure that the sliders are allowed to control other sliders and let the other sliders of the group control them:


data-control-others="true" 
data-controlled-by-others="true"
                        

controlOthers: true
controlledByOthers: true
                        


Only for the middle slider, slider number 5, we change the settings a little bit:


data-control-others="true" 
data-controlled-by-others="false"
                        

controlOthers: true
controlledByOthers: false
                        


Also in this example we want all sliders from this group to be synchronized:


data-group-sync="true"
                        

groupSync: true
                        


Here is the complete code for this example:

HTML

<div id="aics-9" data-orientation="horizontal" data-cursor="ew-resize" data-follow-easing-factor="5" data-control-others="true" data-controlled-by-others="true" data-group="g5" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
<div id="aics-10" data-orientation="vertical" data-cursor="ns-resize" data-follow-easing-factor="5" data-control-others="true" data-controlled-by-others="true" data-group="g5" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
<div id="aics-11" data-orientation="horizontal" data-cursor="ew-resize" data-follow-easing-factor="5" data-control-others="true" data-controlled-by-others="true" data-group="g5" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>

<div id="aics-12" data-orientation="horizontal" data-cursor="ew-resize" data-follow-easing-factor="5" data-control-others="true" data-controlled-by-others="true" data-group="g5" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
<div id="aics-13" data-orientation="vertical" data-cursor="ns-resize" data-follow-easing-factor="5" data-control-others="true" data-controlled-by-others="false" data-group="g5" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
<div id="aics-14" data-orientation="horizontal" data-cursor="ew-resize" data-follow-easing-factor="5" data-control-others="true" data-controlled-by-others="true" data-group="g5" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>

<div id="aics-15" data-orientation="horizontal" data-cursor="ew-resize" data-follow-easing-factor="5" data-control-others="true" data-controlled-by-others="true" data-group="g5" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
<div id="aics-16" data-orientation="vertical" data-cursor="ns-resize" data-follow-easing-factor="5" data-control-others="true" data-controlled-by-others="true" data-group="g5" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
<div id="aics-17" data-orientation="horizontal" data-cursor="ew-resize" data-follow-easing-factor="5" data-control-others="true" data-controlled-by-others="true" data-group="g5" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
                        
JavaScript

new AnyImageComparisonSlider( document.getElementById( 'aics-9' ) );
new AnyImageComparisonSlider( document.getElementById( 'aics-10' ) );
new AnyImageComparisonSlider( document.getElementById( 'aics-11' ) );
new AnyImageComparisonSlider( document.getElementById( 'aics-12' ) );
new AnyImageComparisonSlider( document.getElementById( 'aics-13' ) );
new AnyImageComparisonSlider( document.getElementById( 'aics-14' ) );
new AnyImageComparisonSlider( document.getElementById( 'aics-15' ) );
new AnyImageComparisonSlider( document.getElementById( 'aics-16' ) );
new AnyImageComparisonSlider( document.getElementById( 'aics-17' ) );
                        
jQuery

$( '#aics-9' ).anyImageComparisonSlider();
$( '#aics-10' ).anyImageComparisonSlider();
$( '#aics-11' ).anyImageComparisonSlider();
$( '#aics-12' ).anyImageComparisonSlider();
$( '#aics-13' ).anyImageComparisonSlider();
$( '#aics-14' ).anyImageComparisonSlider();
$( '#aics-15' ).anyImageComparisonSlider();
$( '#aics-16' ).anyImageComparisonSlider();
$( '#aics-17' ).anyImageComparisonSlider();
                        
Example 4

In this example we see again two sliders that control each other. The special feature here is that the control is reversed.

As in the previous examples we have to make sure that the sliders are in the same group and can control each other. In this particular case we also want to achieve that the control is reversed by the other slider:


data-group="g6">
                        

group: 'g6'
                        

data-control-others="true" 
data-controlled-by-others="true"
data-controlled-reverse="true"
                        

controlOthers: true
controlledByOthers: true
controlledReverse: true
                        


Also in this example we want all sliders from this group to be synchronized:


data-group-sync="true"
                        

groupSync: true
                        


So much for the special features. Here's the complete code:

HTML

<div id="aics-18" data-control-others="true" data-controlled-by-others="true" data-controlled-reverse="true" data-group="g6" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
<div id="aics-19" data-control-others="true" data-controlled-by-others="true" data-controlled-reverse="true" data-group="g6" data-group-sync="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
                        
JavaScript

new AnyImageComparisonSlider( document.getElementById( 'aics-18' ) );
new AnyImageComparisonSlider( document.getElementById( 'aics-19' ) );
                        
jQuery

$( '#aics-18' ).anyImageComparisonSlider();
$( '#aics-19' ).anyImageComparisonSlider();
                        
Example 5

In this example we again add the ui element in the slider markup which allows us to add custom controls to the slider.
And furthermore, we give the slider a different style and hide the mouse cursor.

dragger

Let us first take a closer look at the ui element in the slider markup.
In the div with the class ui we can add an img element with the class dragger. If this element is present, the slider automatically places it in the middle of the separator. At this point you can use any image you like. Don't forget to specify the width and height of your image:


<div class="ui">
    <img class="dragger" src="./path/dragger-h.png" alt="dragger" width="84" height="48">
</div>
                        


For styling the dividing line and hiding the mouse cursor you have two options.
We do this either by the data attributes data-cursor and data-dividing-line:


data-cursor="none" 
data-dividing-line="solid 3px rgba(0, 0, 0, 1.0)"
                        


Or we can do it using the settings object and the properties cursor and dividingLine:


cursor: 'none'
dividingLine: 'solid 3px rgba(0, 0, 0, 1.0)'
                        


And here is the complete code for this example:

HTML

<div id="aics-20" data-cursor="none" data-dividing-line="solid 3px rgba(0, 0, 0, 1.0)">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
    <div class="ui">
        <img class="dragger" src="./path/dragger-h.png" alt="dragger" width="84" height="48">
    </div>
</div>
                        
JavaScript

new AnyImageComparisonSlider( document.getElementById( 'aics-20' ) );
                        
jQuery

$( '#aics-20' ).anyImageComparisonSlider();
                        
Example 6

This example is based on the previous example and is intended to show further possibilities for customizing the slider.
To do so, we change the orientation of the slider from horizontal to vertical, insert a different mouse cursor and use CSS to make our dragger element visible outside the slider.

dragger

Because this slider has a vertical orientation we need a different image for our dragger element at this point:


<div class="ui">
    <img class="dragger" src="./path/dragger-v.png" alt="dragger" width="48" height="84">
</div>
                        


We change the orientation and the mouse cursor of the slider as follows.
We do this either by the data attributes data-orientation and data-cursor:


data-orientation="vertical"
data-cursor="row-resize" 
                        


Or we can do it using the settings object and the properties orientation and cursor:


orientation: 'vertical'
cursor: 'row-resize'
                        


To make our dragger element visible outside of our slider we have to add a small CSS.
We can either apply our style globally to all sliders on the page, or, as in this case, apply the style only to the corresponding slider:


div[data-slider-type|=any-image-comparison-slider] {
    overflow: visible !important;
}
                        

#aics-21 {
    overflow: visible !important;
}
                        


And here is the complete code for this example:

HTML

<div id="aics-21" data-orientation="vertical" data-cursor="row-resize" data-dividing-line="solid 3px rgba(0, 0, 0, 1.0)">
    <div class="images">
        <div class="image-btm" data-src="./path/image-btm.jpg"></div>
        <div class="image-top" data-src="./path/image-top.jpg"></div>
    </div>
    <div class="ui">
        <img class="dragger" src="./path/dragger-v.png" alt="dragger" width="48" height="84">
    </div>
</div>
                        
CSS

#aics-21 {
    overflow: visible !important;
}
                        
JavaScript

new AnyImageComparisonSlider( document.getElementById( 'aics-21' ) );
                        
jQuery

$( '#aics-21' ).anyImageComparisonSlider();
                        
Example 7

In this example we have our standard slider with a slightly different dividing line and a custom cursor for the mouse.

We change the orientation and the mouse cursor of the slider as follows.
We do this either by the data attributes data-cursor and data-dividing-line:


data-cursor="url('./path/dragger-h.cur') 22 12, auto"
data-dividing-line="solid 3px rgba(0, 0, 0, 1.0)"
                        


Or we can do it using the settings object and the properties cursor and dividingLine:


cursor: 'url('./path/dragger-h.cur') 22 12, auto'
dividingLine: 'solid 3px rgba(0, 0, 0, 1.0)'
                        


Please note that only files of type CUR may be used. For converting your graphic from e.g. PNG to CUR there are many small tools available on the web for free.

And here is the complete code for this example:

HTML

<div id="aics-22" data-cursor="url('./path/dragger-h.cur') 22 12, auto" data-dividing-line="solid 3px rgba(0, 0, 0, 1.0)">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
                        
JavaScript

new AnyImageComparisonSlider( document.getElementById( 'aics-22' ) );
                        
jQuery

$( '#aics-22' ).anyImageComparisonSlider();
                        
Example 8

In this example we see a slider that only reacts when the mouse button is held down. In addition, we have a differently styled dividing line and a mouse cursor suitable for this purpose.

Let's have a look at how we make the slider only react to the mouse movement if the mouse button is pressed down.
And of course we look at the dividing line and the mouse cursor again.
We do this either by the data attributes data-on-pointer-down, data-cursor and data-dividing-line:


data-on-pointer-down="true" 
data-cursor="grab"
data-dividing-line="solid 1px rgba(255, 255, 255, 0.5)"
                        


Or we can do it using the settings object and the properties onPointerDown, cursor and dividingLine:


onPointerDown: true
cursor: 'grab'
dividingLine: 'solid 1px rgba(255, 255, 255, 0.5)'
                        


The onPointerDown property is set to false by default. If we set it to true we make sure that the slider can only react to the mouse movement when the mouse button is pressed.

And here is the complete code for this example:

HTML

<div id="aics-23" data-on-pointer-down="true" data-cursor="grab" data-dividing-line="solid 1px rgba(255, 255, 255, 0.5)">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
                        
JavaScript

new AnyImageComparisonSlider( document.getElementById( 'aics-23' ) );
                        
jQuery

$( '#aics-23' ).anyImageComparisonSlider();
                        
Example 9

In this example we see a slider without automatic animation of the dividing line. The slider only reacts when the user interacts with it. In addition, we have again a differently styled dividing line and a suitable mouse cursor.

Let's look at how to disable the automatic animation of the dividing line.
And of course, let's have a look at the divider and the mouse cursor again.
We do this either by the data attributes data-cursor, data-dividing-line and data-auto-animation:


data-cursor="ew-resize"
data-dividing-line="solid 3px rgba(255, 255, 255, 0.5)"
data-auto-animation="false"
                        


Or we can do it using the settings object and the properties cursor, dividingLine and autoAnimation:


cursor: 'ew-resize'
dividingLine: 'solid 3px rgba(255, 255, 255, 0.5)'
autoAnimation: false
                        


The autoAnimation property is activated by default. If we deactivate the property with false, as in this example, we forbid the slider to animate the dividing line automatically by itself.

And here is the complete code for this example:

HTML

<div id="aics-24" data-cursor="grab" data-dividing-line="solid 3px rgba(255, 255, 255, 0.5)" data-auto-animation="false">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
                        
JavaScript

new AnyImageComparisonSlider( document.getElementById( 'aics-24' ) );
                        
jQuery

$( '#aics-24' ).anyImageComparisonSlider();
                        
Example 10

In this example we see a slider that the user cannot interact with. It only runs automatically and cannot be operated. In addition to this change, this slider has a different speed than the standard slider, does not pause between direction changes and applies a different easing to the animation.

Let's look at how we can deactivate the interaction with the slider and change speed, deactivate pause between direction changes and apply a different easing to the animation.
We do this either by the data attributes data-interactive, data-auto-animation-speed, data-auto-animation-pause and data-auto-animation-easing:


data-interactive="false"
data-auto-animation-speed="6"
data-auto-animation-pause="0"
data-auto-animation-easing="inOutSine"
                        


Or we can do it using the settings object and the properties interactive, autoAnimationSpeed, autoAnimationPause and autoAnimationEasing:


interactive: false
autoAnimationSpeed: 6
autoAnimationPause: 0
autoAnimationEasing: 'inOutSine'
                        


And here is the complete code for this example:

HTML

<div id="aics-25" data-interactive="false" data-auto-animation-speed="6" data-auto-animation-pause="0" data-auto-animation-easing="inOutSine">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
                        
JavaScript

new AnyImageComparisonSlider( document.getElementById( 'aics-25' ) );
                        
jQuery

$( '#aics-25' ).anyImageComparisonSlider();
                        
Example 11

In this example we take a closer look at the two buttons in the slider with which the user can easily download the images used in the slider. With a few lines of CSS code we can reposition the buttons and style them differently.

As already known from one of the previous examples we set the two buttons within the ui element:


<div class="ui">
    <a class="button-rgt" alt="full size image of 2020" target="_blank">2020</a>
    <a class="button-lft" alt="full size image of 2019" target="_blank">2019</a>
</div>
                        


To reposition and style the buttons we just added we need a few extra lines of CSS. To get the button elements we need the id of the slider element, the class name of the ui element and the two class names of the buttons. With this data we can then style the elements as usual. Please note that as in this example it might be necessary to apply the important rule to the styles:


#aics-26 .ui .button-rgt {
    bottom: 0px !important;
    border-radius: 4px 0 0 0 !important;
}
#aics-26 .ui .button-lft {
    bottom: 0px !important;
    border-radius: 0 4px 0 0 !important;
}
                        


There is nothing more to consider in this example.

And here the complete code:

HTML

<div id="aics-26">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
    <div class="ui">
        <a class="button-rgt" alt="full size image of 2020" target="_blank">2020</a>
        <a class="button-lft" alt="full size image of 2019" target="_blank">2019</a>
    </div>
</div>
                        
CSS

#aics-26 .ui .button-rgt {
    bottom: 0px !important;
    border-radius: 4px 0 0 0 !important;
}
#aics-26 .ui .button-lft {
    bottom: 0px !important;
    border-radius: 0 4px 0 0 !important;
}
                        
JavaScript

new AnyImageComparisonSlider( document.getElementById( 'aics-26' ) );
                        
jQuery

$( '#aics-26' ).anyImageComparisonSlider();
                        
Example 12

In this example we have a slider that can only be operated with an external input slider element.

First you should try to remote control the slider using the input slider above.
The slider shown here is a standard slider with the small exception that we set its start position to the middle, i.e. 50%, and allow the slider to be remotely controlled by other elements. And of course we forbid our slider to interact directly with the user and prevent automatic animation.
We do this either by the data attributes data-initial-position, data-interactive, data-auto-animation and data-controlled-by-others:


data-initial-position="0.5"
data-interactive="false"
data-auto-animation="false"
data-controlled-by-others="true"
                        


Or we can do it using the settings object and the properties initialPosition, interactive, autoAnimation and controlledByOthers:


initialPosition: 0.5
interactive: false
autoAnimation: false
controlledByOthers: true
                        


That's it with the configurations for our slider.

Now let's have a look at the input slider element with which we want to remote control our slider.:


<input type="range" min="0" max="100" value="50" id="range-slider">
                        


With the input slider element you should make sure that you always set 0 as min. At least if you want the control to start at 0%.
For max you can set a value. Either 1, 100 or 1000, it depends on how smooth and precise the control should be. The higher the value, the more precise the control.
And if you want your input slider element to be initialized in the middle, i.e. at 50%, then enter the value 50, if you have previously entered 100 as max. If you have set max to 1000 before you have to set value to 500 to stay in the middle. You know what I mean.

Now we have to script a little bit so that the input slider element can control our slider.
First, let's have a look at the whole thing in pure JavaScript:


var anyImageComparisonSlider27 = new AnyImageComparisonSlider( document.getElementById( 'any-image-comparison-slider-27' ) );

var rangeSlider = document.getElementById( 'range-slider' );
                
rangeSlider.addEventListener( 'input', function( e ) {

    anyImageComparisonSlider27.controlByExternalSource( true, e.target.value, e.target.max );

} );

rangeSlider.addEventListener( 'change', function( e ) {

    anyImageComparisonSlider27.controlByExternalSource( false );

} );
                        


First we initialize the slider and store the instance in a variable. Then we get the input slider element with which we want to control the slider and save this instance in a variable.
To control the slider during interaction with the input slider element we add an EventListener for the input event.
The first parameter must be true at this point to ensure that an interaction is taking place. The second parameter (e.target.value) passes the current value of the input slider element to the method controlByExternalSource. And the last parameter (e.target.max) passes the maximum value assigned in the HTML code (max="100") for the input slider element.
To tell the slider that the interaction is finished we add another EventListener for the change event and pass false as the only parameter.

And here the whole thing implemented with jQuery:


$( '#any-image-comparison-slider-27' ).anyImageComparisonSlider();

$( '#range-slider' ).on( 'input', function( e ) {

    $( '#any-image-comparison-slider-27' ).anyImageComparisonSlider( 'controlByExternalSource', true, e.target.value, e.target.max );

} );

$( '#range-slider' ).on( 'change', function( e ) {

    $( '#any-image-comparison-slider-27' ).anyImageComparisonSlider( 'controlByExternalSource', false );

} );
                        


In principle it works the same way as in the example with pure JavaScript. Only the syntax is of course typical for jQuery a bit different.

Done. Now let's look at the complete code again:

HTML

<div id="aics-27 data-initial-position="0.5" data-interactive="false" data-auto-animation="false" data-controlled-by-others="true">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
</div>
                        

<input type="range" min="0" max="100" value="50" id="range-slider">
                        
JavaScript

var anyImageComparisonSlider27 = new AnyImageComparisonSlider( document.getElementById( 'any-image-comparison-slider-27' ) );

var rangeSlider = document.getElementById( 'range-slider' );
                
rangeSlider.addEventListener( 'input', function( e ) {

    anyImageComparisonSlider27.controlByExternalSource( true, e.target.value, e.target.max );

} );

rangeSlider.addEventListener( 'change', function( e ) {

    anyImageComparisonSlider27.controlByExternalSource( false );

} );
                        
jQuery

$( '#any-image-comparison-slider-27' ).anyImageComparisonSlider();

$( '#range-slider' ).on( 'input', function( e ) {

    $( '#any-image-comparison-slider-27' ).anyImageComparisonSlider( 'controlByExternalSource', true, e.target.value, e.target.max );

} );

$( '#range-slider' ).on( 'change', function( e ) {

    $( '#any-image-comparison-slider-27' ).anyImageComparisonSlider( 'controlByExternalSource', false );

} );
                        
Example 13

Here we see a standard slider where we use JavaScript/jQuery to display the current position of the dividing line in percent.

Slider Position: 0%

How do we achieve this?
In both following code examples we initialize the slider first. After that we set an interval that accesses the method getPos from the slider every 100ms. We modify the position value we received a bit for a nicer presentation and then output it in a text field.
First, let's have a look at the whole thing in pure JavaScript:


var anyImageComparisonSlider28 = new AnyImageComparisonSlider( document.getElementById( 'any-image-comparison-slider-28' ) );

setInterval( function() { 

    document.getElementById( 'slider-position-output' ).innerHTML = 'Slider Position: ' + ( Math.round( parseFloat( anyImageComparisonSlider28.getPos() ) * 100 ) ).toString() + '%';

}, 100 );
                        


Same same in jQuery:


$( '#any-image-comparison-slider-28' ).anyImageComparisonSlider();

setInterval( function() { 

    $( '#any-image-comparison-slider-28' ).anyImageComparisonSlider( 'getPos', function( v ) { $( '#slider-position-output' ).text( 'Slider Position: ' + ( Math.round( parseFloat( v ) * 100 ) ).toString() + '%' ) } ); 

}, 100 );
                        


Because this is a standard slider already known to us and we have processed the JavaScript/jQuery part completely, we are done and do not need any further code summary for this example.

Example 14

In this example let's add a preloader animation to the slider.

Let's start with the HTML part. We take our default slider and insert the already known div with the class ui. In this ui div you can insert any animation. As you like. In this example I decided to use the roller animation of Loading.io. There you get the necessary code for the animation which you can insert directly into the ui div and into your CSS with a few small adjustments:

Please note: Microsoft Internet Explorer version earlier than 10 don't support CSS Animation. Therefore this example does not work on all browsers.
But in case you need to support older browsers, you can simply use an animated gif instead of a CSS loading animation. This way backwards compatibility is not a thing.


<div id="aics-29">
    <div class="images">
        <div class="image-rgt" data-src="./path/image-rgt.jpg"></div>
        <div class="image-lft" data-src="./path/image-lft.jpg"></div>
    </div>
    <div class="ui">
        <div id="lds-wrapper"><div class="lds-roller"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div></div>
    </div>
</div>
                        


This is the part you copy and paste from Loading.io. To make sure the animation is centered in the slider, we put its markup in a small wrapper div with the id lds-wrapper:


<div id="lds-wrapper"><div class="lds-roller"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div></div>
                        


This is how the CSS for the lds-wrapper div looks like:


#lds-wrapper {
    position: absolute;
    top: 50%;
    left: 50%;
    margin-left: -40px;
    margin-top: -40px;
}
                        


And here the complete CSS including our wrapper to center the animation and the copied code from Loading.io:


#lds-wrapper {
    position: absolute;
    top: 50%;
    left: 50%;
    margin-left: -40px;
    margin-top: -40px;
}
.lds-roller {
    display: inline-block;
    position: relative;
    width: 80px;
    height: 80px;
}
.lds-roller div {
    animation: lds-roller 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
    transform-origin: 40px 40px;
}
.lds-roller div:after {
    content: " ";
    display: block;
    position: absolute;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #fff;
    margin: -4px 0 0 -4px;
}
.lds-roller div:nth-child(1) {
    animation-delay: -0.036s;
}
.lds-roller div:nth-child(1):after {
    top: 63px;
    left: 63px;
}
.lds-roller div:nth-child(2) {
    animation-delay: -0.072s;
}
.lds-roller div:nth-child(2):after {
    top: 68px;
    left: 56px;
}
.lds-roller div:nth-child(3) {
    animation-delay: -0.108s;
}
.lds-roller div:nth-child(3):after {
    top: 71px;
    left: 48px;
}
.lds-roller div:nth-child(4) {
    animation-delay: -0.144s;
}
.lds-roller div:nth-child(4):after {
    top: 72px;
    left: 40px;
}
.lds-roller div:nth-child(5) {
    animation-delay: -0.18s;
}
.lds-roller div:nth-child(5):after {
    top: 71px;
    left: 32px;
}
.lds-roller div:nth-child(6) {
    animation-delay: -0.216s;
}
.lds-roller div:nth-child(6):after {
    top: 68px;
    left: 24px;
}
.lds-roller div:nth-child(7) {
    animation-delay: -0.252s;
}
.lds-roller div:nth-child(7):after {
    top: 63px;
    left: 17px;
}
.lds-roller div:nth-child(8) {
    animation-delay: -0.288s;
}
.lds-roller div:nth-child(8):after {
    top: 56px;
    left: 12px;
}
@keyframes lds-roller {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}
                        


Now that we have implemented the preloader animation we want to know how to remove this element when the slider is fully loaded and initialized.

Let's start with the JavaScript solution as usual:


var loadingDone29 = function() {

    setTimeout( function() { 
        
        document.getElementById( 'lds-wrapper' ).style.display = 'none';
    
    }, 3000 );

}

var anyImageComparisonSlider29 = new AnyImageComparisonSlider( document.getElementById( 'any-image-comparison-slider-29' ), { onReady: loadingDone29 } );
                        


Same same in jQuery:


var loadingDone29 = function() {

    setTimeout( function() { 
        
        $( '#lds-wrapper').hide();
    
    }, 3000 );

}

$( '#any-image-comparison-slider-29' ).anyImageComparisonSlider( { onReady: loadingDone29 } );
                        


First we write a small callback function that the slider can call when it is loaded. Within this callback function we set a timeout that removes the animation after 3000ms, i.e. 3 seconds.
The timeout is of course only an example and is used to show you the animation at all. Normally I would omit the timeout so that you can remove the loading animation immediately after successful loading.

After that you initialize the slider as usual and give it the settings object with the callback function.

Because this is a standard slider already known to us and we have processed the JavaScript/jQuery part completely, we are done and do not need any further code summary for this example.




Yeah. You made it. Congrats!

If you are reading this you have come a long way and should now be an expert in using the any Image Comparison Slider.

Thanks for your time.
I wish you all the best and much success with your project.

Please tell your friends and colleagues about the any Image Comparison Slider and recommend it to others.

If you have any questions or suggestions, found a bug, or just want to share how you implemented the slider on your site, feel free to contact me.

So long, cheers.