Определить текущий раздел с помощью JavaScript

0

Я работаю над сайтом onePage. У меня есть фиксированное меню в чистом HTML, вот код:

<header class="header-menu">
    <div class="header-menu-inside">
        <h1>WM Flying</h1>
        <nav class="menu menustandard">
            <a class="target-section1 current" href="#section1">Home</a>
            <a class="target-section2" href="#section2">About</a>
            <a class="target-section3" href="#section3">Portfolio</a>
            <a class="target-section4" href="#section4">The team</a>
            <a class="target-section5" href="#section5">Contact</a>
        </nav>
    </div>
</header>

<div class="main" id="main">
    <section id="section1" class="home">
    </section>
    <section id="section2" class="about">
    </section>
    <section id="section3" class="portfolio">
    </section>
    <section id="section4" class="team">
    </section>
    <section id="section5" class="contact">
    </section>
</div>

Затем я хотел бы изменить "текущий" раздел, чтобы применить другой стиль к выбранному меню. здесь мой javascript:

var currentSection = "section1";

function change($section){
    $('nav.menu a').removeClass('current');
    currentSection = $section.attr('id');
    $('body').removeClass();
    $('body').addClass( $section.attr('id') + '-visible' );
    $('.target-'+currentSection).addClass('current');
}

Тогда я просто применил css следующим образом:

nav a.current, nav a:hover{
    background: #F0F0F0;
    color: #E46C51;
}

для вызова функции я использую это:

$("#main section").waypoint( function( direction ) {
    if( direction === 'down' ) {
        change( $( this ) );
    }
}, { offset: '20%' } ).waypoint( function( direction ) {
    if( direction === 'up' ) {
        change( $( this ) );
    }
}, { offset: '-20%' } );

Но, похоже, это не работает. Может кто-нибудь мне помочь?

Большое спасибо,

никола

  • 0
    Не работает? Как так? Что оно делает? Есть ошибки?
  • 0
    Нет, это просто ничего не делает ... Как будто бы не использовалась ни одна функция
Показать ещё 8 комментариев
Теги:
website

1 ответ

0
$(function () {
    // Select all links with hashes
    $('a[href*="#"]')
      // Remove links that don't actually link to anything
      .not('[href="#"]')
      .not('[href="#0"]')
      .click(function (event) {
          // On-page links
          if (
            location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '')
            &&
            location.hostname == this.hostname
          ) {
              // Figure out element
              var target = $(this.hash);
              target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
              // Does a target exist?
              if (target.length) {
                  //remove all class 'current-session before add new class'
                  $(".menustandard").children().removeClass("current-session");
                  var id_target = target.attr('id');
                  var current_section_menu = $(".menustandard " + "a[href='#" + id_target + "']");
                  //add css current section selector
                  current_section_menu.addClass('current-session');

                  // Only prevent default if animation is actually gonna happen
                  event.preventDefault();
              }
          }
      });
});

и CSS

.current-session{
    //your style here
}

Ещё вопросы

Сообщество Overcoder
Наверх
Меню