Adjustable diagonal line within a container using CSS styling

Seeking assistance with a rectangular div that can have its diagonal length adjusted using JavaScript. I am looking to add a diagonal line to label the diagonal length when it is increased or decreased using the +/- buttons.

I require the line to resize automatically while maintaining quality. The background color should remain green, and the image and label colors should be white. To achieve this, I intend to use CSS to draw the line instead of using an image. An image demonstrating how the div should appear has been attached.

Thank you for your help!

View Div Demo Image

function max(){
    var w = document.getElementById('resizable').clientHeight;
    var h = document.getElementById('resizable').clientWidth;

    document.getElementById('resizable').style.height = h + 5 +'px';
    document.getElementById('resizable').style.width= w + 5 +'px';

    
  }
  
  
  function min(){
    var w = document.getElementById('resizable').clientHeight;
    var h = document.getElementById('resizable').clientWidth;

    document.getElementById('resizable').style.height = h - 5 +'px';
    document.getElementById('resizable').style.width= w - 5 +'px';

    
  }
<button class="btn" id="increase" onClick="max()">Max (+)</button>
<button class="btn" id="decrease" onClick="min()">Min (-)</button>

<div id="resizable" style="border:1px solid black;background-color:green;width:100px;height:50px;">

</div>

Answer №1

You have the option to utilize another division with a position:absolute

const dash = document.getElementById('dash')
const div = document.getElementById('resizable')

function getLength() {
  dash.textContent = Math.floor(Math.sqrt((Math.pow(div.clientHeight, 2) + Math.pow(div.clientWidth, 2))))
}

function maximize() {
  var h = document.getElementById('resizable').clientHeight;
  var w = document.getElementById('resizable').clientWidth;
  const angle = Math.atan(h / w) * 180 / Math.PI;

  document.getElementById('resizable').style.height = h + 5 + 'px';
  document.getElementById('resizable').style.width = w + 5 + 'px';

  dash.style.transform = `rotate(${angle}deg)`;
  getLength()
}


function minimize() {
  var h = document.getElementById('resizable').clientHeight;
  var w = document.getElementById('resizable').clientWidth;
  const angle = Math.atan(h / w) * 180 / Math.PI;

  document.getElementById('resizable').style.height = h - 5 + 'px';
  document.getElementById('resizable').style.width = w - 5 + 'px';

  dash.style.transform = `rotate(${angle}deg)`;
  getLength()
}

getLength()
#resizable {
  border: 1px solid black;
  background-color: green;
  width: 100px;
  height: 50px;
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
}

#dash {
  position: absolute;
  text-align: center;
  content: "";
  line-height: 0;
  width: 150%;
  height: 1px;
  background-color: #fff;
  transform: rotate(26.5deg);
}
<button class="btn" id="increase" onClick="maximize()">Max (+)</button>
<button class="btn" id="decrease" onClick="minimize()">Min (-)</button>

<div id="resizable">
  <div id="dash"></div>
</div>

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

When using jQuery each method, it may return an [object Object]

Having an issue with the html variable displaying [object Object] instead of actual elements. Any suggestions on what I should change? var html = ''; $.each(data.response, function(index, value) { var tr = $('<tr>'); var ...

Is there a way for me to specifically show just the initial image contained in the images[] array within the json data?

{"status": true, "data": {"basic": {"dob": "2018-02-02", "gender": "male", "contact": {"address": null}, "is_new": false, "is_email_verified": false, "is_phone_verified": false}, "listings": [{"pid": 109, "category": {"name": "Education"}, "location": {"l ...

Dividing the content: A two-column CSS layout

I am currently redesigning a layout that is using tables for a two-column design but I have encountered some issues. <div id="frame"> <div id="leftcol"> <div id="1">blah</div> </div> <div id="leftcol"> <div ...

Expand the column to occupy the remaining height of the row

I've been on the hunt for a solution to this issue, but I haven't had any luck finding one. Various flexbox properties like flex-grow: 1; and align-self: stretch; have been attempted, but none seem to achieve the desired outcome. The goal is to ...

Experiencing Issues with Sending Form Requests using Jquery/JSON in PHP backend

I'm currently facing an issue with posting data from my form to my PHP file using AJAX/JSON. When I click the submit button, nothing happens apart from the client-side jQuery error checking on the field. I've tried various methods to make it work ...

AngularJS: Automatically refreshing ng-repeat based on checkbox filter updates in code

I have a container that is populated using ng-repeat="item in items | filter:isselected(item)". To filter the items, I have checkboxes with ng-model="selecteditem[$index]" and a filter function $scope.selectedItems = []; $scope.isselected = function(item ...

Display Angular errors specifically when the input field is filled out and contains invalid data

I need help with toggling an error area on my form so that it only appears once there is input. It seems unnecessary for errors to show up if the user hasn't even started typing. <form name="registerForm"> <input type="email" name="email" ...

Alignment of slider arrows in a CSS carousel with Bootstrap 4.1.1

Is there a way to position the carousel control arrows at the left and right ends of the screen in Bootstrap 4.1.1? I attempted the code below but it didn't yield the desired result. .carousel-control .icon-prev{ left: 5px } .carousel-control .icon ...

How can I retrieve the most recent entry in supabase?

Is there a way to check the latest record of a user in Supabase using their user ID? I want to retrieve only the most recent date in descending order without fetching all records from the table. I am currently working on this project using Node.js with Exp ...

Problem encountered with JQuery Sparkline in Internet Explorer

I recently implemented jQuery sparkline on a webpage, sourced from . However, I encountered an issue with Internet Explorer when the container DIV is not large enough to display it properly. Surprisingly, the code worked perfectly fine in Firefox and Chrom ...

Error encountered: Multer TypeError - fields does not have a function called forEach

I'm currently working on a metadata reader using multer in node.js with express on c9. I've spent some time searching for solutions to my issue, but it seems like no one else has encountered the error I am facing: TypeError: fields.forEach is no ...

Ways to prevent flex from expanding all the way to 100% width?

My section has two columns centered in flexbox, but the container doesn't wrap around them as expected and instead fills the whole page. How can I make the section wrap around the text with a padding of 1rem? body { background-color: #444; col ...

What could be the reason behind the unexpected behavior of my Bootstrap 3 carousel?

I'm attempting to replicate this visual effect in my own carousel, featuring semi-transparent images on the left and right sides. However, I'm encountering a negative result with my project when transitioning between slides at : here. This is th ...

What steps do I need to take to set up CORS properly in order to prevent errors with

I encountered the following error message: "Access to XMLHttpRequest at 'api-domain' from origin 'website-domain' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HT ...

transform the encoded JSON data into a JavaScript array

When it comes to sending data from PHP to JavaScript using json_encode, the data structure looks something like this: [{"albumid":"ASaBFzCtl8","albumname":"anni","type":"3","access":"2","itemcount":"2"},{"albumid":"EmgsZ43ehT","albumname":"testalbum","typ ...

Creating a slider with a large central image and just three slides that automatically transition

I am trying to create a slider that includes: 3 different images 3 different text captions A button that opens a modal dialog similar to Bootstrap, with each slide's button displaying unique text in the modal The slider should automatically slide Fo ...

TSDX incorporates Rollup under the hood to bundle CSS Modules, even though they are not referenced

I have recently developed a React library with TSDX and you can find it here: https://github.com/deadcoder0904/react-typical This library utilizes CSS Modules and applies styles to the React components. Although the bundle successfully generates a CSS fi ...

Text fades into view from the bottom after a line break using CSS and JavaScript

I'm looking to create a unique fade reveal text effect for a multi-line H1. The goal is for each line of the text to fade up from its own row, rather than simply revealing from the bottom. Here's an example I've already developed, but it cu ...

Tips on styling div elements to act as table cells

Is there a way to style divs with ids 1&2 and have the inner divs act like a table? Specifically, I want the first column to display a label, the second column to display an input field, and the third column to display a button. I attempted to adjust ...

What is the JavaScript design for creating plugins?

[I'm brand new to the world of Javascript, so please bear with me.] I am developing an application in node.js that will feature a collection of plugins. Each "plugin" will consist of a function, or possibly two, that can manipulate a string in some w ...