簡體   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