2012-08-16 79 views
1
滾動格菜單

我有jQuery的滾動菜單上的鼠標懸停垂直滾動:(它的一部分被隱藏,直到滾動到視圖)搜索與jQuery

<script type="text/javascript"> 
$(document).ready(function() { 

    //Scroll the menu on mouse move above the #sidebar layer 
    $('#sidebar').mousemove(function(e) { 

     //Sidebar Offset, Top value 
     var s_top = parseInt($('#sidebar').offset().top); 

     //Sidebar Offset, Bottom value 
     var s_bottom = parseInt($('#sidebar').height() + s_top); 

     //Roughly calculate the height of the menu by multiply height of a single LI with the total of LIs 
     var mheight = parseInt($('#menu li').height() * $('#menu li').length); 


     //Calculate the top value 
     //This equation is not the perfect, but it 's very close  
     var top_value = Math.round(((s_top - e.pageY)/100) * mheight/2); 

     //Animate the #menu by chaging the top value 
     $('#menu').animate({ 
      top: top_value 
     }, { 
      queue: false, 
      duration: 5000 
     }); 
    }); 
});​ 
</script> 

,然後我使用的是搜索和突出的jQuery腳本在滾動菜單搜索名稱:

<script type="text/javascript"> 
$(function() { 
    $('#text-search').bind('keyup change', function(ev) { 
     // pull in the new value 
     var searchTerm = $(this).val();) 

    // remove any old highlighted terms 
    $('#sidebar').removeHighlight(); 

    // disable highlighting if empty 
    if (searchTerm) { 
     // highlight the new term 
     $('#sidebar').highlight(searchTerm); 
    } 
    }); 
});​ 
</script> 

但我的問題是:當我在搜索詞輸入,它只會突出可見的菜單項。我如何讓jquery自動滾動到div中搜索到的術語?

回答

1

東西沿着線:

var searchTermTop, 
    searchTermBottom; 

searchTermTop = searchTerm.offsetParent().top; 
searchTermBottom = searchTerm.offsetParent().bottom; 

if(searchTermTop < 0){ 
    //set the menu scroll to it's current scroll + the searchTermTop 
} 
if(searchTermBottom > mheight){ 
    //set the menu scroll to it's current scroll + the searchTermBottom 
} 

類似的規定。如果您需要更多細節,請告訴我。現在需要用完辦公室,否則我會填寫一些細節。

+0

是的,這不適合我 - 可以更具體地說明我將它放在代碼中的位置嗎?謝謝! – user1604408 2012-08-16 21:58:48

+0

我會盡力爲你做一個工作演示。你能給我你正在使用的HTML嗎?將我的一些時間從你的js反向工程節省。 – lupos 2012-08-17 13:46:43

+0

好的 - 你有沒有可以發送給我的電子郵件,讓它更容易? – user1604408 2012-08-17 15:25:33