2013-11-21 58 views
1

好的,所以我創建了自己的parallaxing jQuery/JS腳本,但我不知道如何定位支持以下(需要以下)的瀏覽器:視差/背景位置特徵檢測

  • 支持背景位置

網站(預覽),我想這就是在轉型: My Test Site

我如何可以檢測瀏覽器是否支持,雖然這? 我可以根據窗口寬度運行媒體查詢嗎? 我必須瀏覽器嗅探嗎? 我在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的背景位置變換支持的線索。我在這一點上越來越接近嗅探者,我不想去那裏。

+0

在http://modernizr.com/看看或更具體的這一部分:http://modernizr.com/docs/#features-css – Jimmyt1988

+0

什麼部分詹姆斯?任何人都可以基於堅實的經驗指向正確的方向嗎? – 3Dom

+0

啊,waddya知道......它並沒有專門在......中做到。所以你必須做什麼(並且隨意擴展modernizr庫,但是如果你是cba ..我永遠不會......等待我的答案,那應該可以完成這項工作) – Jimmyt1988

回答

2
<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 
} 
+0

非常有趣。你究竟給了我什麼?看起來你已經給了我一個bg位置檢查來驗證bg pos的特徵檢測嗎?我不是愚蠢的,但我需要幫助,以瞭解爲什麼和你給我... – 3Dom

+0

還沒有完成:)對不起。 – Jimmyt1988

+0

謝謝詹姆斯。我無法理解在這個級別解決別人代碼的熱情。如果你能解決這個問題,請隨時在任何地方使用/分享此代碼。 – 3Dom