Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make HTML element resizable using pure Javascript?

I was wondering how we can make a HTML element like <div> or <p> tag element resizable when clicked using pure JavaScript, not the jQuery library or any other library.

like image 908
monk Avatar asked Jan 22 '12 09:01

monk


People also ask

How do you make elements resizable?

The resize property defines if (and how) an element is resizable by the user. Note: The resize property does not apply to inline elements or to block elements where overflow="visible". So, make sure that overflow is set to "scroll", "auto", or "hidden".

How do I adjust auto size in HTML?

Using width, max-width and margin: auto; Then, you can set the margins to auto, to horizontally center the element within its container.

How do I make my text area resizable?

as per davidwalsh.name/textarea-resize - use resize:vertical or resize:horizontal to constrain resizing to one dimension. Or use any of max-width, max-height, min-width and min-height.

How do I make my Web page fit the screen in HTML?

You should set body and html to position:fixed; , and then set right: , left: , top: , and bottom: to 0; . That way, even if content overflows it will not extend past the limits of the viewport. Caveat: Using this method, if the user makes their window smaller, content will be cut off.

Is there a way to change CSS resize corner's position?

I used transform: rotate(-90deg) to move the resize corner to the upper right, then put in an inner div with transform: rotate(90deg) to make the inside content the right-way-up again.

Which resizing property is used to resize the width of the element?

The resize property in CSS is used to resize the element according to user requirement. It does not apply to inline elements or to block elements where overflow is visible.

How do you resize an object in CSS?

The scale() CSS function defines a transformation that resizes an element on the 2D plane. Because the amount of scaling is defined by a vector, it can resize the horizontal and vertical dimensions at different scales. Its result is a <transform-function> data type.


1 Answers

I really recommend using some sort of library, but you asked for it, you get it:

var p = document.querySelector('p'); // element to make resizable  p.addEventListener('click', function init() {     p.removeEventListener('click', init, false);     p.className = p.className + ' resizable';     var resizer = document.createElement('div');     resizer.className = 'resizer';     p.appendChild(resizer);     resizer.addEventListener('mousedown', initDrag, false); }, false);  var startX, startY, startWidth, startHeight;  function initDrag(e) {    startX = e.clientX;    startY = e.clientY;    startWidth = parseInt(document.defaultView.getComputedStyle(p).width, 10);    startHeight = parseInt(document.defaultView.getComputedStyle(p).height, 10);    document.documentElement.addEventListener('mousemove', doDrag, false);    document.documentElement.addEventListener('mouseup', stopDrag, false); }  function doDrag(e) {    p.style.width = (startWidth + e.clientX - startX) + 'px';    p.style.height = (startHeight + e.clientY - startY) + 'px'; }  function stopDrag(e) {     document.documentElement.removeEventListener('mousemove', doDrag, false);     document.documentElement.removeEventListener('mouseup', stopDrag, false); } 

Demo

Remember that this may not run in all browsers (tested only in Firefox, definitely not working in IE <9).

like image 198
user123444555621 Avatar answered Sep 18 '22 13:09

user123444555621