平滑滚动和引导选项卡冲突

时间:2013-03-05 17:45:38

标签: twitter-bootstrap tabs smooth-scrolling

我正在为我开发一个网站。我正在为单页网站使用带有smoothscroll脚本的bootstrap。

问题是,我需要一个标签窗格,其中包含有关各个服务的信息,但这与平滑滚动脚本相冲突,这意味着当我点击选项卡时,浏览器会变得混乱。当我禁用smoothscroll脚本时,标签工作正常。有没有办法解决我可以兼具两种功能的问题?

现在正在使用的平滑滚动脚本是......来自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>

2 个答案:

答案 0 :(得分:4)

这一行:

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

尝试替换为:

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

答案 1 :(得分:1)

我使用data-toggle="pill"作为标签式菜单,因此我将

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

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

工作正常。