javascript - Bootstrap 3 Fixed Top Navbar 'Flickering' On Mobile Scrolling using jQuery 单页滚动效果

标签 javascript jquery html css twitter-bootstrap

感谢您查看此问题。

我想知道是否有一种方法可以防止 Bootstrap 3 固定顶部导航栏在使用 jQuery 插件自动滚动时“闪烁”或上下跳动。

这里是一个活跃的例子:

http://startbootstrap.com/templates/grayscale/

如果您在手机(我使用的是 iPhone 4)上查看此模板,请使用菜单栏并单击链接。 jQuery 插件会将您带到页面的部分,但请注意顶部的菜单栏。它像疯了似的在四周闪烁和跳舞,并没有真正停留在原地。

这是与此示例关联的代码,但我见过的大多数其他示例都是人们使用 Bootstrap 固定顶部导航以不同方式做同样的事情。

HTML:

<body id="page-top" data-spy="scroll" data-target=".navbar-custom">

<nav class="navbar navbar-custom navbar-fixed-top" role="navigation">
    <div class="container">
        <div class="navbar-header page-scroll">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-main-collapse">
                <i class="fa fa-bars"></i>
            </button>
            <a class="navbar-brand" href="#page-top">
                <i class="fa fa-play-circle"></i>  <span class="light">Start</span> Bootstrap
            </a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse navbar-right navbar-main-collapse">
            <ul class="nav navbar-nav">
                <!-- Hidden li included to remove active class from about link when scrolled up past about section -->
                <li class="hidden">
                    <a href="#page-top"></a>
                </li>
                <li class="page-scroll">
                    <a href="#about">About</a>
                </li>
                <li class="page-scroll">
                    <a href="#download">Download</a>
                </li>
                <li class="page-scroll">
                    <a href="#contact">Contact</a>
                </li>
            </ul>
        </div>
        <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
</nav>

...

jQuery:

//jQuery for page scrolling feature - requires jQuery Easing plugin
$(function() {
    $('.page-scroll a').bind('click', function(event) {
        var $anchor = $(this);
        $('html, body').stop().animate({
            scrollTop: $($anchor.attr('href')).offset().top
        }, 1500, 'easeInOutExpo');
        event.preventDefault();
    });
});

CSS:

默认 Bootstrap 3 CSS 和以下自定义样式的组合:

.navbar {
    margin-bottom: 0;
    border-bottom: 1px solid rgba(255,255,255,.3);
    text-transform: uppercase;
    font-family: Montserrat,"Helvetica Neue",Helvetica,Arial,sans-serif;
    background-color: #000;
}

.navbar-brand {
    font-weight: 700;
}

.navbar-brand:focus {
    outline: 0;
}

.navbar-custom a {
    color: #fff;
}

.navbar-custom .nav li a {
    -webkit-transition: background .3s ease-in-out;
    -moz-transition: background .3s ease-in-out;
    transition: background .3s ease-in-out;
}

.navbar-custom .nav li a:hover,
.navbar-custom .nav li a:focus,
.navbar-custom .nav li.active {
    outline: 0;
    background-color: rgba(255,255,255,.2);
}

.navbar-toggle {
    padding: 4px 6px;
    font-size: 16px;
    color: #fff;
}

.navbar-toggle:focus,
.navbar-toggle:active {
    outline: 0;
}

同样,重现该问题的最佳方法是使用移动设备测试此示例站点:

http://startbootstrap.com/templates/grayscale/

在PC上测试,没有出现这个问题。

感谢阅读!如果您已经找到解决此问题的方法,我很乐意听到它,或者如果您愿意尝试解决这个问题,我们将不胜感激!

最佳答案

关于javascript - Bootstrap 3 Fixed Top Navbar 'Flickering' On Mobile Scrolling using jQuery 单页滚动效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21522527/

相关文章:

html - CSS 布局(div 高度)

javascript - Atom 无法为数组中的字符串元素着色

javascript - 实现 Redux 到 React 以从提供的端点返回 JSON 数据

jquery - 我的for循环有什么问题

html - CSS - 样式似乎妨碍了 Firefox 中的 URL 显示

html - 使 Bootstrap 图像 (svg) 靠得更近

javascript - 从子组件更新父组件的状态

javascript - 在javascript中将持续时间从秒转换为ISO格式

javascript - 增加元素的内容(高效的单行代码?)

javascript - jQuery/Javascript - else if 语句未到达 else if