更好或更差:使用JavaScript vs CSS的样式 [英] Better or Worse: Styling with JavaScript vs CSS

查看:157
本文介绍了更好或更差:使用JavaScript vs CSS的样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个响应式设计想知道如果使用JavaScript或使用CSS来设计元素更好吗? CSS当然更容易维护,我不会争辩,但我更容易在JavaScript中构建一个对象。也许我为自己做更多的工作,而不是我应该,但我觉得我有更多的控制整体,我喜欢能够钩到屏幕上的每一个对象。

I'm working on a responsive design would like to know if it is better to style an element using JavaScript or with CSS? CSS is certainly easier to maintain, I won't argue that, but I'm a lot more comfortable building an object in JavaScript. Maybe I'm making more work for myself than I should, but I feel like I have more control overall and I like being able to hook into every object on the screen.

我有构造函数生成整个页面布局并将其推入DOM。没有图片,只有纯JavaScript样式和SVG图标。

I have constructors that generate the entire page layout and push it into the DOM. No images, just pure JavaScript styling and SVG icons.

我想知道的是:

    $
  • 是一个纯JavaScript解决方案

  • What performs better?
  • What, if any hardware support is there for JS or CSS?
  • Is a pure JavaScript solution better than using CSS Calc()?
  • What's more portable?
  • What's more forward compatible?

我倾向于避免使用jQuery,因为我更感兴趣了解什么使事情发生了,现在只有纯JavaScript和CSS3。 p>

I tend to avoid jQuery as I'm more interested in learning what makes things work right now, so pure JavaScript and CSS3 only, please.

推荐答案

CSS是为(X)HTML文档设置样式的最佳方式。

CSS is the best way to style an (X)HTML document.

即使您需要使用原始JavaScript或DOM或像jQuery这样的框架来对文档进行样式,也意味着您会为CSS规则赋予值。

Even if you need to style a document by using raw JavaScript or DOM, or some framework like jQuery, it'll mean you're giving values to CSS rules.

规则应为:


  • 使用纯CSS可以为可预测的文档设置样式 - 也可以

  • Use pure CSS when you can style a predictable document - also you can enhance your CSS and use CSS selectors for generalized or complex scenarios -.

使用JavaScript / DOM或JavaScript等框架,当文档或其部分't可预测的或是即时创建的和
你应用特殊效果,如淡入淡出或任何其他 - 事实上,
CSS 3.0有转换,所以可以做很多事情
without JavaScript - 。

Use JavaScript/DOM or frameworks like JavaScript when document or portions of it aren't predictable or are created on-the-fly and you're applying special effects like fades or any other - in fact, CSS 3.0 has transitions so it's possible to do a lot of things without JavaScript -.

毕竟,想想用CSS做什么更简单,

After all, think how simpler can be things done with CSS and what kind of overkill is using JavaScript instead, and keep in mind its cons (a very important point: browser compatibility and performance).

您使用的CSS越多,标准化程度越高,浏览器,高性能和可维护的网络。

The more CSS you use, the more standarized, cross-browser, performant and maintainable web.

这篇关于更好或更差:使用JavaScript vs CSS的样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆