Struggling with the resizing functionality in an HTML template designed to mimic a window interface

I am currently working on a web template that resembles a window, but I am facing an issue with the resizing functionality. The resizer located in the bottom right corner functions perfectly fine, however, when attempting to create a resizer for the top right corner, it does not work as expected. Despite trying various approaches, I have been unsuccessful in resolving this problem.

When dragging the resizer at the top right of the window, the resizing behavior is erratic and unusual. You can test it out yourself by visiting .

For the codebase, you can refer to the GitHub repository https://github.com/alhazacod/windowsxphtmltemplate

Below is the JavaScript code snippet:

dragru.onmousedown = function(event) {
  let shiftLeft = event.clientX - wwindow.getBoundingClientRect().left;
  let shiftTop = event.clientY - wwindow.getBoundingClientRect().top;
  let shiftBottom = - event.clientY + wwindow.getBoundingClientRect().bottom;

  function resize(w, h, pageY){
    wwindow.style.top = pageY - shiftTop + 'px';
    if(w>200){
      wwindow.style.width = w + 'px';
    }
    if(h>200){
      console.log(wwindow.getBoundingClientRect().bottom - event.pageY);
      wwindow.style.height = (wwindow.getBoundingClientRect().bottom - event.pageY) + 'px';

    }
    //wwindow.style.top = pageY - shiftTop + 'px';
  }

  function onMouseMove(event){
    shiftLeft = event.clientX - wwindow.getBoundingClientRect().left;
    shiftBottom = -event.clientY + wwindow.getBoundingClientRect().bottom;
    //if(event.pageY < windowh && event.pageY > 1 && event.pageX < windoww && event.pageX > 1){
      resize(shiftLeft, shiftBottom, event.pageY);
    //}
    /*else{
      document.removeEventListener('mousemove', onMouseMove);
      wwindow.onmouseup = null;
    }*/
  }

  document.addEventListener('mousemove', onMouseMove);

  wwindow.onmouseup = function(){
    document.removeEventListener('mousemove', onMouseMove);
    wwindow.onmouseup = null;
  };
};

Answer №1

Make the necessary adjustment to dragru code for it to function correctly

dragru.onmousedown = function (event) {
    let bottom = wwindow.getBoundingClientRect().bottom;
    let left = wwindow.getBoundingClientRect().left;

    function resize(w, h) {
        if (w > 200) {
            wwindow.style.width = w + 'px';
        }
        if (h > 200) {
            wwindow.style.top = bottom - h + 'px';
            wwindow.style.height = h + 'px';
        }
    }

    function onMouseMove(event) {
        if (event.pageY < windowh && event.pageY > 1 && event.pageX < windoww && event.pageX > 1)
            resize(event.clientX - left, bottom - event.clientY);
        else {
            document.removeEventListener('mousemove', onMouseMove);
            wwindow.onmouseup = null;
        }
    }

    document.addEventListener('mousemove', onMouseMove);

    wwindow.onmouseup = function () {
        document.removeEventListener('mousemove', onMouseMove);
        wwindow.onmouseup = null;
    };
};

An additional snippet has been included to demonstrate its functionality, with adjustments made to the minimum height/width of the window to prevent jumping during initial resizing due to the size of the "window"

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

Coloring vertices in Three.js IcosahedronGeometry

I have been experimenting with dynamically changing the color of a selected vertex. Based on my work at https://jsfiddle.net/pmankar/m70cpxes/, I constructed an IcosahedronGeometry point cloud geometry. When a click event occurs, my goal is to alter the co ...

Is it possible to refactor this forwardRef so that it can be easily reused in a function?

Currently, I am in the process of transitioning my application to Material UI V4 and facing challenges with moving my react router Link components into forwardRef wrapped components when setting the 'to' prop programmatically. The code below doe ...

Instructions for activating and deactivating a numerical input field with a checkbox

Is there a way to create a pair of a checkbox and number field that are linked together? When the checkbox is clicked, it should disable the associated number field. Javascript File: $(document).ready(function(){ $("input[name=check]").click(function(){ ...

What is the best way to include and transmit multiple records in ASP.NET MVC with the help of jQuery?

Having trouble sending multiple records to the database using JavaScript in ASP.NET MVC? Look no further, as I have the best code that can send data to the controller. However, the file attachment is not being sent along with it. I've tried various m ...

When a DOM object is passed in JavaScript, its value becomes indeterminate

I am currently working on creating a table that can change its background color based on an RGB value. Each row in this table contains clickable <td> cells that contribute to the RGB value. For example, the first row might be +/- 123, the second row ...

Is the "require" function absent in Firebase?

I'm encountering an issue while trying to execute an HTML file in the Firebase staging environment. I have included the Firebase npm package in my JavaScript code, but when it runs in the browser, I get an error stating "require is not defined". HTML ...

The issue of checkboxes not being sorted properly in Slick Grid

Currently, I am working on resolving a sorting issue within one of our applications using Slick Grid for grid implementation. The existing sort function, although functional, presents a challenge. While it successfully sorts columns, the checkbox associate ...

Struggling to make Tumblr Javascript function properly

I have integrated the following code snippet: <script type="text/javascript" src="http://underlineent.tumblr.com/api/read/json"> </script> Unfortunately, my Tumblr blog is not appearing on this site. Can anyone provide assistance? The provid ...

Fetching a substantial amount of data via AJAX to generate a graph

Currently, I am in the process of developing a server that will supply data and information to both a web client and a mobile client in the second phase. One of the key features is displaying this data on a graph, such as showing the price of a stock over ...

Is it feasible to have several polygons at once?

I am attempting to construct a unique pyramid using CSS properties like clip-path. Despite successfully creating a triangle, my efforts to create trapezoids beneath it have not been as fruitful. .container { min-width: 50%; max-width: 50%; } ...

Utilizing a function as an express middleware in a specific route

I've encountered an issue with my route where I have some messy code that I want to transform into middleware. However, the documentation provided by Express is a bit unclear and as a result, my code keeps throwing a 404 error. Is there a way for me ...

Encounter the issue of receiving 'undefined' as a response for data that was assigned within

Instead of calling the service in the .ts file and wasting time, I want to assign this as a global value that I can use. However, I keep getting undefined. Here is my service file @Injectable({ providedIn: 'root' }) export class DeService { ...

How can I alter the position of the scrollbar in a nested div structure in CSS?

Within my nested div structure, I have an inner div that functions as an auto scroller. However, this inner div is contained within another outer div, which itself is positioned inside yet another outer div. The scroller is only visible within the first ...

"Which is the better choice for a Django application's for loop: views.py or

I'm in the process of creating a Django app that features a word list. The app currently utilizes a speech function to inform the user of the first word on the list. The user is then able to record an audio clip of a word they say, which is converted ...

html - Link not clickable inside accordion header

I am encountering an issue with my accordion that has links in the header. Currently, the accordion opens when clicking anywhere on the header, including the link itself. This unintended behavior prevents users from being redirected to the link (href) when ...

What is the accurate way to write the ID selector for the select-option-selected in JQuery?

When it comes to extracting a value from a Select-Option using jQuery, the following syntax can be used. I have successfully retrieved data using this method. $( "#Vienna\\.rail0\\.track option:selected" ).text() However, there is an ...

The error message `Error [ERR_REQUIRE_ESM]: require() of ES Module` is triggered when attempting to call the old

I've been attempting to integrate with the Unsplash API, but I'm encountering an issue. When I try to execute the script using ts-node like this: ts-node unsplash.ts I receive the following error: C:\Users\USER\AppData\Roamin ...

Designate categories by utilizing the x-amz-tagging request header

I'm in the process of creating a Node program to upload files to aws s3, and I'm having trouble figuring out how to specify the x-amz-tagging with the request header. I attempted a solution, but it's not working as expected! function crea ...

Although the Flexbox is configured with 0 gap and margin, there remains a slight space between rows

I am currently attempting to utilize a flexbox in order to display several 600x300 images, however, I am encountering an unexpected small gap between the rows despite specifying a 0 gap and margin. Here is a screenshot for reference: .gallery { display: ...

A more efficient method for tallying values within an object (JavaScript, React)

Is there a more efficient way to determine the count of items with a specific key/value pair in a React state? When the list is large, this method may become a bottleneck. To illustrate the question, consider the following simplified example: class App ...