Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

296
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda