錨點全屏滾動(jQuery)

<!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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章