Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="page-container">
    <h1>Responsive jQuery Before & After Image Comparison</h1>
    <h2>Hurricane Harvey 2017</h2>

    <div class="js-img-compare">
        <div style="display: none;">
            <span class="images-compare-label">Before</span>
            <img src="https://keystonewebstudios.com/wp-content/uploads/2017/09/before.jpg" alt="Before">
        </div>
        <div>
            <span class="images-compare-label">After</span>
            <img src="https://keystonewebstudios.com/wp-content/uploads/2017/09/after.jpg" alt="After">
        </div>
    </div>
    <div>
        <button class="btn js-front-btn">Show Before</button>
        <button class="btn js-back-btn">Show After</button>
        <button class="btn js-toggle-btn">Toggle</button>
    </div>

    <footer>
        Example By <a href="https://keystonewebstudios.com">Keystone Web Studios</a>
    </footer>

</div>


              
            
!

CSS

              
                /* Images compare main container */
.images-compare-container {
    display: inline-block;
    position: relative;
    overflow: hidden;
}

/* Images compare front element */
.images-compare-before {
    will-change: clip;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
    pointer-events: none;
    overflow: hidden;
}

/* Images compare back element */
.images-compare-after {
    pointer-events: none;
}

/* Responsive image settings */
.images-compare-before img, .images-compare-after img {
    max-width: 100%;
    height: auto;
    display: block;
}

/* Separator (thin vertical blank line) */
.images-compare-separator {
    position: absolute;
    background: white;
    height: 100%;
    width: 1px;
    z-index: 4;
    left: 0;
    top: 0;
}

/* Drag handle (circle) */
.images-compare-handle {
    height: 38px;
    width: 38px;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -22px;
    margin-top: -22px;
    border: 3px solid white;
    -webkit-border-radius: 1000px;
    -moz-border-radius: 1000px;
    border-radius: 1000px;
    -webkit-box-shadow: 0 0 12px rgba(51, 51, 51, 0.5);
    -moz-box-shadow: 0 0 12px rgba(51, 51, 51, 0.5);
    box-shadow: 0 0 12px rgba(51, 51, 51, 0.5);
    z-index: 3;
    background: rgb(0, 0, 0);
    background: rgba(0, 0, 0, 0.7);
    cursor: pointer;
}

/* Drag handle arrows */
.images-compare-left-arrow, .images-compare-right-arrow {
    width: 0;
    height: 0;
    border: 6px inset transparent;
    position: absolute;
    top: 50%;
    margin-top: -6px;
}

.images-compare-left-arrow {
    border-right: 6px solid white;
    left: 50%;
    margin-left: -17px;
}

.images-compare-right-arrow {
    border-left: 6px solid white;
    right: 50%;
    margin-right: -17px;
}

/* Label */
.images-compare-label {
    font-family: sans-serif;
    text-transform: uppercase;
    font-weight: bold;
    position: absolute;
    top: 10px;
    left: 10px;
    z-index: 1;
    color: rgb(0, 0, 0);
    color: rgba(0, 0, 0, 0.4);
    background: rgb(255, 255, 255);
    background: rgba(255, 255, 255, 0.7);
    padding: 10px;
    border-radius: 5px;
    pointer-events: none;
    display: none;
}

.images-compare-container .images-compare-label {
    display: inherit;
}

.images-compare-before .images-compare-label {
    left: 10px;
}

.images-compare-after .images-compare-label {
    left: auto;
    right: 10px;
}

body {
    font-family: "Helvetica Neue Light", "HelveticaNeue-Light", "Helvetica Neue", Calibri, Helvetica, Arial, sans-serif;
    background: darkgrey;
}

.page-container {
    max-width: 1024px;
    margin: 0 auto;
    text-align: center;
    border-left: 1px solid #EFEFEF;
    border-right: 1px solid #EFEFEF;
    padding: 15px;
    background: white;
}

h2 {
    margin-bottom: 15px;
}

button.btn {
    -webkit-box-sizing: content-box;
    -moz-box-sizing: content-box;
    box-sizing: content-box;
    background: none;
    color: cornflowerblue;
    /* cursor: default; */
    font: inherit;
    line-height: normal;
    overflow: visible;
    -webkit-appearance: button; /* for input */
    -webkit-user-select: none; /* for button */
    -moz-user-select: none;
    -ms-user-select: none;
    padding: 10px;
    border: 1px solid cornflowerblue;
    cursor: pointer;
    margin-top: 10px;
}

button.btn:hover {
    background: cornflowerblue;
    color: white;
}

button.btn::-moz-focus-inner {
    border: 0;
    padding: 0;
}

footer {
    padding: 30px 15px 15px 15px;
}

a {
    color: cornflowerblue;
}
              
            
!

JS

              
                
    $(function () {
        var imagesCompareElement = $('.js-img-compare').imagesCompare();
        var imagesCompare = imagesCompareElement.data('imagesCompare');
        var events = imagesCompare.events();

        imagesCompare.on(events.changed, function (event) {
            console.log(events.changed);
            console.log(event.ratio);
            if (event.ratio < 0.4) {
                console.log('We see more than half of the back image');
            }
            if (event.ratio > 0.6) {
                console.log('We see more than half of the front image');
            }

            if (event.ratio <= 0) {
                console.log('We see completely back image');
            }

            if (event.ratio >= 1) {
                console.log('We see completely front image');
            }
        });

        $('.js-front-btn').on('click', function (event) {
            event.preventDefault();
            imagesCompare.setValue(1, true);
        });

        $('.js-back-btn').on('click', function (event) {
            event.preventDefault();
            imagesCompare.setValue(0, true);
        });

        $('.js-toggle-btn').on('click', function (event) {
            event.preventDefault();
            if (imagesCompare.getValue() >= 0 && imagesCompare.getValue() < 1) {
                imagesCompare.setValue(1, true);
            } else {
                imagesCompare.setValue(0, true);
            }
        });
    });

              
            
!
999px

Console