2012-12-19 63 views
1

我編寫了這段代碼來創建一個懸浮在瀏覽器窗口頂部的浮動邊欄。我得到這個錯誤在第3行鉻。Uncaught TypeError:無法讀取null的屬性'top':conflct

Uncaught TypeError: Cannot read property 'top' of null 

這是造成我使用的插件不工作。

$(document).ready(function(){ 
    // Floating sidebars on page nodes 
    var sidebartop = $('.page-node .panels-flexible-region-node_layout-right').offset().top; 
    $(window).scroll(function(){ 
     if($(window).scrollTop() > sidebartop) { 
      $('.page-node .panels-flexible-region-node_layout-right').css({position: 'fixed', top: '40px', margin: '0 0 0 650px'}); 
     } else { 
      $('.page-node .panels-flexible-region-node_layout-right').css({position: 'static', margin: '-35px 0 0 0'}); 
     } 
    }); 
}); 
+3

你確定'$('。page-node .panels-flexible-region-node_layout-right')'存在嗎? –

回答

3

我想,jQuery找不到指定的選擇器。我是對的

$('.page-node .panels-flexible-region-node_layout-right') 

返回空數組。儘量設置一些標識爲您的側邊欄(或元素你想獲得偏移)並重寫像

var sidebartop = $('#your-selector-id').offset().top; 

同樣的代碼,你可以.first-class .second-class.first-class.second-class之間尋找差異(注:對於缺乏第二個例子空間)查詢規則。

相關問題