user2136851
user2136851

Reputation: 31

Smooth scroll and bootstrap tabs conflict

I am developing a site for my sme. I'm using bootstrap with a smoothscroll script for the one page website.

Problem is, I need to have a tabbed pane with info on individual services but this is conflicting with the smooth scroll script, which means when i click on a tab the browser goes haywire. When I disable the smoothscroll script, tabs work just fine. Is there a way around where I can have both functionalities?

The smooth scroll script am using right now is this... taken from CSS-tricks.com

<script>
$(function() {

    function filterPath(string) {
        return string
        .replace(/^\//,'')
        .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
        .replace(/\/$/,'');
    }

    var locationPath = filterPath(location.pathname);
    var scrollElem = scrollableElement('html', 'body');

    // Any links with hash tags in them (can't do ^= because of fully qualified URL potential)
    $('a[href*=#]').each(function() {

        // Ensure it's a same-page link
        var thisPath = filterPath(this.pathname) || locationPath;
        if (  locationPath == thisPath
            && (location.hostname == this.hostname || !this.hostname)
            && this.hash.replace(/#/,'') ) {

                // Ensure target exists
                var $target = $(this.hash), target = this.hash;
                if (target) {

                    // Find location of target
                    var targetOffset = $target.offset().top;
                    $(this).click(function(event) {

                        // Prevent jump-down
                        event.preventDefault();

                        // Animate to target
                        $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {

                            // Set hash in URL after animation successful
                            location.hash = target;

                        });
                    });
                }
        }

    });

    // Use the first element that is "scrollable"  (cross-browser fix?)
    function scrollableElement(els) {
        for (var i = 0, argLength = arguments.length; i <argLength; i++) {
            var el = arguments[i],
            $scrollElement = $(el);
            if ($scrollElement.scrollTop()> 0) {
                return el;
            } else {
                $scrollElement.scrollTop(1);
                var isScrollable = $scrollElement.scrollTop()> 0;
                $scrollElement.scrollTop(0);
                if (isScrollable) {
                    return el;
                }
            }
        }
        return [];
    }

});
</script>

Upvotes: 3

Views: 3950

Answers (2)

Md Shibli
Md Shibli

Reputation: 11

I use data-toggle="pill" for tabbed menu, so I replace

$('a[href*=#]').each(function() {

with

$('a[href*=#]:not([data-toggle="pill"])').each(function() {

That works fine.

Upvotes: 1

Anto Khan
Anto Khan

Reputation: 37

This line:

$('a[href*=#]').each(function() {

try to replace with:

$('a[href*=#]:not([data-toggle="tab"])').each(function() {

Upvotes: 5

Related Questions