1

I was trying to use the same function of fontSize() in mobile view to make it responsive but I don't know to do it. This is what I'm using in window view

document.getElementById('hakdog').onload = function () {func()};

function func() {
var str = document.getElementById("voter").innerHTML;
var a = str.length;

if(a >= 40) {
    voter.style.fontSize = "18px";
} else if(a >=30) {
    voter.style.fontSize = "22px";
} else {
    voter.style.fontSize = "27px";
}
}

Is there any same function for mobile view? I only know some function but I know its not responsive

$(document).ready(function(){
if($(window).width() < 480) { 
     $('h1').css('font-size','10px');
     $('h2').css('font-size','8px');
     $('h3').css('font-size','6px');
     $('h4').css('font-size','4px');
}
});

EDIT

I was trying to make it responsive using java just like on the first code I given, normal .css is not gonna work since its only going to give for h1, h2, h3, etc. I was pointing out that I wanted to make the responsive if the full name reached 40 letters then the fontSize would be 18px same with other functions given

EDIT 5:30pm MARCH 29

This is what I tried so far but didn't work as what I expected.

document.getElementById('hakdog').onload = function () {func()};
function func() {
$(document).ready(function() {
    function resizes() {
        if($(window).width() < 480) {
            var str = document.getElementById("voter").innerHTML;
            var a = str.length;

            if(a >= 40) {
                voter.style.fontSize = "9px";
            } else if(a >=30) {
                voter.style.fontSize = "10px";
            } else {
                voter.style.fontSize = "15px";
            }
        }
    }

    resizes();
    $(window).resize(resizes);
  
})
}
新Acesyyy
  • 1,152
  • 1
  • 3
  • 22

3 Answers3

0

Edit: Made changes to include the font size based on the length of the content.


This should work. You can check this out on the full page in the below code snippet.

$(document).ready(function() {
  function resizeMe() {
    if ($(window).width() < 480) {
      if ($("#name").text().length < 5) {
        $('h1').css('font-size', '46px');
      } else {
        $('h1').css('font-size', '10px');
      }
      $('h2').css('font-size', '8px');
      $('h3').css('font-size', '6px');
      $('h4').css('font-size', '4px');
    } else {
      $('h1').css('font-size', '20px');
      $('h2').css('font-size', '18px');
      $('h3').css('font-size', '16px');
      $('h4').css('font-size', '14px');

    }
  }

  resizeMe();
  $(window).resize(resizeMe);

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1 id="name">Helo</h1>
<h2>Yes</h2>
<h3>Nooooooooooooooooooooooooooo</h3>
<h4>okkkkkk</h4>
innocent
  • 864
  • 6
  • 17
  • Sorry but this is not what i wanted to do in js, Since I said that I need to change the `fontSize` depending on length content – 新Acesyyy Mar 29 '22 at 09:29
  • You can add the length of the content inside the if block and based on it the size it will get set – innocent Mar 29 '22 at 09:31
  • This is what i tried, i will edit my post – 新Acesyyy Mar 29 '22 at 09:31
  • Because im not using any `h1, h6` I only use `paragraph` with `classes` so its not really accurate your answer to my question. – 新Acesyyy Mar 29 '22 at 09:34
  • hmm ok, you can add the paragraphs or apply the following to classes I will remove this or edit it to change based on the length of the characters – innocent Mar 29 '22 at 09:37
  • Edit will be okay, no need for deletion for others – 新Acesyyy Mar 29 '22 at 09:38
  • @新Acesyyy I added the change I was telling. If you want a more dynamic approach. Also, I would recommend using CSS As that would be more efficient and easy to do (for eg, You can add the classes based on the content length). Also, You can checkout this answer https://stackoverflow.com/questions/16056591/font-scaling-based-on-width-of-container – innocent Mar 29 '22 at 10:11
0

Do you try to change 'px' to 'rem', 'rem' resize in relation to the size of the screen.

OmOursPor
  • 14
  • 2
0

This is what i meant for auto sizing for every device

var wid = $(window).width();

if(wid < 320) {
var str = document.getElementById("voter").innerHTML
var a = str.length;

if(a >= 40) {
    voter.style.fontSize = "9px";
} else if(a >= 25) {
    voter.style.fontSize = "11px";
} else {
    voter.style.fontSize = "15px";
}
}

Thanks for everyone commented out!

新Acesyyy
  • 1,152
  • 1
  • 3
  • 22