简体   繁体   English

在响应式网站上使用javascript修改字体大小

[英]modifying font size with javascript on responsive website

I am making a responsive page with bootstrap. 我正在用引导程序制作一个响应页面。 In the middle of the screen i have a text: 在屏幕中间,我有一个文本:

<p id="entershop" ><a class=no-deco  href="shop.html">enter shop</a></p>

The page is using bootstrap, so the p is inside a div that has a certain size. 该页面正在使用引导程序,因此p位于具有特定大小的div内。 I made a script that creates an effect that the p gets bigger and smaller. 我制作了一个脚本,使p越来越大。 The p has the size 7rem and it goes from 7 to 10 with this script: p的大小为7rem,使用以下脚本从7变为10:

var time = 100;
var up = true;
var size = 7.0;
var getbiggersmaller  = function () {
var text = document.getElementById("entershop");


if (size > 9.5){
    up = false;
}
if (size< 7){
    up = true;
}
if (up){
    size+= 0.1;
    text.style.fontSize = size.toString() +"rem";
   // console.log(size.toString() +"rem");
}

if (up == false){
    size-= 0.1;
    text.style.fontSize = size.toString() +"rem";
   // console.log(size.toString() +"rem");
}


}

window.onload = function() {

setInterval(getbiggersmaller,time);
};

It looks fine on my 16:10 monitor but when I resize the window, the font gets way too big. 在我的16:10显示器上看起来不错,但是当我调整窗口大小时,字体变得太大。 I need a calculation / script which makes the amount the size is in-/decremented and the original font size etc dependant of the screen size. 我需要一个计算/脚本,使大小增加/减少以及原始字体大小等取决于屏幕大小。 As i am a beginner, I dont really know where to start :( 作为一个初学者,我真的不知道从哪里开始:(

I would strongly suggest using media query instead of javascript. 我强烈建议您使用媒体查询而不是JavaScript。 But if you are bent on using javascript I would suggest you use the unit 'pixel' instead of 'rem' because pixel stays constant and the value doesn't change. 但是,如果您热衷于使用javascript,我建议您使用单位“像素”而不是“ rem”,因为像素保持恒定并且值不变。 While rem changes according to the website design and it is translated to pixel after that calculation.Which may vary from situation to situation.Also i think you forgot to put quotes around the class name. 虽然rem根据网站设计而变化,并在计算后转换为像素,但因情况而异。我也认为您忘了在类名前后加上引号。 A Media query is a CSS technique used for creating responsive websites.Using media queries you can manipulate behaviour of your elements on various screen sizes. 媒体查询是一种用于创建响应式网站的CSS技术。使用媒体查询,您可以在各种屏幕尺寸上操纵元素的行为。 link to basic media query tutorial For example 链接到基本媒体查询教程 ,例如

html HTML

<p id="entershop" ><a class="no-deco"  href="shop.html">enter shop</a></p>

css: CSS:

@media only and screen(min-width:1200px) {
    .a.no-deco {
        font-size:9.5rem;
    }
}
@media only screen and (min-width: 992px) {
    .a.no-deco {
        font-size:8rem;
    }    
}

...and so on ...等等

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

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