简体   繁体   English

用JavaScript调整文本大小的“最佳实践”是什么?

[英]What is the “best practice” for resizing text with Javascript?

What is the "best practice" (accessibility/usability-wise) for resizing text with Javascript? 使用Java调整文本大小的“最佳实践”(在可访问性/可用性方面)是什么?

My current solution is that for when the user clicks the resize link it adds an extra class to the body tag. 我当前的解决方案是,当用户单击调整大小链接时,它会向body标签添加一个额外的类。

It also sets a cookie and then when the page is loaded - onLoad() - it reads the cookie and reapplys the body class tag again if needs be. 它还设置一个cookie,然后在页面加载时-onLoad()-读取cookie,并在需要时再次重新应用body类标签。

The only thing not ideal is that it loads then applys - the previous solution was to edit the CSS file before the page loaded but that threw up security warnings if the CSS file was in a different directory. 唯一不理想的是先加载然后再应用-先前的解决方案是在页面加载之前编辑CSS文件,但是如果CSS文件位于其他目录中,则会引发安全警告。

So, essentially is my solution reasonable or is there a better way? 因此,从本质上讲,我的解决方案是否合理?或者有更好的方法吗?

Adam 亚当

What is the "best practice" (accessibility/usability-wise) for resizing text with Javascript? 使用Java调整文本大小的“最佳实践”(在可访问性/可用性方面)是什么?

Best practise is "Don't". 最佳做法是“不要”。

Specify the font size in relative units and let the native browser controls change it if the user wishes. 以相对单位指定字体大小,如果用户愿意,让本机浏览器控件对其进行更改。

Attempts to duplicate this functionality rarely give the font sizes that users need (and usually end up offering a choice atomic, microscopic and tiny). 尝试复制此功能的情况很少会给出用户所需的字体大小(通常最终会提供原子,微观和微小的选择)。 Yes, some users need 72pt text (or larger). 是的,某些用户需要72pt文本(或更大)。

The built in browser controls work on most, if not all, sites. 内置的浏览器控件可在大多数(如果不是全部)站点上运行。 You don't need to use up canvas real estate duplicating them. 您不需要用尽画布空间来复制它们。

Your solution sounds fine, with one addition: you can read the cookie as the page loads and add the class to the body element while the markup is generated. 您的解决方案听起来不错,有一个补充:您可以在页面加载时读取cookie,并在生成标记时将类添加到body元素。 Most server-side languages support this: PHP, JSP, RoR etc. Other than that, you have a solid solution. 大多数服务器端语言都支持:PHP,JSP,RoR等。除此之外,您还拥有可靠的解决方案。

Adding a CSS style to HTML via JavaScript 通过JavaScript向HTML添加CSS样式

I think your suggestion is an excellent way of doing it. 我认为您的建议是一种很好的方法。

It means you have the flexibility of being able to add the CSS class to any sub element rather than the body so that you can change the text size of a specific element if you so desire. 这意味着您可以灵活地将CSS类添加到任何子元素而不是正文中,从而可以根据需要更改特定元素的文本大小。

Reading the cookie on page load 在页面加载中读取Cookie

To get around the issue you describe I would look for the cookie in your server side language (php/asp/ruby etc) and display the content as specified by the cookie on page load. 为了解决您描述的问题,我将使用服务器端语言(php / asp / ruby​​等)查找cookie,并在页面加载时显示cookie指定的内容。

In PHP your CSS class definition may look like this: 在PHP中,您的CSS类定义可能如下所示:

 echo '<div class=\\"'.($_COOKIE['text_size']!='' ? $_COOKIE['text_size'] : 'normal'). '\\">'; 

What is the "best practice" (accessibility/usability-wise) for resizing text with Javascript? 使用Java调整文本大小的“最佳实践”(在可访问性/可用性方面)是什么?

"Web Accessibility Gone Wild" sums it up quite nicely imho: “ Web Accessibility Gone Wild” 总结得很不错,恕我直言:

If your default font size may be too small for site visitors, then make it an adequate size. 如果您的默认字体大小对于网站访问者而言可能太小,则使其足够大。

and, 和,

All browsers allow the sizing or scaling of the page content - why duplicate this browser functionality? 所有浏览器都允许调整页面内容的大小或比例-为什么要重复此浏览器功能?

Also, Care With Font Size : 另外, 请注意字体大小

The problem here is a basic usability and accessibility issue: a good design should look good without requiring the user to enlarge or reduce the text size. 这里的问题是一个基本的可用性和可访问性问题:一个好的设计应该看起来不错,而不需要用户放大或缩小文本大小。

However, if you have a valid reason - feel free to ignore this. 但是,如果您有正当的理由-请忽略此。

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

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