Left and Right Carousel Controls not Working when Smooth Scroll for Anchor Tags Used

喜你入骨 提交于 2019-12-12 06:37:18

问题


I am using the following script for smooth scroll effect for anchor tags:

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

After using this script the Left and Right controls for Bootstrap Carousel stopped working.

<a class="left carousel-control" href="#home-carousel" role="button" data-slide="prev">
                <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                <span class="sr-only">Previous</span>
            </a>
            <a class="right carousel-control" href="#home-carousel" role="button" data-slide="next">
                <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                <span class="sr-only">Next</span>
            </a>

How do I fix this?


回答1:


Then you just have to edit you code for the smooth scroll like this, depending if you have the ID header-menu or class header-menu

Change

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

to (if you use the ID header-menu)

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

or (if you use the class header-menu):

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

This way you will only focus the smooth scroll effect on the menu href elements, and the script will not "collide" with you bootstrap hrefs.



来源:https://stackoverflow.com/questions/30840059/left-and-right-carousel-controls-not-working-when-smooth-scroll-for-anchor-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!