Prevent the left margin from causing the element to be pushed off the

Here is the code I've been working on:

https://jsfiddle.net/rzcos32n/

This code includes a function called "isItANumber" that calculates and determines specific results based on certain conditions. The function also controls the position of a circle element on a line, representing different percentage values.

However, there seems to be an issue when the percentage value in the circle reaches its maximum (100%). This causes the circle to go off the page due to its margin-left property being set too far. Is there a way to keep the circle within the visible area at all times, even when it's positioned at the extremes (0% or 100%)?

Answer №1

I made a modification by adding the code snippet

var percent =  "calc("+parseFloat(element1)*100+"% - 50px)";
. This adjustment ensures that we achieve 100%, while also subtracting the width of the circle (50px).

function isItANumber(el) {

var element1 = document.getElementById("circle").innerHTML;
var percent =  "calc("+parseFloat(element1)*100+"% - 50px)"; 

  var increased = parseInt(el.innerText);
  var element = document.getElementById("Value");

  if (isNaN(increased) || element1 >= 0) {
    document.getElementById("Result").innerHTML = "KanBan";
    document.getElementById("circle").style.marginLeft = percent;
  };
  if (isNaN(increased) || element1 >= 0.33) {
    document.getElementById("Result").innerHTML = "ScrumBan";
    document.getElementById("circle").style.marginLeft = percent;
  };
  if (isNaN(increased) || element1 >= 0.66) {
    document.getElementById("Result").innerHTML = "Scrum";
    document.getElementById("circle").style.marginLeft = percent;
  }
    if (isNaN(increased) || element1 > 0.9) {
    document.getElementById("Result").innerHTML = "Scrum";
    document.getElementById("circle").style.marginRight = '0%';
  }
}
var element = document.getElementById("circle");
isItANNumber(element);
#line {
  width: 100%;
  /* 2 */
  height: 5px;
  background: gray;
  position:relative;
  margin-top: 30px;
}

#circle {
  height: 50px;
  width: 50px;
  border-radius: 50%;
  background: blue;
  text-align: center;
  color: white;
  font-weight: 700;
  line-height: 50px;
  font-size: 14px;
  position:absolute;
  margin-top:-22.5px;
}
<div id="Result"></div>
<div id="line">
  <div id="circle">
    1.0
  </div>
</div>

Answer №2

Instead of using the marginLeft property, you can utilize the left property since it's a positioned element. Below is your accepted output or some helpful information:

function isItANumber(el) {

var element1 = document.getElementById("circle").innerHTML;
var percent =  parseFloat(element1)*100;

percent+='%';

  var increased = parseInt(el.innerText);
  var element = document.getElementById("Value");

  if (isNaN(increased) || element1 >= 0 && element1 < 0.33 ) {
    document.getElementById("Result").innerHTML = "KanBan";
    document.getElementById("circle").style.left = percent;
    
  };
  if (isNaN(increased) || element1 >= 0.33 && element1 < 0.66) {
    document.getElementById("Result").innerHTML = "ScrumBan";
    document.getElementById("circle").style.left = percent;
    
  };
  if (isNaN(increased) || element1 >= 0.66 && element1 < 0.9) {
    document.getElementById("Result").innerHTML = "Scrum";
    document.getElementById("circle").style.left = percent;
   
  }
    if (isNaN(increased) || element1 > 0.9) {
    document.getElementById("Result").innerHTML = "Scrum";
    document.getElementById("circle").style.left = 'auto';
    document.getElementById("circle").style.right = '0';
  }
}
var element = document.getElementById("circle");
isItANNumber(element);
#line {
  box-sizing: border-box;
  /* 2 */
  height: 5px;
  background: gray;
  position:relative;
  margin: 30px auto 0 auto;

}

#circle {
  height: 50px;
  width: 50px;
  border-radius: 50%;
  background: blue;
  text-align: center;
  color: white;
  font-weight: 700;
  line-height: 50px;
  font-size: 14px;
  position:absolute;
  margin-top:-22.5px;
  transition: 0.3s;
  left: 0;
}
<div id="Result"></div>

<div id="line">
  <div id="circle">
    1.0
  </div>
</div>

=== Thank you ===

Answer №3

Eliminated the javascript code that was setting 100% margin-left and replaced it with margin-left:auto. This adjustment positions the circle on the right side and utilized margin-top: -22.5px to align it above the line. Much obliged!

function isItANumber(el) {

  var element1 = document.getElementById("circle").innerHTML;
  var percent = parseFloat(element1) * 100;
  percent += '%';


  var increased = parseInt(el.innerText);
  var element = document.getElementById("Value");

  if (isNaN(increased) || element1 >= 0) {
    document.getElementById("Result").innerHTML = "KanBan";
    document.getElementById("circle").style.marginLeft = percent;
  };
  if (isNaN(increased) || element1 >= 0.33) {
    document.getElementById("Result").innerHTML = "ScrumBan";
    
  };
  if (isNaN(increased) || element1 >= 0.66) {
    document.getElementById("Result").innerHTML = "Scrum";
    document.getElementById("circle").style.marginLeft = percent;
  }
  if (isNaN(increased) || element1 > 0.9) {
    document.getElementById("Result").innerHTML = "Scrum";
    document.getElementById("circle").style.marginRight = '0%';
  }
}
var element = document.getElementById("circle");
isItANumber(element);
#line {
  width: 100%;
  /* 2 */
  height: 5px;
  background: gray;
  position: relative;
  margin-top: 30px;
  padding-top: 1px;
}

#circle {
  height: 50px;
  width: 50px;
  border-radius: 50%;
  background: blue;
  text-align: center;
  color: white;
  font-weight: 700;
  line-height: 50px;
  font-size: 14px;
  /* position: absolute; */
  margin-top: -22.5px;
  /* margin: auto; */
  /* margin-top: 100px; */
  margin-left: auto !important;
}
<div id="Result"></div>


<div id="line">
  <div id="circle">
    1.0
  </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

Experiencing difficulties with the footer design in bootstrap framework

After creating a basic web page, I encountered an issue with the footer. No matter what adjustments I make, the text is not centered and it does not stay fixed when changing the browser height. To view the code snippet and preview, visit: ...

Information regarding Django and managing static files during deployment through pythonanywhere

Is there a way for me to run collectstatic again in PythonAnywhere? I ran it after removing a line from my remote repository, but the new styles don't seem to be applying. Any suggestions on what I can do? English is not my first language. ...

Troubles with horizontal list incompatibility in Internet Explorer 6 and 7

I am facing an issue with displaying an unordered list in IE6+7. The problem arises when styling the list items with specific width and height properties, causing IE to stack the items vertically instead of horizontally. Below is my code snippet: For live ...

The issue arises when the Javascript function is triggered twice, resulting in only 3 out of 4

I'm currently working on a JavaScript calculator project. Everything is running smoothly except for the add() function (subtraction, multiplication, and division are all functioning properly). Additionally, when I hit "=" it displays the answer twice ...

Tips for utilizing the sticky-top class with Bootstrap 4 grid system

Has anyone had success using the sticky-top class in a grid layout? I'm having trouble getting it to work. <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"> <div class="container-fluid ...

How can Vue assign a distinct v-model parameter during iteration?

I need help with assigning unique parameters in a loop to each input tag using v-model. Here is the code that I have attempted: <template> <div v-for="item in lst" key="item"> <input :v-model="item"> </i ...

Mobile devices offer a seamless vertical scrolling experience

Here is the HTML structure I am working with: <div> <a>..</a> <i>..</i> <a>..</a> <i>..</i> <a>..</a> <i>..</i> </div> On larger screens, all elements are dis ...

Substitute the text with various phrases that include the specified pattern

Imagine you have a string such as $str = "<img src='i12'><img src='i105'><img src='i12'><img src='i24'><img src='i15'>...."; Can we replace each i+n with the corresponding va ...

Angular 2 form validation enhancements

How can I implement a combined validation in Angular2 using OR statements in a Control Group? For example, I have three input fields and I want one to be required, and the other two with an OR requirement. [Input name] = Required, ([Input email] OR [Input ...

"Seamlessly transition between items in a ng-repeat loop

My list of names in the scope is pretty straightforward: $scope.friends = [ {name:'John'}, {name:'Jessie'}, {name:'Johanna'}, {name:'Joy'}, {name:'Mary'}, {name:'Peter'}, ...

Struggling with your Dropdown Menu onclick functionality in Javascript? Let me lend

I am seeking assistance with implementing a Javascript onclick Dropdown Menu. The current issue I am facing is that when one menu is opened and another menu is clicked, the previous menu remains open. My desired solution is to have the previously open menu ...

The div element does not have an inline display style

My challenge lies in creating a container with two main elements - a header and body. Within the header div, I aim to have a 50px by 50px image alongside a user name displayed inline, but despite my efforts, I can't seem to achieve the desired layout. ...

Create a function that adds an event listener to a button that is

My dynamic radio buttons, text area, and click events are not functioning properly for the add and remove button. I attempted to use jQuery's .on function with no success: $("#TextBoxesGroup")‌​.on("click", ".abc", (function () { //some script } ...

Troubleshooting: Font-Awesome Symbols are not Showing up

I am facing an issue where the font-awesome social media icons are not displaying on any browser. However, all other icons appear to be working without any problems. I have attached a screenshot of the page open in Mozilla with inspect element (you can ...

Is it possible to hide a class by toggling it with jQuery mobile's click event?

I've come across an issue in my CSS where I have a class called hide that I applied to a textarea. When clicking a button, I want the class to be removed, but for some reason, it's not working as expected. I even added an alert in the function to ...

What could be the reason for only the First DIV showing up? Is there a way to display the other DIV

In my LabResults table, each order result is associated with a specific department: Department number 1 = Hematology Department Department number 2 = Biochemistry Department Department number 3 = Serology Department Department number 4 = Hormones Departm ...

Problems with navigation alignment in Flask website due to HTML, CSS, and

Here's a snapshot of my current website design: website I'm attempting to place the Login & Sign up buttons in line with the rest of the navigation bar. I've tried various methods without success so far. My attempts include adjusting text a ...

Firefox: repeated occurrences of style sheet in developer tools

I am currently managing a sample website with multiple linked style sheets at . One of these style sheet links needs to be toggled on and off, hence it includes a unique id: <link id="sketch" rel="stylesheet" type="text/css" ...

Use the onClick attribute with Iframe

Is there a method to wrap an Iframe within another element to create a simulated onClick event? I am aware that Iframes do not support events, however, I require the ability to monitor clicks from my website that are being redirected through the Iframe. A ...

In PHP, when a variable matches a value in a dropdown menu, include the 'selected' attribute to that option

I am working on implementing a feature where a drop-down menu will check if a user has already selected a value by querying the database. If the user has selected a value, I want to add a 'selected' attribute to that option so that it is preselec ...