繁体   English   中英

增加/减少字体大小并存储在 cookie 中

[英]increase / decrease font size and store in cookie

我需要一个脚本来增加和减少 fonts 但仍然是用户返回站点时设置的值。 我相信这应该由 cookie 来完成。 我找到了一个例子,但是当我把它付诸实践时,什么也没有发生。 当我点击 A + (increaseFont) 时没有任何反应。 按照网站下面的脚本: https://erika.codes/jquery/increase-decrease-page-font-size-jquery/

<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js'></script>
<script src="https://cdn.jsdelivr.net/npm/js-cookie@rc/dist/js.cookie.min.js"></script>

<div>
    <span class="increaseFont">A+</span>
    <span class="decreaseFont">A-</span>
    <span class="resetFont">Aa</span>
</div>

<script>
var fontResize = {
    textresize : function(){
        var $cookie_name = "eip-FontSize";
        var originalFontSize = $("html").css("font-size");
 
        $.cookie($cookie_name, originalFontSize, { expires: 7, path: '/' });
 
        // if exists load saved value, otherwise store it
        if($.cookie($cookie_name)) {
            var $getSize = $.cookie($cookie_name);
            $("html").css({fontSize : $getSize + ($getSize.indexOf("px")!=-1 ? "" : "px")}); // IE fix for double "pxpx" error
        } else {
            $.cookie($cookie_name, originalFontSize);
            //$.cookie($cookie_name, originalFontSize, {expires: 7, path: '/' });
        }
 
        // reset font size
        $(".resetFont").bind("click", function() {
            $("html").css("font-size", originalFontSize);
            $.cookie($cookie_name, originalFontSize);
            //$.cookie($cookie_name, originalFontSize, { expires: 7, path: '/' });
        });
 
        // function to increase font size
        $(".increaseFont").bind("click", function() {
            var currentFontSize = $("html").css("font-size");
            var currentFontSizeNum = parseFloat(currentFontSize, 10);
            var newFontSize = currentFontSizeNum*1.05;
            //var newFontSize = currentFontSizeNum + 2;
            if (newFontSize, 11) {
                $("html").css("font-size", newFontSize);
                $.cookie($cookie_name, newFontSize);
                //$.cookie($cookie_name, newFontSize, { expires: 7, path: '/' });
            }
            return false;
        });
 
        // function to decrease font size
        $(".decreaseFont").bind("click", function() {
          var currentFontSize = $("html").css("font-size");
          var currentFontSizeNum = parseFloat(currentFontSize, 10);
          var newFontSize = currentFontSizeNum*0.95;
          if (newFontSize, 11) {
            $("html").css("font-size", newFontSize);
            $.cookie($cookie_name, newFontSize);
            //$.cookie($cookie_name, newFontSize, { expires: 7, path: '/' });
          }
          return false;
        });
    }
}
 
$(document).ready(function(){
    fontResize.textresize();
})
</script>

*我发现另一个有效但没有cookie的例子,当页面更新时,值恢复正常: https://jsfiddle.net/pairdocs/yq8Le0gn/4/

  • 您可以使用localStorage()来存储一个值。
  • 将 CSS body设置为font-size: 16px;
  • 将所有其他元素设置为以相对单位定义的字体大小,例如 ie: emrem
  • 使用 JS 将字体大小更改为body并查看所有其他元素相应调整。

使用两个-/+按钮

 const EL_body = document.querySelector("body"); const ELS_fontSize = document.querySelectorAll(".fontSize"); localStorage.fontSize = localStorage.fontSize || 16; // Read or default to 16px function changeSize() { EL_body.style.fontSize = `${localStorage.fontSize}px`; } ELS_fontSize.forEach(el => el.addEventListener("click", function() { localStorage.fontSize = parseInt(localStorage.fontSize) + parseInt(el.value); changeSize(); })); // Change size on subsequent page load changeSize();
 <button class="fontSize" type="button" value="-2">A-</button> <button class="fontSize" type="button" value="2">A+</button> <h1>Lorem ipsum...</h1> <p>Lorem ipsum...</p>

使用单选按钮

const EL_body = document.querySelector("body");
const ELS_fontSize = document.querySelectorAll("[name='fontSize']");
localStorage.fontSize = localStorage.fontSize || 16; // Read or default to 16px

function changeSize() {
  ELS_fontSize.forEach(el => el.checked = el.value === localStorage.fontSize);
  EL_body.style.fontSize = `${localStorage.fontSize}px`;
}

ELS_fontSize.forEach(el => el.addEventListener("change", function() {
  localStorage.fontSize = el.value;
  changeSize();
}));

// Change size on subsequent page load
changeSize();
[name="fontSize"]+span {
  display: inline-block;
  padding: 5px 10px;
  border: 1px solid currentColor;
}

[name="fontSize"]:checked+span {
  color: #0bf;
}
<label><input type="radio" name="fontSize" value="14" hidden><span>A-</span></label>
<label><input type="radio" name="fontSize" value="16" hidden checked><span>A</span></label>
<label><input type="radio" name="fontSize" value="18" hidden><span>A+</span></label>

<h1>Lorem ipsum...</h1>
<p>Lorem ipsum...</p>

使用 select 盒子

const EL_body = document.querySelector("body");
const EL_fontSize = document.querySelector("#fontSize");
localStorage.fontSize = localStorage.fontSize || 16; // Read or default to 16px

function changeSize() {
  EL_fontSize.value = localStorage.fontSize; // Update select value;
  EL_body .style.fontSize = `${localStorage.fontSize}px`;
}

EL_fontSize .addEventListener("change", function() {
  localStorage.fontSize = this.value;
  changeSize();
});

// Change size on subsequent page load
changeSize(); 
<select id="fontSize">
  <option value="14">Small</option>
  <option value="16">Normal</option>
  <option value="18">Big</option>
</select>
<h1>Lorem ipsum...</h1>
<p>Lorem ipsum...</p>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM