Smooth Scrolling Issue

2019-08-31 12:28发布

问题:

I am using following js code for the smooth scrolling on the page. The issue is I am not able to use the navigation of mycarousel after using following JS . Is there anyway to fix my following js so it doesnot effect my carousel to navigate. This issue is happening because of # (href="#myCarousel") on the carousel navigation. So in that case how can the issue be resolved.

$(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;
      }
    }
  });
});

MY Carousel Navigation:

<a class="left carousel-control" href="#myCarousel" data-slide="prev"><span class="glyphicon glyphicon-chevron-left"></span></a>
      <a class="right carousel-control" href="#myCarousel" data-slide="next"><span class="glyphicon glyphicon-chevron-right"></span></a>

回答1:

Replace this

$('a[href*=#]:not([href=#])').click(function() {

with:

$('a[href*=#]:not([href=#media])').click(function () { 

Happy coding!



回答2:

This worked for me. I replaced

$('a[href*=#]:not([href=#])').click(function() {

with

$('a[href*=#]:not([href=#carousel-example-generic])').click(function() {

where #carousel-example-generic can be the name of the carousel that the controls are pointing to