简体   繁体   English

有没有办法让跨浏览器的CSS3代码干掉?

[英]Is there a way to make cross-browser CSS3 code DRY?

When I want to create a gradient background in CSS3 I have to do this: 当我想在CSS3中创建渐变背景时,我必须这样做:

background-color: #3584ba;
background-image: -webkit-gradient(linear, left top, left bottom, from(#54a0ce), to(#3584ba)); /* Safari 4+, Chrome */
background-image: -webkit-linear-gradient(top, #54a0ce, #3584ba); /* Safari 5.1+, Chrome 10+ */
background-image:    -moz-linear-gradient(top, #54a0ce, #3584ba);  /* FF3.6 */
background-image:      -o-linear-gradient(top, #54a0ce, #3584ba); /* Opera 11.10+ */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#54a0ce', endColorstr='#3584ba'); /* IE */

and this is really annoying. 这真的很烦人。 Is there a better solution, for example a jQuery plugin, that will make my code cross browser compatible, if I just use: 有没有更好的解决方案,例如jQuery插件,这将使我的代码跨浏览器兼容,如果我只是使用:

background-image: -webkit-linear-gradient(top, #54a0ce, #3584ba); /* Safari 5.1+, Chrome 10+ */

for example? 例如? Is there a tool to help me write CSS3 code more easy? 有没有工具可以帮助我更轻松地编写CSS3代码?

There are many tools for this: 有很多工具:

These are generally referred to as CSS Preprocessors. 这些通常称为CSS预处理器。

You would end up writing something like this once, like a function definition (usually called a "mixin"): 你最终会写一次这样的东西,比如函数定义(通常称为“mixin”):

.linear-gradient(@start, @end) {
    background-color: @end;
    background-image: -webkit-gradient(linear, left top, left bottom, from(@start), to(@end)); /* Safari 4+, Chrome */
    background-image: -webkit-linear-gradient(top, @start, @end); /* Safari 5.1+, Chrome 10+ */
    background-image:    -moz-linear-gradient(top, @start, @end);  /* FF3.6 */
    background-image:      -o-linear-gradient(top, @start, @end); /* Opera 11.10+ */
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='@start', endColorstr='@end'); /* IE */
}

Then to apply: 然后申请:

.my-class {
    .linear-gradient(#54a0ce, #3584ba);
}
.my-other-class {
    .linear-gradient(#ccc, #aaa);
}

Highly recommend. 极力推荐。

You could always use an online tool to generate them for you: 您可以随时使用在线工具为您生成它们:

http://www.colorzilla.com/gradient-editor/ http://www.colorzilla.com/gradient-editor/

http://www.css3maker.com/css-gradient.html http://www.css3maker.com/css-gradient.html

Is there a better solution, for example a jquery plugin, that will make my code cross browser compatible 是否有更好的解决方案,例如jquery插件,这将使我的代码跨浏览器兼容

For a client-side solution, you could use http://lea.verou.me/prefixfree/ 对于客户端解决方案,您可以使用http://lea.verou.me/prefixfree/

A script that lets you use only unprefixed CSS properties everywhere. 一个脚本,允许您在任何地方仅使用未加前缀的CSS属性。 It works behind the scenes, adding the current browser's prefix to any CSS code, only when it's needed. 它在幕后工作,只在需要时将当前浏览器的前缀添加到任何CSS代码中。

You would then only need to use the unprefixed function, which is mysteriously missing from your CSS: 然后你只需要使用未加前缀的函数,这在你的CSS中神秘地缺失了:

background-image: linear-gradient(top, #54a0ce, #3584ba);

I found the answer to the question. 我找到了问题的答案。 It is a program called "autoprefixer", it's free, and it uses Grunt (as well as a couple other things). 它是一个名为“autoprefixer”的程序,它是免费的,它使用Grunt(以及其他一些东西)。 You give it the file or directory with unedited css, and automatically adds the prefixes based on the specific browsers you want to target, and outputs them in a new file. 您使用未编辑的css为其提供文件或目录,并根据您要定位的特定浏览器自动添加前缀,并将它们输出到新文件中。 Here's an article about how to use it. 这是一篇关于如何使用它的文章。

Post about Autoprefixer by the author: http://css-tricks.com/autoprefixer/ How to use Grunt for the absolute beginner: http://24ways.org/2013/grunt-is-not-weird-and-hard/ Then you can look it up on Github to find the readme. 由作者发表关于Autoprefixer的帖子: http ://css-tricks.com/autoprefixer/如何使用Grunt作为绝对的初学者: http//24ways.org/2013/grunt-is-not-weird-and-hard/然后你可以在Github上查找自述文件。

I was looking for the exact same thing, and this was the best solution I found that just processes regular CSS. 我正在寻找完全相同的东西,这是我发现只处理常规CSS的最佳解决方案。 I hope that helps. 我希望有所帮助。

https://autoprefixer.github.io/ Try this one out.This does not necessarily help you write easier code, it's not a javascript library. https://autoprefixer.github.io/试一试。这不一定能帮助你编写更简单的代码,它不是一个javascript库。 But it does add vendor prefixes and removes useless css code. 但它确实添加了供应商前缀并删除了无用的css代码。

background-image: -webkit-linear-gradient(top, #54a0ce, #3584ba);
background-image: linear-gradient(top, #54a0ce, #3584ba);

Putting your code in, that is the result i get. 把你的代码放进去,这就是我得到的结果。 (I removed the -webkit- from the original code to get that result). (我从原始代码中删除了-webkit-以获得该结果)。

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

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