LATEST >>

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

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / JavaScript Codes » How To Create Resizable Split DIVs Using Pure JavaScript?

How To Create Resizable Split DIVs Using Pure JavaScript?

How-To-Create-Resizable-Split-DIVs-Using-Pure-JavaScript
Resizable containers empower different users to customize a UI to emphasise what they find most important. A basic implementation of this is a split view or split pane, allowing users to enlarge content that is more relevant to them.

When you needed to add support for a resizable panel-based layout in HTML. Specifically, this is for HTML Help Builder which is an application that generates HTML-based documentation from class libraries, and databases and that also lets you create help topics manually for full documentation purposes. The generated output for documentation typically has a two-panel layout and I needed to integrate resizing functionality from the old-school frames interface that had been in use before.

There are libraries that can implement this feature for you, but creating it yourself gives you full control over the styling and functionality, so you can rapidly tailor it to your project.

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.

Table of Contents

Recommended For You:
How To Resize An Image Using Pure Vanilla JavaScript In Aspect Ratio?

Features:

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

How To Create Resizable Split DIVs Using Pure JavaScript?

There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.

CSS:

<style type="text/css">
.container {
display: flex;

/* Misc */
border: 1px solid #cbd5e0;
height: 16rem;
width: 100%;
}
.container__left {
/* Initially, the left takes 3/4 width */
width: 75%;

/* Misc */
align-items: center;
display: flex;
justify-content: center;
}
.resizer {
background-color: #cbd5e0;
cursor: ew-resize;
height: 100%;
width: 2px;
}
.container__right {
/* Take the remaining width */
flex: 1;

/* Misc */
align-items: center;
display: flex;
justify-content: center;
}
</style>

HTML:

<div class="container">
<div class="container__left">Left</div>
<div class="resizer" id="dragMe"></div>
<div class="container__right">Right</div>
</div>

JavaScript:

<script type="text/javascript">
document.addEventListener('DOMContentLoaded', function () {
// Query the element
const resizer = document.getElementById('dragMe');
const leftSide = resizer.previousElementSibling;
const rightSide = resizer.nextElementSibling;

// The current position of mouse
let x = 0;
let y = 0;
let leftWidth = 0;

// Handle the mousedown event
// that's triggered when user drags the resizer
const mouseDownHandler = function (e) {
// Get the current mouse position
x = e.clientX;
y = e.clientY;
leftWidth = leftSide.getBoundingClientRect().width;

// Attach the listeners to `document`
document.addEventListener('mousemove', mouseMoveHandler);
document.addEventListener('mouseup', mouseUpHandler);
};

const mouseMoveHandler = function (e) {
// How far the mouse has been moved
const dx = e.clientX - x;
const dy = e.clientY - y;

const newLeftWidth = ((leftWidth + dx) * 100) / resizer.parentNode.getBoundingClientRect().width;
leftSide.style.width = `${newLeftWidth}%`;

resizer.style.cursor = 'col-resize';
document.body.style.cursor = 'col-resize';

leftSide.style.userSelect = 'none';
leftSide.style.pointerEvents = 'none';

rightSide.style.userSelect = 'none';
rightSide.style.pointerEvents = 'none';
};

const mouseUpHandler = function () {
resizer.style.removeProperty('cursor');
document.body.style.removeProperty('cursor');

leftSide.style.removeProperty('user-select');
leftSide.style.removeProperty('pointer-events');

rightSide.style.removeProperty('user-select');
rightSide.style.removeProperty('pointer-events');

// Remove the handlers of `mousemove` and `mouseup`
document.removeEventListener('mousemove', mouseMoveHandler);
document.removeEventListener('mouseup', mouseUpHandler);
};

// Attach the handler
resizer.addEventListener('mousedown', mouseDownHandler);
});
</script>

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need. Remember to add JavaScript after the HTML code.

Recommended For You:
Use CSS3/HTML5 Outlines To Upgrade Your Website

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 *