2016-09-27 3 views
0

У меня есть HTML-страницы и менюПлавная прокрутка с помощью JQuery

Меню:

<div id="sidebar-wrapper"> 
    <ul class="sidebar-nav"> 
     <li> 
      <a href="#info">info</a> 
     </li> 
     ... 
    </ul> 
</div> 

Часть содержания:

<div id="info" class="row"> 
    <div class="col-lg-12"> 
      <h1>Title</h1> 
      <p>Lorem Ipsum</p>  
    </div> 
</div> 

И я хочу, чтобы добавить гладкой прокрутки страниц на якорь. Поэтому я пробовал:

$(document).ready(function($) { 

    $('a[href*=#]:not([href=#])').click(function() { 
     if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { 
      var target = $(this.hash); 
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 
      if (target.length) { 
       $('html,body').animate({ 
        scrollTop: target.offset().top 
       }, 1000); 
       return false; 
      } 
     } 
    }); 

}); 

Но это не сработает. И свиток не гладкий.

ответ

1

Заменить функцию JQuery с этим:

$(function() { 
$('a[href*="#"]:not([href="#"])').click(function() { 
if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { 
    var target = $(this.hash); 
    target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 
    if (target.length) { 
    $('html, body').animate({ 
     scrollTop: target.offset().top 
    }, 1000); 
    return false; 
    } 
} 
}); 
}); 
+1

А в чем разница? – adeneo

+0

Подождите, пока вы замените '$ (document) .ready (function ($) {' с '$ (function() {' ?, это все? –

+0

Ahh и вы завернули 'href * =" # "' в кавычки ... .. –

Смежные вопросы