2016-08-30 37 views
-2

我已經嘗試了幾個在這裏提供的腳本來突出顯示滾動菜單項,但由於某些原因,它們都無法找到我的.section id s。突出顯示滾動問題上的菜單 - 找不到scrollTop的div id?

var currentScroll = $(this).scrollTop(); 
// $currentSection is somewhere to place the section we must be looking at 
var $currentSection 

// We check the position of each of the divs compared to the windows scroll positon 
$sections.each(function(){ 
    // divPosition is the position down the page in px of the current section we are testing  
    var divPosition = $(this).offset().top; 

    // If the divPosition is less than the the currentScroll position the div we are testing has moved above the window edge. 
    // the -1 is so that it includes the div 1px before the div leave the top of the window. 
    if(divPosition - 1 < currentScroll){ 
    // We have either read the section or are currently reading the section so we'll call it our current section 
    $currentSection = $(this); 

    // If the next div has also been read or we are currently reading it we will overwrite this value again. 
    // This will leave us with the LAST div that passed. 
    } 

    // This is the bit of code that uses the currentSection as its source of ID 
    var id = $currentSection.attr('id'); 
$('a').removeClass('active'); 
$("[href=#"+id+"]").addClass('active'); 

    }) 
}); 
}); 

這方面的工作了一段時間之後,我想這是無法發現這些div S的ID出於某種原因,也許。我能夠得到它到所有鏈接addClass,但它不會找到相關鏈接(「[href =#」+ id +「]」)或a(「[href =#」+ id +「 ]「) -

我把一個alert(id)在那裏,它不會返回id值;它返回「未定義」 ......

這裏是如何我nav建:

<nav id="ubermenu-main-111" class="ubermenu ubermenu-nojs ubermenu-main ubermenu-menu-111 ubermenu-responsive ubermenu-responsive-1113 ubermenu-responsive-collapse ubermenu-horizontal ubermenu-transition-slide ubermenu-trigger-hover ubermenu-skin-none ubermenu-bar-align-full ubermenu-items-align-right"> 
<ul id="ubermenu-nav-main-111" class="ubermenu-nav"> 
<li id="menu-item-3328" class="ubermenu-item ubermenu-item-type-custom ubermenu-item-object-custom ubermenu-item-3328 ubermenu-item-level-0 ubermenu-column ubermenu-column-auto" > 
<a class="ubermenu-target ubermenu-item-layout-default ubermenu-item-layout-text_only" href="#home" tabindex="0" data-ubermenu-scrolltarget="#home">... 

div.section

<div class="section mcb-section full-width menuitem " id="home"> 

我這一個從CodePen,但我試着從幾個也是如此,所有結果都一樣...

有人嗎?非常感謝...

+0

我錯過了這部分功能 - > $(文件)。就緒(函數(){// $部分incleudes所有涉及到菜單項的div容器 變量$部分= $(。 '.section'); //用戶滾動 $(window).scroll(function(){ –

+0

)您忘了將該代碼添加到答案中的代碼中嗎?是否可以編輯您的答案以包含此代碼? – Xetnus

+0

不,我忘了在我的問題中添加該代碼。這不是一個答案。謝謝,tho ... –

回答

-1

這已經解決 - 功能工作時,我把它放在頁面的不同部分。