视差/背景位置的特征检测
Feature detection for parallax / background-position
好的,所以我已经创建了自己的并行jQuery/JS脚本,但我只是不知道如何针对支持以下内容的浏览器(需要以下内容):
- 支持背景位置的转换
我正在尝试的网站(预览)是:我的测试站点
但是,我如何检测浏览器是否支持此功能?我可以根据窗口宽度运行媒体查询吗?我必须浏览嗅探吗?我在WWW上看不到任何能为我提供如何进行特征检测的线索。
如有任何帮助,我们将不胜感激。
我的JS代码在这里:(如果你喜欢,请随意使用)
var goTop = 0;
var thisGoTop = 0;
var thisHeight = 0;
var thisWinH = 0;
var bottomIntrusion = 0;
var thisMax = 0;
var bgPos = 0;
function parallax(){
goTop = $(window).scrollTop();
thisWinH = $(window).height();
$('div.para').each(function(){
thisGoTop = $(this).offset().top;
thisHeight = $(this).height();
thisMax = Math.floor(thisHeight * 0.3);
bottomIntrusion = (thisGoTop + (thisHeight / 2) - goTop) / thisWinH;
if (bottomIntrusion > 0) {
bgPos = 0 - (Math.floor(thisMax * bottomIntrusion));
$(this).css('background-position','center ' + bgPos + 'px');
}
});
}
parallax();
$(window).scroll(function(){
parallax();
});
编辑:我查阅了Modernizr库,没有发现任何关于CSS3的背景位置转换支持的线索。在这一点上,我真的很接近brower嗅探,我不想去那里。
<script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$( document ).ready( function() {
var Compatibility = function()
{
var context = this;
context.TestObject = function( id )
{
function CreateObject( id )
{
$('body').prepend('<div id="CompatibilityTestObject_' + id + '"></div>');
}
// Constructor
CreateObject( id );
return {
GetObject: function( id )
{
try
{
// Fetch with raw Javascript so if the javascript method does not exist it will throw an error
var fetchedDomElement = document.getElementById( "CompatibilityTestObject_" + id );
return fetchedDomElement;
}
catch( e )
{
// Failed to fetch DOM Element
return false;
}
},
DeleteObject: function( id )
{
try
{
$( "#CompatibilityTestObject_" + id ).remove();
return true;
}
catch( e )
{
// Failed to remove object
return false;
}
}
}
}
context.Factory = function()
{
return {
CreateTestObject: function( id )
{
var m_TestObject = new context.TestObject( id );
return m_TestObject.GetObject( id );
},
DeleteTestObject: function( id )
{
return context.DeleteObject( id );
}
}
}();
var CSS = function()
{
return {
BackgroundPosition: function()
{
var testObject = context.Factory.CreateTestObject( "BackgroundPosition" );
try
{
// My first try at testing for background positioning
testObject.style.backgroundPosition = "center";
return true;
}
catch( e )
{
// Implement the handling of this error
//alert( "Does not support backgroundPosition: " + e.message );
return false;
}
}
}
}();
return {
CheckCSS: CSS
}
}();
} );
</script>
复制上面的脚本。。。把它放在你的代码中,然后像这样调用它:
if( Compatibility.CheckCSS.BackgroundPostion() )
{
// Compatible browser
}
else
{
// Not compatible
}
相关文章:
- 当静态元素与滚动上的固定元素位置重叠时进行检测
- jQuery:检测Flexbox内部的位置
- 如何在 html Web 应用中检测滑动原点位置
- 使用特征检测通过点表示法检查对保留字的支持
- 什么's特征检测、特征推断和使用UA字符串之间的区别
- 视差/背景位置的特征检测
- HTML Audio - 使用 jQuery 检测位置
- 如何检测位置是否位于文本区域中一行的开头
- 如何特征检测XMLHttpRequest是否支持responseType=“”;阵列缓冲器”;
- HTML5拖放的setDragImage特征检测
- 检测位置.在一个框架中
- 特征检测是否需要用户手势
- 如何用jquery检测位置的变化
- 特征检测-狮子滚动条
- SVG中矢量效果属性的特征检测
- 特征检测与Modernizr不工作
- 带有换行符的文本区域maxlength的特征检测
- CSS3过渡属性的特征检测
- 如何为Web推送做特征检测
- 检测位置:用javascript固定