smoothscroll是一款锚点平滑地滚动到指定的地方的jquery插件

smoothscroll是一款锚点平滑地滚动到指定的地方的jquery插件

smoothscroll是一款锚点平滑地滚动到指定的地方的jquery插件,可以解决chrome锚点失效的问题。

使用方式:

第一步引入bfwone 加载依赖项jquery

第二步use插件

方法:

window.scroll({ top: 2500, left: 0, behavior: 'smooth' });//滚动到scrolltop 2500的位置

window.scrollBy({ top: 100, left: 0, behavior: 'smooth' });向下滚动100个像素

window.scrollBy({ top: -100, left: 0, behavior: 'smooth' });//向上滚定100个像素

document.querySelector('.hello').scrollIntoView({ behavior: 'smooth' });//滚定到指定的element

element.scroll({ top: 0, left: 0, behavior: 'smooth' });//滚动元素到顶部

element.scrollBy({ top: 100, left: 0, behavior: 'smooth' });//元素向下滚动100

window.scroll({ top: 0, left: 0, behavior: 'smooth' });//滚动到顶部

document.querySelector('header').scrollIntoView({ behavior: 'smooth' });//滚动到元素

完整代码如下

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script id="bfwone" data="dep=jquery.17|jquery.okayNav&err=0" type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/bfwone.js"></script>
    <script type="text/javascript">
        bready(function() {
            use(["smoothscroll.min", "okayNav-base|okayNav-theme"], function() {
                var navigation = $('#nav-main').okayNav();

            });
        });
        function gotoTest1() {
            document.querySelector('#home').scrollIntoView({
                behavior: 'smooth'
            });
        }function gotoTest2() {
            document.querySelector('#shop').scrollIntoView({
                behavior: 'smooth'
            });
        }function gotoTest3() {
            document.querySelector('#blog').scrollIntoView({
                behavior: 'smooth'
            });
        }function gotoTest4() {
            document.querySelector('#service').scrollIntoView({
                behavior: 'smooth'
            });
        }function gotoTest5() {
            document.querySelector('#connect').scrollIntoView({
                behavior: 'smooth'
            });
        }function gotoTest6() {
            document.querySelector('#about').scrollIntoView({
                behavior: 'smooth'
            });
        }
        function gotoTest7() {
            document.querySelector('#test').scrollIntoView({
                behavior: 'smooth'
            });
        }
    </script>
    <style>
           <style>
        * {
            box-sizing: border-box
        }
        *:before, *:after {
            box-sizing: inherit
        }

        html {
            font-size: 62.5%;
            -webkit-tap-highlight-color: rgba(0,0,0,0);
            font-family: @font-family-base;
        }

        body {
            font-family: 'Roboto', 'Helvetica Neue', Helvetica, Arial, sans-serif;
            font-size: 1.4rem;
            line-height: 1.42857143;
            color: #555;
            background-color: #f7f7f7;
        }

        a {
            text-decoration: none
        }

        ul {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        main {
            margin-top: 10rem;
            text-align: center;
        }
    </style>
    </style>
</head>
<body>

    <header id="header" class="okayNav-header">
        <a class="okayNav-header__logo" href="#">
            Logo
        </a>

        <nav role="navigation" id="nav-main" class="okayNav">
            <ul>
                <li><a href="javascript:void(0)" onclick="gotoTest1()">首页</a></li>
                <li><a href="javascript:void(0)" onclick="gotoTest2()">购物</a></li>
                <li><a href="javascript:void(0)" onclick="gotoTest3()">博客</a></li>
                <li><a href="javascript:void(0)" onclick="gotoTest4()">服务</a></li>
                <li><a href="javascript:void(0)" onclick="gotoTest5()">联系我们</a></li>
                <li><a href="javascript:void(0)" onclick="gotoTest6()">关于我们</a></li>
                <li><a href="javascript:void(0)" onclick="gotoTest7()">测试</a></li>
            </ul>
        </nav>
    </header>
    <!-- /header -->

    <main>
        <h1 id="home">Resize your browser to preview okayNav</h1>
    </main>


    <section id="shop" style="min-height:620px;background-color:#777">
        <h1 style="padding-top:100px;">shop</h1>

    </section>


    <section id="blog" style="min-height:620px;background-color:#236777">
        <h1 style="padding-top:100px;">blog</h1>

    </section>


    <section id="service" style="min-height:620px;background-color:red">
        <h1 style="padding-top:100px;">service</h1>

    </section>

    <section id="connect" style="min-height:620px;background-color:#25fb65">
        <h1 style="padding-top:100px;">connect</h1>

    </section>

    <section id="about" style="min-height:620px;background-color:#66fb65">
        <h1 style="padding-top:100px;">about</h1>

    </section>

    <section id="test" style="min-height:620px;background-color:#e34565">
        <h1 style="padding-top:100px;">test</h1>

    </section>


</body>
</html>

官网http://iamdustan.com/smoothscroll/
立即下载smoothscroll.min.js查看所有js插件

网友评论0

程序员在线工具箱