简体   繁体   English

jQuery插件 - 内联CSS还是外部样式表?

[英]jQuery plugin - inline CSS or external stylesheet?

I'm coding a jquery plugin and I'm thinking whether I should move all the CSS in the .js file, just to make it a little bit easier to setup. 我正在编写一个jquery插件,我在想是否应该移动.js文件中的所有CSS,只是为了让它更容易设置。

Should I do this? 我应该这样做吗? Is it too bad, performance wise? 这太糟糕了,表现明智吗?

What is most common is to use: 最常见的是使用:

jquery.myplugin.js
jquery.myplugin.css

and deliver these together to anyone using the plugin. 并将这些一起交付给使用该插件的任何人。 It is an extra request but it's practicing separation of concerns: styling and logic . 这是一个额外的请求,但它正在实践关注点分离: stylinglogic

Don't do this and use a CSS file for layout and feel purposes. 不要这样做并使用CSS文件进行布局和感觉。 Loose coupling is a great asset. 松耦合是一项重要的资产。 This will ensure that any developer with CSS knowledge can change the look and layout in the future easily without fiddling with the javascript code. 这将确保任何具有CSS知识的开发人员可以轻松地在未来更改外观和布局,而无需使用javascript代码。

Layout and looks are altogether a different concern. 布局和外观完全是一个不同的问题。 Merging it with .js file will always require a javascript coder just for mere change to the background or dimensions of your element. 将它与.js文件合并将始终需要javascript编码器,仅仅是为了更改元素的背景或尺寸。

Performance wise, there won't be any difference to the eye. 性能方面,眼睛没有任何差别。 But, development wise, you will have nightmare. 但是,明智的发展,你会有噩梦。

It's a pure no-no IMHO 这是一个纯粹的禁止恕我直言

没有性能降低或类似的东西,只是它对于使用你的插件的人来说是否更具可读性和可维护性。

Why do you want to move the CSS to the JS file? 为什么要将CSS移动到JS文件? It's fairly easy to link a CSS file in the HTML. 在HTML中链接CSS文件相当容易。

I usually prefer having the CSS and the JS separate from each other so as to make it easier to maintain and modify if required. 我通常更喜欢让CSS和JS相互分离,以便在需要时更容易维护和修改。 It could be potentially messy to modify the CSS if the styles are located in the JS file itself. 如果样式位于JS文件本身中,则修改CSS可能会很麻烦。

Having the CSS loaded separately also allows the styles to get rendered across the HTML, even if there is a problem loading the JS. 单独加载CSS也允许样式在HTML中呈现,即使加载JS时出现问题。

To reduce the size, you could also minify the JS and the CSS files, if performance is a concern - http://code.google.com/p/minify/ 为了减小尺寸,如果性能受到关注,您还可以缩小JS和CSS文件 - http://code.google.com/p/minify/

More info on the YUI compressor - http://developer.yahoo.com/yui/compressor/ 有关YUI压缩器的更多信息 - http://developer.yahoo.com/yui/compressor/

just make css editable, the className and id are good for you, the css selector, you know, sample 只是让css可编辑,className和id对你有好处,css选择器,你知道,样本

style: 样式:

.graceful {width:100px;height:100px;background:#36c;}

script: 脚本:

var div = document.createElement('div');
    div.className = 'graceful';
    div.innerHTML = ' ';
    document.getElementById('_images').appendChild(div)

I'm working on a plugin that consumes a few resource files: sounds, css and a small image. 我正在开发一个消耗一些资源文件的插件:声音,css和一个小图像。 I already inlined the sounds and the image (really small in fact). 我已经内联了声音和图像(实际上非​​常小)。
The purpose is to make life easier for the plugin users, saving them the chance of not copying the resource files and having issues and loss of time (I'm a usability guy). 目的是让插件用户的生活更轻松,节省他们不复制资源文件和出现问题和浪费时间的机会(我是一个可用性人)。

In the same vein, I was researching about inlining the css file, to make the plugin self-contained, a single file containing everything. 同样,我正在研究内联css文件,使插件自包含,包含所有内容的单个文件。

The answer by @c4urself, that it's usual for a plugin to be made out of two files with the same name, a .js and a .css, made me change my mind: now I think that my user might waste time searching for the .css file rather than skipping ik. @ c4urself的答案,通常是一个插件由两个同名文件,.js和.css组成,让我改变主意:现在我认为我的用户可能会浪费时间搜索.css文件而不是跳过ik。
So I decided to go with the usual 2-files standard, that in a while will be reflected in my binabacus plugin structure. 所以我决定采用通常的2文件标准,有一段时间会反映在我的binabacus插件结构中。 Which, btw, I think nobody will actually use, it was a practice on leveraging the jquery-boilerplate that I'm documenting in a tutorial to be published soon. 哪个,顺便说一句,我认为没有人会真正使用它,这是一个利用jquery-boilerplate的做法,我正在编写的教程即将发布。

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

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