javascript - 重新加载后字体大小发生变化

标签 javascript css

enter image description here

在我点击 A+ 或 A- 后,它工作正常,但问题是在加载页面或点击页面上的某些链接后它恢复正常。

即使在点击某些链接后我也需要它来保持大小。

谢谢你的帮助。

代码如下:

function increaseFont() {

  var currentFontSizeH1 = $('h1').css('font-size'); //36

  var currentFontSizeNumH1 = parseFloat(currentFontSizeH1, 5);

  var currentFontSizeNumH1 = currentFontSizeNumH1 + 1.5;

  $('h1').css('font-size', currentFontSizeNumH1);

  var deccurrentFontSize = $('body').css('font-size'); //14
  var deccurrentFontSizeNum = parseFloat(deccurrentFontSize, 10);
  var decnewFontSize = deccurrentFontSizeNum * 1.2;
  $('body').css('font-size', decnewFontSize);
  $('body').css('font-size', decnewFontSize);
  $('p').css('font-size', decnewFontSize);
  $('span').css('font-size', decnewFontSize);
  $('li').css('font-size', decnewFontSize);
  $('h2').css('font-size', decnewFontSize);

  $('a').css('font-size', decnewFontSize);

  return false;
}

function decreaseFont() {

  var currentFontSizeH1 = $('h1').css('font-size');

  var currentFontSizeNumH1 = parseFloat(currentFontSizeH1, 5);

  var currentFontSizeNumH1 = currentFontSizeNumH1 - 1.5;

  $('h1').css('font-size', currentFontSizeNumH1);

  var deccurrentFontSize = $('body').css('font-size');
  var deccurrentFontSizeNum = parseFloat(deccurrentFontSize, 10);
  var decnewFontSize = deccurrentFontSizeNum / 1.2;
  $('body').css('font-size', decnewFontSize);
  $('p').css('font-size', decnewFontSize);
  $('span').css('font-size', decnewFontSize);
  $('li').css('font-size', decnewFontSize);
  $('h2').css('font-size', decnewFontSize);

  $('a').css('font-size', decnewFontSize);

  return false;
}
h1 {
  font-size: 36px;
  line-height: 42px;
  color: #007ea6;
}
body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
  font-size: 14px;
  line-height: 21px;
  color: #333333;
  background-color: #ffffff;
}
.....
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<div title="{" Increase the text size "|i18n( 'design/siteinstitutionnel/page_header_changerCSS' )}" style="width: 6px;" class="span1"><a href="#" style="text-decoration:none;font-size: 17px; box-shadow: 0 3px 0 #aaa,0px 0 0 #888,0px 3px 0 #666;" onClick="increaseFont();" alt="Aggrandir la taille du texte">A+</a>
</div>
<div title="{" Decrease the text size "|i18n( 'design/siteinstitutionnel/page_header_changerCSS' )}" style="width: 21px;" class="span1"><a href="#" style="text-decoration:none;font-size: 17px; box-shadow: 0 3px 0 #aaa,0px 0 0 #888,0px 3px 0 #666;" onClick="decreaseFont();" alt="Reduire la taille du texte">A-</a> 
</div>

最佳答案

您可以将大小保存在 Window.sessionStorage 的变量中。

// Save data to sessionStorage
sessionStorage.setItem('fontSize', value);

// Get saved data from sessionStorage
var size = sessionStorage.getItem('fontSize');

存储在 sessionStorage 中的数据在页面 session 结束时被清除。只要浏览器打开,页面 session 就会持续,并在页面重新加载和恢复后继续存在。在新选项卡或窗口中打开页面将导致启动新 session 。

关于javascript - 重新加载后字体大小发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36767373/

相关文章:

javascript - 在 Google Chrome 中加载图像的问题

javascript - 将数组中的值存储到jquery中的变量中

javascript - 命令值的事件监听器

css - 在 CSS 中, "width: fit-content;"和 "width: "fit-content(some_value); 之间有什么区别?

html - chrome 和 safari 中的圆形按钮中断

css - woocommerce 主题内联样式在 IE9 浏览器中不起作用

html - Apple/iPhone 上的链接无法正常工作

javascript - 使用 Rhino 和 Rhinodo 从 Java 使用 NodeJS 库

javascript - 使用discord.js 检查是否有人在 5 秒内发送了 2 条相同的消息

javascript - tinymce 编辑器的占位符插件