锚点全屏滚动(jQuery)

来源:互联网 发布:log4j java 代码样例 编辑:程序博客网 时间:2024/06/05 18:24
<!DOCTYPE html><html>    <head>        <meta charset="UTF-8">        <title></title>        <style type="text/css">            * {                margin: 0;                padding: 0;            }            html,            body {                height: 100%;            }            div {                height: 100%;            }            div:nth-child(1) {                background: #f00;            }            div:nth-child(2) {                background: #ff0;            }            div:nth-child(3) {                background: #f0f;            }            div:nth-child(4) {                background: #00f;            }            p{position: fixed;top:0;}        </style>    </head>    <body>        <div id="view01"></div>        <div id="view02"></div>        <div id="view03"></div>        <div id="view04"></div>        <p>            <a href="#view01">1</a>            <a href="#view02">2</a>            <a href="#view03">3</a>            <a href="#view04">4</a>        </p>    </body>    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>    <script type="text/javascript">        $('a[href*=#]').click(function() {            if(location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {                var _target = $(this.hash);                console.log(this.hash)                _target = _target.length && _target || $('[name=' + this.hash.slice(1) + ']');                if(_target.length) {                    var targetTop = _target.offset().top;                    $('body').animate({                            scrollTop: targetTop                        },                        1000);                    return false;                }            }        });    </script></html>
0 0
原创粉丝点击