简体   繁体   English

当检测到一定的最小屏幕分辨率大小时,如何通过Javascript更改CSS属性

[英]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修改规则。

Details: 细节:

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.

相关问题 如何根据屏幕分辨率运行某个 JavaScript (jQuery) 函数? - How can I run a certain JavaScript (jQuery) function based on screen resolution? Css - 如果它到达屏幕上的某个位置,如何更改元素大小? - Css - how to change element size if it reached to certain spot on screen? javascript已检测到如何将屏幕分辨率数据调整为适用于Web - how to adapt screen resolution data to web once already detected by javascript 当 window 大小达到 javascript 中的某个点时,如何更改 css - How do you change css when window size gets to a certain point in javascript 屏幕一定大小时如何包含JavaScript文件? - How to include a JavaScript file when the screen is a certain size? 如何将使用 javascript 的动画限制为特定屏幕尺寸? - How do i restrict a animation using javascript to certain screen size? Javascript:如何获得浏览器的最小字体大小? - Javascript: How can I get a browser's minimum font size? 按下按钮时将浏览器分辨率更改为特定大小 - Change browser resolution to a certain size when pressed on a button 我在某些屏幕尺寸上禁用了某些jQuery脚本,但是如何在调整屏幕尺寸时检查它? - I Disabled certain jQuery script at certain screen size but how do I get it check when screen is resized? 如何更改此框的大小? (CSS) - How can I change the size of this box? (CSS)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM