2016-02-13 66 views
1

我有一个单页面网站,左侧有一个固定(粘滞)侧栏导航菜单,内容为主div。主要的div分为5个部分,每个ID中有许多元素。单页网站粘性导航:更改内容元素的活动状态

我试图实现一些相关的导航菜单相关的主要内容div中的元素相互依存/动态状态更改,但我努力使等式的所有部分都能正常工作。请参阅下面的代码。

  1. 当一个菜单项被选择

一个)它的状态应该改变到“选择”。和

b)页面应平滑滚动到主div中相应的部分ID;和

c)与选定菜单项对应的主div中的元素应将状态更改为“激活”。

  • 当用户向上和向下滚动的页面:
  • a)所述导航应根据针对当前部分进行自我更新(“选择” );和

    b)主要内容div中的元素在进入视口中心时应改变状态('active')。

    (我认识到,点1.C 2.B和或多或少都可以互换,但不知道最好遵循其逻辑。)

    从上面的列表,点1,似乎工作正常,到目前为止,但是当我尝试根据滚动位置更新导航时,我的代码开始崩溃。我用我有限的JavaScript技能打了一个障碍,非常感谢您的建议。

    $(function() { 
     
        $('.nav_menu_item a').click(function(evt) { 
     
        var selectedTab = $(this); 
     
        var featureGroup = selectedTab.parents('.sidebar_nav_container'); 
     
        var allTabs = featureGroup.find('.nav_menu_item a'); 
     
        var allContent = featureGroup.find('.feature_box'); 
     
    
     
        // get the rel attribute to know what box we need to activate 
     
        var rel = $(this).parent().attr('rel'); 
     
    
     
        // clear tab selections 
     
        allTabs.removeClass('selected'); 
     
        selectedTab.addClass('selected'); 
     
    
     
        // make all boxes "in-active" 
     
        $('.feature_box').each(function() { 
     
         $(this).removeClass('active'); 
     
         $(this).removeClass('in-active'); 
     
        }); 
     
    
     
        //show what we need 
     
        $('.feature_category_'+rel).addClass('active'); 
     
    
     
        // find correlated content 
     
        var idx = selectedTab.index(); 
     
        var selectedContent = $(allContent); 
     
        selectedContent.removeClass('in-active'); 
     
    
     
        $('html, body').animate({ 
     
         scrollTop: $("#"+rel).offset().top -90 
     
        }, 800); 
     
    
     
        }); 
     
    }); 
     
    
     
    
     
    $(document).ready(function() { 
     
    
     
        var length = $('#sidebar_wrapper').height() - $('.sidebar_nav_container').height() + $('#sidebar_wrapper').offset().top; 
     
    
     
        $(window).scroll(function() { 
     
    
     
        var scroll = $(this).scrollTop(); 
     
        var height = $('.sidebar_nav_container').height() + 'px'; 
     
    
     
        if (scroll < $('#sidebar_wrapper').offset().top) { 
     
    
     
         $('.sidebar_nav_container').css({ 
     
         'position': 'absolute', 
     
         'top': '0' 
     
         }); 
     
    
     
        } else if (scroll > length) { 
     
    
     
         $('.sidebar_nav_container').css({ 
     
         'position': 'absolute', 
     
         'bottom': '0', 
     
         'top': 'auto' 
     
         }); 
     
    
     
        } else { 
     
    
     
         $('.sidebar_nav_container').css({ 
     
         'position': 'fixed', 
     
         'top': '45px', 
     
         'height': height 
     
    
     
         }); 
     
    
     
        } 
     
        }); 
     
    
     
    }); 
     
    
     
    
     
    
     
    $(document).ready(function() { 
     
    
     
    \t \t (function highlightNav() { 
     
    \t \t  var prev; //keep track of previous selected link 
     
    \t \t  var isVisible= function(el){ 
     
    \t \t   el = $(el); 
     
    
     
    \t \t   if(!el || el.length === 0){ 
     
    \t \t    return false 
     
    \t \t   }; 
     
    
     
    \t \t   var docViewTop = $(window).scrollTop(); 
     
    \t \t   var docViewBottom = docViewTop + $(window).height(); 
     
    
     
    \t \t   var elemTop = el.offset().top; 
     
    \t \t   var elemBottom = elemTop + el.height(); 
     
    \t \t   return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom)); 
     
    \t \t  } 
     
    
     
    \t \t  $(window).scroll(function(){ 
     
    \t \t   $('.sidebar_nav_container > .nav_menu_item a').each(function(index, el){ 
     
    \t \t    el = $(el); 
     
    \t \t    if(isVisible(el.attr('href'))){ 
     
    \t \t     if(prev){ 
     
    \t \t      prev.removeClass('selected'); 
     
    \t \t     } 
     
    \t \t     el.addClass('selected'); 
     
    \t \t     prev = el; 
     
    
     
    \t \t     //break early to keep highlight on the first/highest visible element 
     
    \t \t     //remove this you want the link for the lowest/last visible element to be set instead 
     
    \t \t     return false; 
     
    \t \t    } 
     
    \t \t   }); 
     
    \t \t  }); 
     
    
     
    \t \t  //trigger the scroll handler to highlight on page load 
     
    \t \t  $(window).scroll(); 
     
    \t \t })(); 
     
    \t });
    .hidden { 
     
    \t display:block; 
     
    \t color: blue; 
     
    \t } 
     
    
     
    .features_page { 
     
        margin-top:12px; 
     
        position: relative; 
     
        } 
     
    
     
    .container { 
     
    \t \t margin:0 auto; 
     
    \t \t padding-left:12px; 
     
    \t \t padding-right:12px 
     
    \t \t } 
     
    
     
    .container .features_public_content_container { 
     
    \t height: 100% !important; 
     
    \t position: relative; 
     
    \t max-width:1200px; 
     
    \t margin-left:auto; 
     
    \t margin-right:auto; 
     
    \t font-size:12px; 
     
    \t padding:auto; 
     
    \t } 
     
    
     
    .col { 
     
        display:block; 
     
        float:left; 
     
        width:100%; 
     
        } 
     
    
     
    .span_2 { 
     
        width: 20%; 
     
        } 
     
    
     
    .span_10 { 
     
        width: 80%; 
     
        } 
     
    
     
    #sidebar_wrapper { 
     
        height: 100% !important; 
     
        position: fixed; 
     
    \t float: left; 
     
    \t padding-top: 12px; 
     
    \t } 
     
    
     
    #right { 
     
        height: auto; 
     
        top: 0; 
     
        right: 0; 
     
        float: right; 
     
    \t position: relative; 
     
    \t } 
     
    
     
    
     
    .sidebar_nav_container { 
     
        \t margin:auto; 
     
    \t \t position: relative; 
     
    \t \t float: left 
     
    \t \t } 
     
    
     
    .sidebar_nav_container .nav_menu_item a { 
     
    \t float:left; 
     
    \t width:100%; 
     
    \t color:#1193f6 !important; 
     
    \t text-align: left; 
     
    \t line-height:40px; 
     
    \t height:40px; 
     
    \t padding-left: 24px; 
     
    \t border-left: 1px solid #efefef; 
     
    \t text-transform:uppercase; 
     
    \t font-size:12px; 
     
    \t font-weight:500; 
     
    \t overflow:hidden; 
     
    \t cursor:pointer; 
     
    \t position:relative 
     
    \t } 
     
    
     
    
     
    .sidebar_nav_container .nav_menu_item a .indicator { 
     
    \t position:relative; 
     
    \t width:100%; 
     
    \t height: 100%; 
     
    \t display:none; 
     
    \t bottom:0; 
     
    \t left: 0 
     
    \t } 
     
    
     
    .sidebar_nav_container .nav_menu_item a.indicator:hover { 
     
    \t display:block; 
     
    \t border-left:4px solid #d6ecfd; 
     
    \t } 
     
    
     
    .sidebar_nav_container .nav_menu_item a.selected { 
     
    \t display:block; 
     
    \t border-left:4px solid #1193f6; 
     
    \t } 
     
    
     
    
     
    .feature_boxes_container { 
     
    \t padding-bottom:12px; 
     
    \t padding-top:12px; 
     
    \t text-align: center !important; 
     
    \t background: #f2f2f2; 
     
    \t } 
     
    
     
    .feature_boxes_container .feature_box { 
     
    \t \t \t float: none; 
     
    \t \t \t text-align: center; 
     
    \t \t \t display: inline-block; 
     
    \t \t \t position: relative; 
     
    \t \t background:#fff; 
     
    \t \t height:60px; 
     
    \t \t width:60px; 
     
    \t \t margin:12px; 
     
    \t \t padding: 24px; 
     
    \t \t vertical-align:top; 
     
    \t \t -webkit-border-radius:2px; 
     
    \t \t -moz-border-radius:2px; 
     
    \t \t -ms-border-radius:2px; 
     
    \t \t -o-border-radius:2px; 
     
    \t \t border-radius:2px; 
     
    \t \t -webkit-box-shadow:0 1px 3px rgba(0,0,0,0.12); 
     
    \t \t -moz-box-shadow:0 1px 3px rgba(0,0,0,0.12); 
     
    \t \t -ms-box-shadow:0 1px 3px rgba(0,0,0,0.12); 
     
    \t \t -o-box-shadow:0 1px 3px rgba(0,0,0,0.12); 
     
    \t \t box-shadow:0 1px 3px rgba(0,0,0,0.12) 
     
    \t \t } 
     
    
     
    .feature_boxes_container .feature_box.active { 
     
    \t \t border: 2px solid #1193f6; 
     
    \t \t }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
     
    
     
    <!DOCTYPE html> 
     
    <html> 
     
    
     
    <body id='public_layout' class='with_header'> 
     
    
     
        <div class="layout_wrapper"> 
     
    
     
        <div class="features_page"> 
     
    
     
         <div class="container features_public_content_container"> 
     
    
     
         
     
         <div class="col span_2" id="sidebar_wrapper"> 
     
    
     
          <div class="sidebar_nav_container"> 
     
    
     
          <div class="nav_menu_item feature_category_A selected" rel="A"> 
     
           <a href="#A" class="indicator">Features A</a> 
     
          </div> 
     
    
     
          <div class="nav_menu_item feature_category_B" rel="B"> 
     
           <a href="#B" class="indicator">Features B</a> 
     
          </div> 
     
    
     
          <div class="nav_menu_item feature_category_C" rel="C"> 
     
           <a href="#C" class="indicator">Features C</a> 
     
          </div> 
     
    
     
          <div class="nav_menu_item feature_category_D" rel="D"> 
     
           <a href="#D" class="indicator">Features D</a> 
     
          </div> 
     
    
     
          <div class="nav_menu_item feature_category_E" rel="E"> 
     
           <a href="#E" class="indicator">Features E</a> 
     
          </div> 
     
         
     
          </div> <!--/.sidebar_nav_container --> 
     
         
     
         </div> <!--/#left-sidebar --> 
     
    
     
    
     
         <div class="col span_10" id="right"> 
     
          
     
          \t <div class="feature_boxes_container"> 
     
          
     
    \t    <!-- Features A --> 
     
    \t 
     
    \t    <div class="feature_box feature_category_A active" id="A">Feature A-1</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_A active">Feature A-2</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_A active">Feature A-3</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_A active">Feature A-4</div> 
     
    \t    
     
    \t    <div class="feature_box feature_category_A active">Feature A-5</div> 
     
    \t \t \t \t \t \t \t \t \t \t \t 
     
    \t    <!-- Features B --> 
     
    \t 
     
    \t    <div class="feature_box feature_category_B in-active" id="B">Feature B-1</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_B in-active">Feature B-2</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_B in-active">Feature B-3</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_B in-active">Feature B-4</div> 
     
    \t    
     
    \t    <div class="feature_box feature_category_B in-active">Feature B-5</div> 
     
    \t    
     
    \t    <!-- Features C --> 
     
    \t 
     
    \t    <div class="feature_box feature_category_C in-active" id="C">Feature C-1</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_C in-active">Feature C-2</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_C in-active">Feature C-3</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_C in-active">Feature C-4</div> 
     
    \t    
     
    \t    <div class="feature_box feature_category_C in-active">Feature C-5</div> 
     
    \t   
     
    \t    <!-- Features D --> 
     
    \t 
     
    \t    <div class="feature_box feature_category_D in-active" id="D">Feature D-1</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_D in-active">Feature D-2</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_D in-active">Feature D-3</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_D in-active">Feature D-4</div> 
     
    \t    
     
    \t    <div class="feature_box feature_category_D in-active">Feature D-5</div> 
     
    \t    
     
    \t \t \t \t <!-- Features E --> 
     
    
     
    \t    <div class="feature_box feature_category_E in-active" id="E">Feature E-1</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_E in-active">Feature E-2</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_E in-active">Feature E-3</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_E in-active">Feature E-4</div> 
     
    \t    
     
    \t    <div class="feature_box feature_category_E in-active">Feature E-5</div>    
     
          
     
          
     
          </div> <!-- /.feature_boxes_container --> 
     
    
     
          </div> <!--/#right --> 
     
         
     
    \t  </div> <!--/.container .features_public_content_container --> 
     
    
     
    \t </div> <!--/.features_page --> 
     
    
     
        </div> <!--/.layout_wrapper --> 
     
    
     
    </body> 
     
    
     
    </html>

    回答

    1

    好了,所以这是jQuery的代码,最终使工作:

    $(window).on("scroll", function(){ 
     
        $(".feature_category").each(function() { 
     
        var sectionID = $(this).attr("id"); 
     
        if ($(window).scrollTop() >= $(this).offset().top -180) { 
     
         $('.nav_menu_item a.selected').removeClass("selected"); 
     
         $('.nav_menu_item a[href=\'#'+sectionID+'\']').parent().addClass("selected"); 
     
         $('.main_contents .feature_boxes_container .feature_category').removeClass('active'); 
     
         $('#'+sectionID+'').addClass('active'); 
     
        } 
     
        }); 
     
    }); 
     
    
     
    // Cache selectors 
     
    var topMenu = $(".sidebar_nav_container"), 
     
        topMenuHeight = topMenu.outerHeight(), 
     
        // All list items 
     
        menuItems = topMenu.find("a"), 
     
        // Anchors corresponding to menu items 
     
        scrollItems = menuItems.map(function(){ 
     
         var item = $($(this).attr("href")); 
     
         if (item.length) { return item; } 
     
        }); 
     
    
     
    // Bind click handler to menu items 
     
    // so we can get a fancy scroll animation 
     
    menuItems.click(function(e){ 
     
        var href = $(this).attr("href"), 
     
         offsetTop = href === "#" ? 0 : $(href).offset().top-topMenuHeight+36; 
     
        $('html, body').stop().animate({ 
     
        scrollTop: offsetTop 
     
        }, 300); 
     
        e.preventDefault(); 
     
    }); 
     
    
     
    // Bind to scroll 
     
    $(window).scroll(function(){ 
     
        // Get container scroll position 
     
        var fromTop = $(this).scrollTop()+topMenuHeight; 
     
    
     
        // Get id of current scroll item 
     
        var cur = scrollItems.map(function(){ 
     
        if ($(this).offset().top < fromTop) 
     
         return this; 
     
        }); 
     
        // Get the id of the current element 
     
        cur = cur[cur.length-1]; 
     
        var id = cur && cur.length ? cur[0].id : ""; 
     
    
     
        $(window).scroll(function(){ 
     
        menuItems 
     
        .parent().removeClass("selected") 
     
        .end().filter("[href=#"+id+"]").parent().addClass("selected"); 
     
        }); 
     
    }); 
     
    
     
    
     
    $(document).ready(function() { 
     
    
     
        var length = $('#sidebar_wrapper').height() - $('.sidebar_nav_container').height() + $('#sidebar_wrapper').offset().top; 
     
    
     
        $(window).scroll(function() { 
     
    
     
        var scroll = $(this).scrollTop(); 
     
        var height = $('.sidebar_nav_container').height() + 'px'; 
     
    
     
        if (scroll < $('#sidebar_wrapper').offset().top -90) { 
     
    
     
         $('.sidebar_nav_container').css({ 
     
         'position': 'absolute', 
     
         'top': '0' 
     
         }); 
     
    
     
        } else if (scroll > length) { 
     
    
     
         $('.sidebar_nav_container').css({ 
     
         'position': 'absolute', 
     
         'bottom': '0', 
     
         'top': 'auto' 
     
         }); 
     
    
     
        } else { 
     
    
     
         $('.sidebar_nav_container').css({ 
     
         'position': 'fixed', 
     
         'top': '60px', 
     
         'height': height 
     
         }); 
     
        } 
     
        }); 
     
    });

    我设法根据其他stackoverflow的答案凑齐这一点,以及一些有用的指针在这里。这可能是你见过的最糟糕的意大利面条代码,但它很有效。

    0

    让检查你的问题的某些部分。将更新为完整的答案

    \t \t \t $(function() { 
     
    \t \t \t $('.nav_menu_item a').click(function(evt) { 
     
    \t \t \t  var selectedTab = $(this); 
     
    \t \t \t  var featureGroup = selectedTab.parents('.sidebar_nav_container'); 
     
    \t \t \t  var allTabs = featureGroup.find('.nav_menu_item a'); 
     
    \t \t \t  var allContent = featureGroup.find('.feature_box'); 
     
    \t \t \t 
     
    \t \t \t  // get the rel attribute to know what box we need to activate 
     
    \t \t \t  var rel = $(this).parent().attr('rel'); 
     
    \t \t \t 
     
    \t \t \t  // clear tab selections 
     
    \t \t \t  allTabs.removeClass('selected'); 
     
    \t \t \t  selectedTab.addClass('selected'); 
     
    \t \t \t 
     
    \t \t \t  // make all boxes "in-active" 
     
    \t \t \t  $('.feature_box').each(function() { 
     
    \t \t \t  $(this).removeClass('active'); 
     
    \t \t \t  $(this).removeClass('in-active'); 
     
    \t \t \t  }); 
     
    \t \t \t 
     
    \t \t \t  //show what we need 
     
    \t \t \t  $('.feature_category_'+rel).addClass('active'); 
     
    \t \t \t 
     
    \t \t \t  // find correlated content 
     
    \t \t \t  var idx = selectedTab.index(); 
     
    \t \t \t  var selectedContent = $(allContent); 
     
    \t \t \t  selectedContent.removeClass('in-active'); 
     
    \t \t \t  
     
    \t \t \t  $('html, body').animate({ 
     
    \t \t \t   scrollTop: $("#"+rel).offset().top 
     
    \t \t \t  }, 2000); 
     
    \t \t \t 
     
    \t \t \t }); 
     
    \t \t \t }); 
     
    \t \t \t 
     
    \t \t \t 
     
    \t \t \t $('a').click(function(){ 
     
    \t \t \t $('html, body').animate({ 
     
    \t \t \t  scrollTop: $($(this).attr('href')).offset().top -90 
     
    \t \t \t }, 800); 
     
    \t \t \t });
    \t \t \t .clearfix:after { 
     
    \t \t \t content: " "; /* Older browser do not support empty content */ 
     
    \t \t \t visibility: hidden; 
     
    \t \t \t display: block; 
     
    \t \t \t height: 0; 
     
    \t \t \t clear: both; 
     
    \t \t \t } 
     
    \t \t \t .hidden { 
     
    \t \t \t \t display:block; 
     
    \t \t \t \t color: blue; 
     
    \t \t \t \t } 
     
    \t \t \t 
     
    \t \t \t .container { 
     
    \t \t \t \t \t margin:0 auto; 
     
    \t \t \t \t \t padding-left:80px; 
     
    \t \t \t \t \t padding-right:80px 
     
    \t \t \t \t \t } 
     
    \t \t \t 
     
    \t \t \t .features_public_content_container { 
     
    \t \t \t \t width:100%; 
     
    \t \t \t \t height: 100% !important; 
     
    \t \t \t \t position: relative; 
     
    \t \t \t \t max-width:1200px; 
     
    \t \t \t \t margin-left:auto; 
     
    \t \t \t \t margin-right:auto; 
     
    \t \t \t \t font-size:12px; 
     
    \t \t \t \t padding:0 40px 
     
    \t \t \t \t } 
     
    \t \t \t 
     
    \t \t \t 
     
    \t \t \t #left_sidebar { 
     
    \t \t \t  height: 100% !important; 
     
    \t \t \t  width: 25% !important; 
     
    \t \t \t  position: fixed; 
     
    \t \t \t \t float: left 
     
    \t \t \t \t } 
     
    \t \t \t 
     
    \t \t \t #right { 
     
    \t \t \t \t height: auto; 
     
    \t \t \t \t max-width:75% !important; 
     
    \t \t \t \t top: 0; 
     
    \t \t \t \t right: 0; 
     
    \t \t \t \t float: right; 
     
    \t \t \t \t position: relative; 
     
    \t \t \t \t } 
     
    \t \t \t 
     
    \t \t \t 
     
    \t \t \t .features_page { 
     
    \t \t \t  margin-top:12px; 
     
    \t \t \t  position: relative; 
     
    \t \t \t \t } 
     
    \t \t \t 
     
    \t \t \t .features_page .sidebar_nav_container { 
     
    \t \t \t \t margin: auto; 
     
    \t \t \t  position: relative; 
     
    \t \t \t \t float: left 
     
    \t \t \t \t } 
     
    \t \t \t 
     
    \t \t \t .features_page .sidebar_nav_container .feature_tab { 
     
    \t \t \t \t float:none; 
     
    \t \t \t \t width:100%; 
     
    \t \t \t \t color:#1193f6 !important; 
     
    \t \t \t \t text-align: left; 
     
    \t \t \t \t line-height:40px; 
     
    \t \t \t \t height:40px; 
     
    \t \t \t \t padding-left: 24px; 
     
    \t \t \t \t font-size:12px; 
     
    \t \t \t \t border-left:2px solid #efefef !important; 
     
    \t \t \t \t text-transform:uppercase; 
     
    \t \t \t \t font-weight:500; 
     
    \t \t \t \t overflow:hidden; 
     
    \t \t \t \t cursor:pointer; 
     
    \t \t \t \t position:relative 
     
    \t \t \t \t } 
     
    \t \t \t 
     
    \t \t \t .features_page .sidebar_nav_container .feature_tab .indicator { 
     
    \t \t \t \t position:absolute; 
     
    \t \t \t \t width:100%; 
     
    \t \t \t \t height: 100%; 
     
    \t \t \t \t display:none; 
     
    \t \t \t \t bottom:0; 
     
    \t \t \t \t left: 0 
     
    \t \t \t \t } 
     
    \t \t \t 
     
    \t \t \t .features_page .sidebar_nav_container .feature_tab:hover .indicator { 
     
    \t \t \t \t display:block; 
     
    \t \t \t \t border-left:4px solid #d6ecfd 
     
    \t \t \t \t } 
     
    \t \t \t 
     
    \t \t \t .features_page .sidebar_nav_container .feature_tab.selected .indicator { 
     
    \t \t \t \t display:block; 
     
    \t \t \t \t border-left:4px solid #1193f6; 
     
    \t \t \t \t } 
     
    \t \t \t 
     
    \t \t \t 
     
    \t \t \t .features_page .feature_boxes_container { 
     
    \t \t \t \t padding-bottom:12px; 
     
    \t \t \t \t padding-top:12px; 
     
    \t \t \t \t text-align: center; 
     
    \t \t \t \t background: #f2f2f2; 
     
    \t \t \t \t } 
     
    \t \t \t 
     
    \t \t \t .features_page .feature_boxes_container .feature_box { 
     
    \t \t \t \t \t background:#fff; 
     
    \t \t \t \t \t display:inline-block; 
     
    \t \t \t \t \t height:150px; 
     
    \t \t \t \t \t width:60px; 
     
    \t \t \t \t \t margin:12px; 
     
    \t \t \t \t \t padding: 24px; 
     
    \t \t \t \t \t text-align:center; 
     
    \t \t \t \t \t vertical-align:top; 
     
    \t \t \t \t \t -webkit-border-radius:2px; 
     
    \t \t \t \t \t -moz-border-radius:2px; 
     
    \t \t \t \t \t -ms-border-radius:2px; 
     
    \t \t \t \t \t -o-border-radius:2px; 
     
    \t \t \t \t \t border-radius:2px; 
     
    \t \t \t \t \t -webkit-box-shadow:0 1px 3px rgba(0,0,0,0.12); 
     
    \t \t \t \t \t -moz-box-shadow:0 1px 3px rgba(0,0,0,0.12); 
     
    \t \t \t \t \t -ms-box-shadow:0 1px 3px rgba(0,0,0,0.12); 
     
    \t \t \t \t \t -o-box-shadow:0 1px 3px rgba(0,0,0,0.12); 
     
    \t \t \t \t \t box-shadow:0 1px 3px rgba(0,0,0,0.12) 
     
    \t \t \t \t \t } 
     
    \t \t \t 
     
    \t \t \t .features_page .feature_boxes_container .feature_box.active { 
     
    \t \t \t \t \t border: 2px solid #1193f6; 
     
    \t \t \t \t \t } 
     
    \t \t \t 
     
    \t \t \t .features_page .feature_boxes_container .feature_box.in-active .feature_overview_icon { 
     
    \t \t \t \t color: #363636; 
     
    \t \t \t } 
     
    \t \t \t 
     
    \t \t \t 
     
    \t \t \t .nav_menu_item { 
     
    \t \t \t \t margin: 8px 0; 
     
    \t \t \t } 
     
    \t \t \t .nav_menu_item a{padding: 5px;} 
     
    \t \t \t .nav_menu_item .selected { 
     
    \t \t \t \t background-color: black; 
     
    \t \t \t \t border: 1px dotted red; 
     
    \t \t \t \t color: white; 
     
    \t \t \t }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
     
    
     
    
     
    \t \t <div class="layout_wrapper"> 
     
    
     
        <div class="features_page"> 
     
    
     
         <div class="container features_public_content_container clearfix"> 
     
    
     
         
     
         <div class="col span_2 clearfix" id="left_sidebar"> 
     
    
     
          <div class="sidebar_nav_container"> 
     
    
     
          <div class="nav_menu_item feature_category_A selected" rel="A"> 
     
           <a href="#A" class="indicator">Features A</a> 
     
          </div> 
     
    
     
          <div class="nav_menu_item feature_category_B" rel="B"> 
     
           <a href="#B" class="indicator">Features B</a> 
     
          </div> 
     
    
     
          <div class="nav_menu_item feature_category_C" rel="C"> 
     
           <a href="#C" class="indicator">Features C</a> 
     
          </div> 
     
    
     
          <div class="nav_menu_item feature_category_D" rel="D"> 
     
           <a href="#D" class="indicator">Features D</a> 
     
          </div> 
     
    
     
          <div class="nav_menu_item feature_category_E" rel="E"> 
     
           <a href="#E" class="indicator">Features E</a> 
     
          </div> 
     
         
     
          </div> <!--/.sidebar_nav_container --> 
     
         
     
         </div> <!--/#left-sidebar --> 
     
    
     
    
     
         <div class="col span_10" id="right"> 
     
          
     
          \t <div class="feature_boxes_container"> 
     
          
     
    \t    <!-- Features A --> 
     
    \t 
     
    \t    <div class="feature_box feature_category_A active" id="A">Feature A-1</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_A active">Feature A-2</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_A active">Feature A-3</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_A active">Feature A-4</div> 
     
    \t    
     
    \t    <div class="feature_box feature_category_A active">Feature A-5</div> 
     
    \t \t \t \t \t \t \t \t \t \t \t 
     
    \t    <!-- Features B --> 
     
    \t 
     
    \t    <div class="feature_box feature_category_B in-active" id="B">Feature B-1</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_B in-active">Feature B-2</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_B in-active">Feature B-3</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_B in-active">Feature B-4</div> 
     
    \t    
     
    \t    <div class="feature_box feature_category_B in-active">Feature B-5</div> 
     
    \t    
     
    \t    <!-- Features C --> 
     
    \t 
     
    \t    <div class="feature_box feature_category_C in-active" id="C">Feature C-1</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_C in-active">Feature C-2</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_C in-active">Feature C-3</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_C in-active">Feature C-4</div> 
     
    \t    
     
    \t    <div class="feature_box feature_category_C in-active">Feature C-5</div> 
     
    \t   
     
    \t    <!-- Features D --> 
     
    \t 
     
    \t    <div class="feature_box feature_category_D in-active" id="D">Feature D-1</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_D in-active">Feature D-2</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_D in-active">Feature D-3</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_D in-active">Feature D-4</div> 
     
    \t    
     
    \t    <div class="feature_box feature_category_D in-active">Feature D-5</div> 
     
    \t    
     
    \t \t \t \t <!-- Features E --> 
     
    
     
    \t    <div class="feature_box feature_category_E in-active" id="E">Feature E-1</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_E in-active">Feature E-2</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_E in-active">Feature E-3</div> 
     
    \t 
     
    \t    <div class="feature_box feature_category_E in-active">Feature E-4</div> 
     
    \t    
     
    \t    <div class="feature_box feature_category_E in-active">Feature E-5</div>    
     
          
     
          
     
          </div> <!-- /.feature_boxes_container --> 
     
    
     
          </div> <!--/#right --> 
     
         
     
    \t  </div> <!--/.container .features_public_content_container --> 
     
    
     
    \t </div> <!--/.features_page --> 
     
    
     
        </div>

    +0

    嗨@PHPExpert!非常感谢您张贴并指引我朝着正确的方向发展。这绝对有帮助! (我可以看到我在原来的CSS和jQuery中犯了一些基本的错误,所以非常感谢编辑。)我已经更新了原来的小提琴,所以方程的第一部分现在可以正常工作。我非常感谢你关于应用我在问题2)中概述的scrollSpy类型效果的想法。我试图为我的页面调整scrollSpy插件,但它不起作用。我应该发布我的“scrollSpy代码”吗?请指教。 –

    +0

    最受欢迎32k_。如果你觉得答案是有用的,那么做upvote并接受它。关于scrollSpy插件,我不太熟悉它。但是添加它可以帮助解决问题。 – PHPExpert

    +0

    我解决了问题的一部分,我解决了答案。我现在也更新了小提琴,以包括滚动效果的标记和jQuery。作为参考,请参阅:http://www.w3schools.com/bootstrap/bootstrap_scrollspy.asp此外,这篇文章在这里:https://stackoverflow.com/questions/17145609/complex-active-state-navigation-on-single我的代码目前不工作。我不知道我正在初始化Scrollspy ......并且也不确定这个HTML标记是否正确。如果您有任何建议,请告诉我。再次感谢! –

    1

    好了,所以我想我会附和,也许提供好一点的设置。使用您的演示JavaScript,我只能假设您将有一个页眉和页脚,并且您希望菜单添加并在某个点停止,但我并不完全确定,因为在您的演示中,情况并非如此,您只需要一个正常的固定菜单。所以我已经添加了这样的词缀样式,一旦你滚动过标题,菜单就会修复,然后当它到达页脚时停下来。然后,主要内容中的项目的活动类和导航将在滚动时更改。所以我建议使用jquery的每个函数,然后在一个范围内包装你的项目,并给这个范围一类功能 - 类别和一个id到这个范围。然后每次窗口滚动到这个范围时,您都可以触发一个更改类的函数。唯一的问题是,如果2个部分占据相同的行,那么你将遇到问题,所以我建议让你的功能盒具有百分比宽度,并将百分比设置为最小数量的盒子,所以如果你每分钟有4个功能盒部分将宽度设置为25%。然后,您可以使用媒体查询在较小的屏幕上使它们变大。

    这里是工作提琴演示Fiddle

    所以你的HTML标记将类似于此:

    <div class='pub_site_nav'> 
        <div class="container"> 
         Page navigation menu 
        </div> 
        </div> <!-- /.container .pub_site_nav --> 
    
        <div class="features_page"> 
    
         <div class="story_section_container"> 
          <div class="container"> 
           Hero section 
          </div> 
         </div><!-- /.container .pub_site_nav --> 
    
    
         <div class="main_contents"> 
          <div class="container"> 
    
    
           <div id="sidebar_wrapper"> 
            <div class="sidebar_nav_container"> 
              <div class="nav_menu_item selected"><a href="#A">Features A</a></div> 
              <div class="nav_menu_item"><a href="#B">Features B</a></div> 
              <div class="nav_menu_item"><a href="#C">Features C</a></div> 
              <div class="nav_menu_item"><a href="#D">Features D</a></div> 
              <div class="nav_menu_item"><a href="#E">Features E</a></div> 
            </div><!-- /.sidebar_nav_container--> 
           </div><!-- /.#sidebar_wrapper--> 
    
           <div class="feature_boxes_container"> 
    
            <span class="feature_category active" id="A"> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature A-1</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature A-2</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature A-3</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature A-4</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature A-5</div> 
              </div> 
            </span> 
    
            <span class="feature_category" id="B"> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature B-1</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature B-2</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature B-3</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature B-4</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature B-5</div> 
              </div> 
            </span> 
    
            <span class="feature_category" id="C"> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature C-1</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature C-2</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature C-3</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature C-4</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature C-5</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature C-6</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature C-7</div> 
              </div> 
            </span> 
    
            <span class="feature_category" id="D"> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature D-1</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature D-2</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature D-3</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature D-4</div> 
              </div> 
            </span> 
    
            <span class="feature_category" id="E"> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature E-1</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature E-2</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature E-3</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature E-4</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature E-5</div> 
              </div> 
              <div class="feature_box_wrapper"> 
               <div class="feature_box">Feature E-6</div> 
              </div> 
            </span> 
    
    
           </div><!-- /.feature_boxes_container--> 
    
          </div><!-- /.container--> 
         </div><!-- /.container .main_contents --> 
         <div class="random_content" style="height:800px;background:#111;"></div> 
    
         <div class="public_footer"> 
          <div class="container"> 
           Footer 
          </div> 
         </div> <!--/.container .public_footer --> 
        </div><!-- /.features_page --> 
    

    然后你的jQuery将看起来是这样的:

    $(window).on("scroll", function(){ 
        $(".feature_category").each(function() { 
         var sectionID = $(this).attr("id"); 
         if ($(window).scrollTop() >= $(this).offset().top - 220) { 
          $('.nav_menu_item.selected').removeClass("selected"); 
          $('.nav_menu_item a[href=\'#'+sectionID+'\']').parent().addClass("selected"); 
          $('.feature_category').removeClass('active'); 
          $('#'+sectionID+'').addClass('active'); 
         } 
        }); 
        /*Affix code*/ 
        var contentTop = $("#sidebar_wrapper").offset().top - 68; 
        var footerTop = $(".random_content").offset().top - $('.sidebar_nav_container').outerHeight() - 65; 
        if ($(this).scrollTop() >= contentTop) { 
         $('.sidebar_nav_container').addClass("fixed"); 
        }else{ 
         $('.sidebar_nav_container').removeClass("fixed"); 
        } 
        if ($(window).scrollTop() >= footerTop) { 
         $('.sidebar_nav_container').addClass("absolute_bottom"); 
        }else{ 
         $('.sidebar_nav_container').removeClass("absolute_bottom"); 
        } 
    }); 
    $(document).on("click", ".sidebar_nav_container a", function(e) { 
        e.preventDefault(); 
        var sectionID = $(this).attr("href"); 
        $('html, body').animate({ 
         scrollTop: $(sectionID).offset().top - 200 
        }, 800); 
    }); 
    

    我在jquery代码中添加了一些注释,如果你不尝试使用它,你可以删除菜单的词缀代码。然后你可以使用正常的位置固定CSS到你的菜单。

    最后这里是CSS,你可以使用:

    body,html{ 
        padding: 0; 
        margin: 0; 
        max-width: 100%; 
        background: #fff; 
    } 
    /*easier to have your container with margin and not padding that way your element is actually in that position and not padding to that position*/ 
    .container { 
        margin:0 80px; 
        position:relative; 
    } 
    .pub_site_nav { 
        position:fixed; 
        width:100%; 
        z-index:10; 
        top:0; 
        left: 0; 
        height:68px; 
        line-height:64px; 
        background:yellow; 
    } 
    .features_page{ 
        margin-top:65px; 
    } 
    .features_page .story_section_container { 
        background-color:#888; 
        padding-bottom:100px; 
        padding-top:72px; 
        text-align:left 
    } 
    /*Better to have a fixed width for your sidebar that way everything will work properly when your sidebar goes fixed because you need to account for your .container margin once it is in fixed position you can do it with a percentage if you use css calc but it has less browser compatibility also your main_contents should not have a max-width because when the sidebar goes fixed it will be in the wrong position if the screen goes above the max-width of the container*/ 
    #sidebar_wrapper { 
        position: absolute; 
        left: 0; 
        top: 0; 
        bottom:0; 
        width: 200px; 
        background:orange; 
    } 
    .sidebar_nav_container { 
        padding: 24px 0; 
        width: 200px; 
    } 
    .sidebar_nav_container.fixed{ 
        position:fixed; 
        top:68px; 
        left:80px; 
    } 
    .sidebar_nav_container.absolute_bottom{ 
        position:absolute; 
        top:auto; 
        bottom: 0; 
        left:0; 
    } 
    .sidebar_nav_item{ 
        width:100%; 
        padding:0; 
        margin:0; 
    } 
    .sidebar_nav_container .nav_menu_item a { 
        text-align: left; 
        padding:10px 10px 10px 24px; 
        border-left: 1px solid #efefef; 
        text-transform:uppercase; 
        text-decoration: none; 
        font-size:12px; 
        font-weight:500; 
        position:relative; 
        display:block; 
        /*so nav item doesn't jump on hover*/ 
        border:4px solid transparent; 
    } 
    
    .sidebar_nav_container .nav_menu_item a:hover { 
        border-left:4px solid #d6ecfd; 
    } 
    
    .sidebar_nav_container .nav_menu_item.selected a { 
        border-left:4px solid #1193f6; 
    } 
    
    .feature_boxes_container{ 
        background: #f2f2f2; 
        padding:45px 0 80px 0; 
        margin-left:200px; 
        /*The Following 2 lines removes the whitespace from feature box wrapper so they will align with a width of 25%*/ 
        font-size:0; 
        zoom: 1; 
    } 
    .feature_box_wrapper{ 
        width:25%; 
        margin:0; 
        padding:0; 
        display:inline-block; 
    } 
    .feature_boxes_container .feature_box { 
        text-align: center; 
        background:#fff; 
        margin:10px; 
        padding: 24px; 
        height:100px; 
        /*so everything aligns properly and doesn't jump when activated*/ 
        border:2px solid transparent; 
        font-size:12px; 
    } 
    .feature_boxes_container .feature_category.active .feature_box { 
        border: 1px solid red 
    } 
    .features_page .public_footer { 
        padding: 60px 0; 
        background: green; 
    } 
    @media screen and (max-width: 1049px){ 
        .feature_box_wrapper{width: 50%; } 
    } 
    @media screen and (max-width: 767px){ 
        .container{margin:0 10px; } 
        #sidebar_wrapper{width: 125px; } 
        .sidebar_nav_container{width:125px; } 
        .sidebar_nav_container.fixed{left:10px; } 
        .sidebar_nav_container.absolute_bottom{left:0; } 
        .feature_boxes_container{margin-left:125px; } 
        .feature_box_wrapper{width: 100%; } 
    } 
    

    的CSS可能会有点乱,因为我只是把它在一起,相当快,但我想你能惹它,使自己的布局,但这应该让你开始。

    如果您有任何问题随时发表评论我,我希望这有助于:)

    +0

    不错!我已经提高了你的答案,因为你的演示提琴完全符合我一直在努力实现的。简单,优雅,有效。此外,你对标题和页脚的假设是完全正确的,但没有考虑到这一点,所以感谢包含附加代码。我仍然在努力处理标记和CSS(试图使其适应我现有的设置)。一旦我修好了一切,我会将帖子标记为已回答。如果我无法使其发挥作用,我可能会发表另一条评论。非常感谢您的帮助! –

    +0

    很高兴帮助。我知道将side nav固定为宽度然后从主内容中删除宽度百分比可能会更容易,然后您应该可以将它复制并粘贴到任何布局中,但不能确定,因为我不知道你的布局是什么。这是一个例子,并修复了几个CSS的东西https://jsfiddle.net/wamosjk/k6e6vg65/有时百分比可能会有点棘手,你的侧面导航并不需要百分比。但如果您有任何问题,请随时评论我。 –

    +0

    所以我注意到这个方法是在更宽的屏幕上有一个小错误,并且有2个id占据相同的行。我会尽力找出一个更好的解决方案并稍后更新我的答案 –