Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

287
Views
How to set Javascript style.tranform="scale" dynamically

I have declared a variable var scale=0.8 in a script on the body of my html and when i resize a graph made by mermaid i update the scale accordingly, for example

<script>
var scale=0.8;
DeployGraph(); 

function toggleZoomScreen(mode){
if (mode == 1){
 scale+=0.1;
//  document.body.style.zoom=scale.toString()+"%";
}
else if (mode == 2){
scale-=0.1;
//  document.body.style.zoom=scale.toString()+"%";
}
var mer = document.getElementById("mermaid");
mer.style.transform= "scale("+scale+","+scale+")";
} 
</script>

I want to somehow keep the value of the variable so that every time I refresh, DeployGraph is called and the graph gets resized in that same scale value. DeployGraph is declared on the head of the html, not the body. Can this be done?

I tried creating a different variable in the head and update it everytime the scale value changes, then in the DeployGraph I use element.style.transform= "scale("+scale+","+scale+")"; where element = document.getElementById("mermaid"); However, the value is always the default.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can't persist variables across page refreshes.

What you need to do is store the value you need in the browser and check for it once the page loads.

You can do this using URL search parameters, cookies or local storage.

When using local storage, you could do the following:

// On document load, after initialising the scale variable
scale = window.localStorage.getItem('scale') || 0.8; // add || 0.8 in case value is undefined

// In toggleZoomScreen
function toggleZoomScreen(mode){
  if (mode == 1){
    scale+=0.1;
  }
  else if (mode == 2){
    scale-=0.1;
  }

  // Update the value in local storage
  window.localStorage.setItem('scale', scale);

  var mer = document.getElementById("mermaid");
  mer.style.transform= "scale("+scale+","+scale+")";
} 
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!