Ways to restrict the resizing direction of a container using mouse gestures

I am currently dealing with a container that has the ability to resize from the right side and the bottom.

The resizing is functioning correctly, but it is adjusting both height and width simultaneously in unlimited length. Is there a way to limit the direction of resizing to either increase or decrease only on the bottom or right side?

It seems that single side increase is only possible when using one condition in an if statement.

Below is the provided code snippet:

const panel = document.getElementById("styleChangeOuterTag");
let m_pos;
let m_pos1;

function resize(e) {
  const dx = m_pos - e.x;
  const dy = m_pos1 - e.y;
  m_pos = e.x;
  m_pos1 = e.y;
  panel.style.width = (parseInt(getComputedStyle(panel).width) - dx) + "px";
  panel.style.height = (parseInt(getComputedStyle(panel).height) - dy) + "px";
}

panel.addEventListener("mousedown", function(e) {
  if (e.offsetX > panel.clientWidth - 10) {
    m_pos = e.x;
    document.addEventListener("mousemove", resize);
  } else if (e.offsetY > panel.clientHeight - 10) {
    m_pos1 = e.y;
    document.addEventListener("mousemove", resize);
  }
});

document.addEventListener("mouseup", function() {
  document.removeEventListener("mousemove", resize);
});

I'm struggling with setting the correct size for the clickable width (grey part in Container-which triggers resize) as it's not quite right and doesn't cover the entire width. It was implemented using e.offsetX > panel.clientWidth - 10. Any advice on this issue would be greatly appreciated.

Thank you in advance for your help!

Answer №1

If you want to make your resize function more robust, consider adding an additional condition like this:

const minWidth = 150
const maxWidth = 500
const minHeight = 150
const maxHeight = 500

function resize(e) {
  const dx = m_pos - e.x;
  const dy = m_pos1 - e.y;
  m_pos = e.x;
  m_pos1 = e.y;

  const newWidth = parseInt(getComputedStyle(panel).width) - dx
  const newHeight = parseInt(getComputedStyle(panel).height - dy

  if (newWidth > minWidth && newWidth < maxWidth) {
    panel.style.width = newWidth + "px";
  }
  if (newHeight > minHeight && newHeight < maxHeight) {
    panel.style.height = newHeight + "px";
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display an error message in the input type file Form Control if the file format is not .doc or .docx

I need a way to display an alert if the user tries to submit a file that is not of type doc or docx. I've implemented a validator for this purpose and would like the alert message (Unacceptable file type) to be shown when the validation fails. Here i ...

Using ASP.Net to Enhance Kendo DatePicker Selection

Is there a way to retrieve the selected date as a string and assign it to a variable using jQuery? <div class="col-md-3"> @Html.Kendo().DatePicker().Name("SearchDate").Value("").HtmlAttributes(new { style = "width:150px", @class = "", @placehol ...

In JavaScript with Node.js, how can one verify a file's size and only download the initial kilobyte of the file?

When using Javascript/Node JS to download a file, you can check the file size and download only the first kilobyte of the file. This is useful if you want to hash the first kb and compare it with the hash of the complete file. If the hashes match and the ...

How can I pass the current value of an HTML.DropDownListFor to an ActionLink?

Is it feasible to transfer the current value of @Html.DropDownListFor to an action link? I am trying to send the template value to the Sample controller using the Create action. The code below is not functioning because @Model.SurveyTemplate does not retur ...

Activate the popup for sharing or bookmarking by clicking on a regular link

I am currently utilizing a Share/Bookmark script: <div class="singles-right"> <a href="#" class="bookmark"></a> <script type="text/javascript" src="http://static.addinto.com/ai/ai2_bkmk.js"></script> <a href="#" clas ...

The organization of JavaScript function declarations

Just a quick query as I seem to be encountering some peculiar bugs and can't seem to find any information on this issue. Does the order in which functions are defined within a file make a difference? Take for instance: function c() { d(); //define ...

Tips for adjusting text color in a paragraph based on its content

I have a paragraph or h1 with phrases like "the color of this item is Magenta" or "this output is Red". These color-specific words (red, black, cyan, magenta or yellow) may appear within the paragraph or h1 tag. Here is my current code snippet: HTML < ...

Tips for handling the "DEPRECATED use https://github.com/pillarjs/path-to-regexp" notification

While some more experienced programmers may consider this a basic issue, I am struggling to resolve it. The warning clearly indicates the use of a deprecated library that needs to be upgraded. This message seems to be related to the routing functionality a ...

Is it possible for me to input text into html while the page is loading?

Is there a way to preload external files while a web page is loading? I'm looking to incorporate a JavaScript templating engine into my website and prefer to store my templates in separate files. Instead of loading these templates asynchronously, I&ap ...

jQuery.appear is seemingly malfunctioning

Does anyone know why this specific HTML code isn't functioning correctly? The intention is for an alert to appear when the image becomes visible on the screen and then again when it disappears from view. Even when utilizing local libraries, this scr ...

Is it possible to decrease the size of a div by scrolling both vertically and horizontally?

Can a div's height and width both be reduced at the same time while scrolling down a page? Let's say that as the user scrolls, the size of the div changes from 400px by 400px to 200px by 200px, all while remaining centered on the page. I've ...

Customize checkbox and label using jQuery

I have a scenario where I have multiple checkboxes and corresponding labels. When the answer is correct, I want to change the background color of the selected checkbox and label. <input type="checkbox" id="a" class="check-with-label" /> <label fo ...

AngularJS / JQuery - input field with no corresponding label

I am encountering an issue with "Form input without an associated label". This problem is occurring on [textarea], [select], and [input] elements. Below is the code I am using: <div class="panel-body"> <form name="f" data-ng-submit="addTodo()"&g ...

A guide on how to dynamically inject CSS using Angular

I've spent the past few days testing out different solutions, advice, and tutorials for this issue, but unfortunately none of them have worked. The closest suggestion I found was from this link: However, it references "extractCss," which has been de ...

how to open a new tab using JavaScript with Selenium

My code is supposed to open a new window that goes from the login window to the main menu, module, reports, and finally the report name. The report name should be opened in the next tab. Issue: The report name is not opening in a new tab; it's openin ...

The Angular Table row mysteriously vanishes once it has been edited

Utilizing ng-repeat within a table to dynamically generate content brings about the option to interact with and manage the table contents such as edit and delete. A challenge arises when editing and saving a row causes it to disappear. Attempts were made ...

The dialog box in CSS is extending too far down past the bottom of the screen, making it impossible to scroll and click on the buttons located

I am currently working on creating a dialog box with a text area using material UI. Unfortunately, when I input a significant amount of text, the dialog box ends up extending beyond the screen, making it impossible to scroll down to access the buttons. &l ...

I am unable to retrieve information from Firebase in Vue

Currently, I am following a tutorial on YouTube to enhance my knowledge about storing data in Firebase and then utilizing it in a Vue application. (You can watch the tutorial at this link: https://www.youtube.com/watch?v=Htt8AKeF1Kw, you will encounter the ...

Adjust styling of web elements according to device screen dimensions

On my page "create.php," everything looks great on desktop computers with normal width and height: https://i.sstatic.net/XJBwA.png However, when I resize the page to its smallest width: https://i.sstatic.net/YlISM.png When viewed on a Mobile device(sams ...

Why does ajax transmit only the initial input value and not the rest of them?

I recently developed a school website where teachers can input students' marks. Currently, I have organized the project into three main files: The first file is designed to showcase every student's name based on their subject and course. <ta ...