有没有一种方法能够检测浏览器是否支持 background-attachment: fixed?
编辑:尽管桌面浏览器广泛支持此功能,但在便携设备上支持性较差,因此我希望能够检测到此功能。
有没有一种方法能够检测浏览器是否支持 background-attachment: fixed?
编辑:尽管桌面浏览器广泛支持此功能,但在便携设备上支持性较差,因此我希望能够检测到此功能。
目前无法直接和具体地测试固定背景的支持,因为移动浏览器会错误地报告它们支持。要亲自查看此错误,请在移动浏览器中加载此测试:
http://codepen.io/mattthew/pen/PwEqJa
function supportsCSS(value) {
try {
var style = document.body.style;
if (!("backgroundAttachment" in style)) return false;
var oldValue = style.backgroundAttachment;
style.backgroundAttachment = "fixed";
var isSupported = (style.backgroundAttachment === value);
style.backgroundAttachment = oldValue;
return isSupported;
}
catch (e) {
return false;
}
}
var el = document.getElementById('result');
var txt = '<b>This device & broswer supports:</b><br>';
txt += '{ background-attachment:fixed; } : ' + supportsCSS('fixed') + '<br>';
txt += { background-attachment:foo; } : ' + supportsCSS('foo');
el.innerHTML = txt;
基于最初由@chao编写的代码
可以通过多种方法间接测试支持性。
选项1:在小屏幕上移除固定背景
此选项使用CSS媒体查询来针对较小的屏幕,覆盖那些屏幕宽度为1024像素或更小的设备(这些设备通常会将固定背景呈现为不可见)。该选项的优点是:非常轻量级,只需要少量的CSS:
#some_element {
background-attachment: fixed;
}
@media all and (max-device-width: 1024px) {
/*
overwrite property for devices with
screen width of 1024px or smaller
*/
#some_element {
background-attachment: scroll;
}
}
很不幸,有一小部分平板电脑品牌的屏幕宽度为1280px和1366px,与最小的桌面屏幕重叠(按CSS高度此列表排序)。最安全的做法是在这个重叠区域使用滚动背景,以确保背景图片能够显示。如果你想玩得更加安全,请使用max-device-width: 1366px。然而,使用这些巨型平板电脑的人数远远小于使用小屏幕笔记本电脑的人数。
选项2:测试触摸事件和鼠标事件
该选项使用JS来测试浏览器是否支持触摸事件API,因此很可能在触摸屏设备上(设备更有可能将固定背景渲染为不可见)。这是一个重量级的选项。它需要Modernizr和jQuery:
if(Modernizr.touch) {
// this browser claims to support touch, so remove fixed background
$('#some_element').css('background-attachment','scroll');
}
不幸的是,这个选项也存在一个灰色地带。一些浏览器会给出错误的正面反馈,而另一些则会给出错误的负面反馈。您可以测试鼠标事件,例如:
$('body').mousemove(function(event){
// this device (touch or not) has a mouse, so revert to fixed background
$('#some_element').css('background-attachment','fixed');
$('body').unbind('mousemove');
});
你可以查看document.body.style
,确保:
Chrome、FF、Opera和Safari都会忽略将属性设置为无效值的尝试。IE9在尝试时会抛出异常。因此,如果出现任何一种情况,则该值肯定不受支持。(如果浏览器盲目地设置该值并保留它,那么它仍然可能无法正常工作。但此时,你真的不能让浏览器告诉你太多了。)
function supportsFixedBackground() {
try {
var style = document.body.style;
if (!("backgroundAttachment" in style)) return false;
var oldValue = style.backgroundAttachment;
style.backgroundAttachment = "fixed";
var isSupported = (style.backgroundAttachment === "fixed");
style.backgroundAttachment = oldValue;
return isSupported;
}
catch (e) {
return false;
}
}
我不再关注IE6,也没有其他不支持固定背景的浏览器可用,因此我无法测试设置“fixed”。
navigator.appName
和/或navigator.appVersion
的值是否在列表中之外,我不知道是否还有其他方法。 :P - cHaoclip
支持,因此我做了这个,并在clip
受支持时更改了DOM。如果不支持,则会回退到background-attachment:fixed;
请参见代码:https://codepen.io/AartdenBraber/pen/gGmdWK
DIV
);style
DOM 属性的值(在您的情况下为 element.style.backgroundAttachment
)设置为要检查的值(在您的情况下为 fixed
);style
值与指定的字符串进行比较。var elem = document.createElement('div');
elem.style.backgroundAttachment = 'fixed';
var isSupported = 'fixed' === elem.style.backgroundAttachment;
isSupported
为 true
,但是该功能本身不能按预期工作,则这不是一个错误的阳性结果:该功能得到支持,但其实现只是有一些特定的细节。 - Marat Tanalin@supports (background-attachment: fixed)
会返回true,因为浏览器引擎成功解释了属性和值。然后,移动webkit决定将您的背景绑定到与应用于相同的 堆叠上下文(相同的渲染平面),以实现更好的性能。(所有z-index都有自己的堆叠层,在桌面浏览器上,固定背景有自己的层。)
可以使用JS检测具有此渲染模式的浏览器,方法是检查用户代理中的iPhone
iPad
iPod
和Android
,这可能会针对正确呈现固定背景的移动浏览器,例如不断发展的移动Firefox。但是,我在纯CSS中找到了更好的方法:
仅适用于Mobile Safari和Chrome的CSS解决方案:
@supports (-webkit-overflow-scrolling: touch)
针对所有拒绝将背景绑定到视口的移动webkit版本。
因此,考虑到这一点,您可以默认修复背景,然后附加此@supports
规则以应用一种移动填充:
示例:
body {
background-image: url('bg.png');
background-size: cover; background-attachment: fixed;
}
@supports (-webkit-overflow-scrolling: touch) {
/* Detach problematic background */
body { background: none !important; }
/* Insert empty div at bottom of page */
#lonelyDiv {
position: fixed; top: 0px; left: 0px; z-index: -1;
width: 100%; height: 100%;
/* Using same background with size=cover may be possible in some situations */
background-image: url('bg.png'); background-size: cover;
/* Other designs may require a stretchable background...
or cropped versions for mobile aspect ratios applied after @media (orientation) rules */
background-image: url('mobile-bg.png'); background-size: 100%;
}
}
http://www.w3schools.com/cssref/pr_background-attachment.asp
页面下方有主要浏览器图标的图片。所有图标都没有变灰。页面显示该功能在所有浏览器中都支持。
@font-face
的网站,尽管IE是该规则的发明者。 - Niet the Dark Absol