LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » Before After Image Slider Using Mouse Drag In Pure Vanilla JavaScript

Before After Image Slider Using Mouse Drag In Pure Vanilla JavaScript

Before-After-Image-Slider-Using-Mouse-Drag-In-Pure-Vanilla-JavaScript
This Vanilla JavaScript code snippet helps you to create before and after image comparison sliders. It comes with scrolling and touch support to slide the vertical slider over the image to see the before and after differences.

There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.

Features:

  1. Light Weight.
  2. Pure JavaScript.
  3. Cross Browser.
  4. No External Files.
  5. Fully Customizable.
  6. Responsive.

Simple-WYSIWYG-Rich-Text-Editor-Using-Pure-JavaScript-DEMO

How To Add Simple WYSIWYG Rich Text Editor Using Pure JavaScript?

Here is the complete code to recognize speech using JavaScript code. There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

HTML:

<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css'>
<div id="page">
<div class="wrapper">
<div class="before">
<img class="content-image" src="https://farm2.staticflickr.com/1638/26145024230_06acd55d1b_b.jpg" draggable="false"/> </div>
<div class="after">
<img class="content-image" src="https://farm2.staticflickr.com/1663/25814974803_d4c55ff708_b.jpg" draggable="false"/>
</div>
<div class="scroller">
<svg class="scroller__thumb" xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><polygon points="0 50 37 68 37 32 0 50" style="fill:#fff"/><polygon points="100 50 64 32 64 68 100 50" style="fill:#fff"/></svg>
</div>
</div>
</div>

CSS:

<style type="text/css">
/* Our normalize css */
*{
margin:0;
box-sizing: border-box;
}

/* Our wrapper */
.wrapper{
width: 900px;
height: 600px;
position: relative;

overflow:hidden;
box-shadow: 0 10px 20px rgba(0,0,0,0.19), 0 6px 6px rgba(0,0,0,0.23);
}

/* Our image information */
.before,
.after {
width:100%;
height:100%;
background-repeat:no-repeat;
background-color: white;
background-size: cover;
background-position: center;
position: absolute;
top:0;
left:0;
pointer-events:none;
overflow: hidden;
}

.content-image{
height:100%;
}

.after{
width:125px;
}

.scroller{
width: 50px;
height:50px;
position: absolute;
left:100px;
top:50%;
transform:translateY(-50%);
border-radius:50%;
background-color: transparent;
opacity:0.9;
pointer-events:auto;
cursor: pointer;
}

.scroller:hover{
opacity:1;
}

.scrolling{
pointer-events:none;
opacity:1;
// z-index: 1;
}

.scroller__thumb{
width:100%;
height:100%;
padding:5px;
}

.scroller:before,
.scroller:after{
content:" ";
display: block;
width: 7px;
height: 9999px;
position: absolute;
left: 50%;
margin-left: -3.5px;
z-index: 30;
transition:0.1s;
}
.scroller:before{
top:100%;
}
.scroller:after{
bottom:100%;
}

/* If you want to cahnge the colors, make sure you change the fill in the svgs to match */
.scroller{
border: 5px solid #fff;
}
.scroller:before,
.scroller:after{
background: #fff;
}
</style>

CSS:

<script type="text/javascript">
// I hope this over-commenting helps. Let's do this!
// Let's use the 'active' variable to let us know when we're using it
let active = false;

// First we'll have to set up our event listeners
// We want to watch for clicks on our scroller
document.querySelector('.scroller').addEventListener('mousedown',function(){
active = true;
// Add our scrolling class so the scroller has full opacity while active
document.querySelector('.scroller').classList.add('scrolling');
});
// We also want to watch the body for changes to the state,
// like moving around and releasing the click
// so let's set up our event listeners
document.body.addEventListener('mouseup',function(){
active = false;
document.querySelector('.scroller').classList.remove('scrolling');
});
document.body.addEventListener('mouseleave',function(){
active = false;
document.querySelector('.scroller').classList.remove('scrolling');
});

// Let's figure out where their mouse is at
document.body.addEventListener('mousemove',function(e){
if (!active) return;
// Their mouse is here...
let x = e.pageX;
// but we want it relative to our wrapper
x -= document.querySelector('.wrapper').getBoundingClientRect().left;
// Okay let's change our state
scrollIt(x);
});

// Let's use this function
function scrollIt(x){
let transform = Math.max(0,(Math.min(x,document.querySelector('.wrapper').offsetWidth)));
document.querySelector('.after').style.width = transform+"px";
document.querySelector('.scroller').style.left = transform-25+"px";
}

// Let's set our opening state based off the width,
// we want to show a bit of both images so the user can see what's going on
scrollIt(150);

// And finally let's repeat the process for touch events
// first our middle scroller...
document.querySelector('.scroller').addEventListener('touchstart',function(){
active = true;
document.querySelector('.scroller').classList.add('scrolling');
});
document.body.addEventListener('touchend',function(){
active = false;
document.querySelector('.scroller').classList.remove('scrolling');
});
document.body.addEventListener('touchcancel',function(){
active = false;
document.querySelector('.scroller').classList.remove('scrolling');
});
</script>

When we execute the above code in the browser, it shows up like this in the browser. It allows you to access your microphone to speak something.

Recommended For You:
Pure Vanilla JavaScript Responsive Image Slider With Text Over It

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *