-1

I need a script that increases and decreases the fonts but remains the value that the user has set when returning to the site. I believe that this should be done by cookie. I found an example but when I put it into practice nothing happens. When I click on A + (increaseFont) nothing happens. Follow the script below the site: 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>

*I found this other example that works but does not have the cookie, when the page is updated, the values return to normal: https://jsfiddle.net/pairdocs/yq8Le0gn/4/

Ed Dias
  • 175
  • 1
  • 2
  • 11

1 Answers1

2
  • You could use localStorage() to store a value.
  • Set CSS body to font-size: 16px;
  • Set al your other elements to font-size defined in relative units like i.e: em or rem.
  • Change the font size just to body using JS and see all the other elements adjust accordingly.

Using two -/+ buttons

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>

Using radio buttons

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>

Using a select box

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>
Roko C. Buljan
  • 164,703
  • 32
  • 260
  • 278