[英]How can I change a CSS attribute by Javascript when a certain minimum screen resolution size is detected
I have this code: 我有以下代码:
function AUTADIV () {
var BRW = window.outerWidth;
x = (BRW/1280) * 20
document.getElementsByTagName("a").style.fontSize = x
}
and the tag <a>
is already under this class in a .css file: 并且标签<a>
已经在.css文件中的此类下:
a {
position:relative;
z-index:1;
color:White;
background-color: transparent;
font-size:20pt;
text-decoration: none;
text-shadow: blue 0em 0em 0.4em
}
When someone with a larger screen sees my site, the background does fill it all, but the font is too small. 当屏幕较大的人看到我的网站时,背景会填满所有内容,但是字体太小。 Is there any way to make it automatically resize? 有什么方法可以使其自动调整大小? If not, how can I change font-size:20pt
by JavaScript? 如果没有,如何通过JavaScript更改font-size:20pt
? My code only works if the font-size style is inline, and I need it in the CSS script. 我的代码仅在字体大小样式为内联时才有效,并且我在CSS脚本中需要它。
I have found that the code I need activates with the onResize
event. 我发现我需要的代码通过onResize
事件激活。
If it needs to be in the CSS then it might be difficult to do. 如果需要放在CSS中,则可能很难做到。 If however, it's able to be changed dynamically with JS then you can accomplish this with a simple test like: 但是,如果可以使用JS动态更改它,则可以使用简单的测试来完成此操作,例如:
(I'm using jquery) (我正在使用jQuery)
$.getDocHeight = function(){
return Math.max(
$(document).width(),
$(window).width(),
/* For opera: */
document.documentElement.clientWidth
);
};
if($.getDocHeight>threshhold){ // some threshhold of a max width
$('a').style('font-size','40pt');
}
This can be done in regular js as well. 这也可以在常规js中完成。 It's hard to determine the width on all different browsers, thats why I included the function. 很难确定所有不同浏览器的宽度,这就是为什么我包含该功能。 But once you have the width, you just need to do a simple check and you can bump up the font-size style for your anchor tags. 但是一旦有了宽度,您只需要做一个简单的检查,就可以提高锚标记的字体大小样式。 I suggest having static sizes so that the font is more predictable and doesn't scale with your page size. 我建议使用静态大小,以使字体更容易预测并且不会随页面大小缩放。
This is a best practice when considering different types of users (like mobile users where you definitely do not want the font to be so small that all of it fits on one page). 当考虑不同类型的用户时(这是移动用户,您绝对不希望字体太小而不能全部容纳在一页上),这是最佳做法。
Src for code: http://james.padolsey.com/javascript/get-document-height-cross-browser/ 代码的Src: http : //james.padolsey.com/javascript/get-document-height-cross-browser/
You may modify the rules by accessing the CSSRule-Object. 您可以通过访问CSSRule-Object修改规则。
IE<9 : http://help.dottoro.com/ljcufoej.php IE <9: http : //help.dottoro.com/ljcufoej.php
Others: http://help.dottoro.com/ljdxvksd.php 其他: http : //help.dottoro.com/ljdxvksd.php
You might get better results using a media query: 使用媒体查询可能会获得更好的结果:
@media all and (max-width: 1280px) {
a{font-size:12pt;}
}
You can repeat this for increasingly smaller sizes. 您可以重复此操作以减小尺寸。 It won't smoothly transition, but it doesn't require JavaScript (and besides so much changes when you resize a window that a jump in text size probably won't be noticed). 它不会平稳过渡,但不需要JavaScript(而且在调整窗口大小时进行了很多更改,以至于可能不会注意到文本大小的变化)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.