有没有办法只让Opera浏览器看到一些CSS规则?
有没有办法只让Opera浏览器看到一些CSS规则?
在 Opera 10.63 上表现良好
noindex:-o-prefocus, .class {
color:#fff;
}
这个技巧适用于最新版本的Opera浏览器:
@media all and (-webkit-min-device-pixel-ratio:10000), not all and (-webkit-min-device-pixel-ratio:0) {
#id {css rule}
}
就我所测试的范围来看,它不会影响到其他任何浏览器,但随着网络技术的飞速发展,这种情况可能会持续数月。
使用纯CSS hack,你可能无法安全地限制你正在hack的上限版本(例如,-o-prefocus
可能会在你的hack停止修复问题并开始破坏它们之后长时间得到支持)。
// remember to limit maximum version, because hacking all future versions
// will eventually break the page
if (window.opera && window.opera.version() < 10)
{
document.documentElement.className += ' opera9';
}
在 CSS 中:
.opera9 .element-to-hack { /*declarations for opera <= 9 only*/ }
但请务必先仔细查看CSS规范,以确保你要改变的内容确实是一个Bug。Opera 10完全支持CSS2.1并通过了所有Acid测试,所以如果某些东西显示不正确,可能是因为其他原因(代码中的其他错误、细节或特殊情况等)。
不要只考虑“检测Opera”。
应该思考“检测不支持X特性的浏览器”。例如,下面这个JavaScript语句可以让你检测支持moz-border-radius特性的浏览器:
typeof (getComputedStyle(document.body, '').MozBorderRadius)=='string'
对于基于WebKit的浏览器(Safari、Chrome),下面是等效的代码:
typeof (getComputedStyle(document.body, '').WebKitBorderRadius)=='string'
function detectBorderRadiusSupport(){
var styleObj;
if( window.getComputedStyle ){
styleObj=window.getComputedStyle(document.body, '');
}else{
styleObj=document.body.currentStyle;
}
return typeof styleObj.BorderRadius != 'undefined' || typeof styleObj.MozBorderRadius != 'undefined' || typeof styleObj.WebKitBorderRadius != 'undefined';
}
// the below must be inside code that runs when document.body exists, for example from onload/document.ready/DOMContentLoaded event or inline in body
if(!detectBorderRadiusSupport())document.body.className+=' fakeBorderRadius';
CSS:
body.fakeBorderRadius .roundMyCorners{
/* CSS for Opera and others to emulate rounded corners goes here,
typically various background-image and background-position properties */
}
box-shadow
仍然存在某些重绘问题。在这种情况下,我认为除了从 Opera 中隐藏它,别无他法,你呢? - RamboNo5Opera12
@media (min-resolution: .001dpcm) {
_:-o-prefocus, .class {
background: red;
};
}
// Sets browser infos as html.className
var params = [];
$.each($.browser, function(k, v) {
var pat = /^[a-z].*/i;
if(pat.test(k)) { params.push(k); }
});
params = params.join(' ');
$('html').addClass(params);
<html lang="de" class="webkit version safari">
or
<html lang="de" class="opera version">
html.opera #content_lock {
background:rgba(0,0,0,0.33);
}
您可以使用JavaScript编写<link>
来包含特定的CSS文件。
if (navigator.userAgent.indexOf(’Opera’) != -1) {
document.write(””);
}
else {
document.write(””);
}
对于 Opera 7,您可以使用以下代码:
/*Visible to only Opera*/
@media all and (min-width: 0) {
/* css rules here */
}
然而,基于浏览器嗅探进行样式设计通常是不好的实践。
<link href="opera.css" rel="stylesheet" type="text/opera" media="all" />
虽然这个解决方案是一种CSS hack,而且不能保证在未来的Opera版本中得到支持。您也可以考虑使用以下解决方案:
@media all and (-webkit-min-device-pixel-ratio:10000),not all and (-webkit-min-device-pixel-ratio:0) {
.element{/*仅适用于Opera的样式*/}
}
http://bookmarks-online.net/link/1308/css-including-style-for-opera-only