文章出處

  如果滾動頁面也是DOM沒有解決的一個問題。為了解決這個問題,瀏覽器實現了一下方法,以方便開發人員如何更好的控制頁面的滾動。在各種專有方法中,HTML5選擇了scrollIntoView()作為標準方法。scrollIntoView()可以在所有的HTML元素上調用,通過滾動瀏覽器窗口或某個容器元素,調用元素就可以出現在視窗中。如果給該方法傳入true作為參數,或者不傳入任何參數,那么窗口滾動之后會讓調動元素頂部和視窗頂部盡可能齊平。如果傳入false作為參數,調用元素會盡可能全部出現在視口中(可能的話,調用元素的底部會與視口的頂部齊平。)不過頂部不一定齊平.

一、scrollIntoView

html

<div>
    <h2>scrollIntoView</h2>
    <button id="roll1">scrollIntoView(false)</button>
    <button id="roll2">scrollIntoView(true)</button>

    <div>
        <div id="myDiv"></div>
        <div id="roll_top">
            scrollIntoView(ture)元素上邊框與視窗頂部齊平
            <span id="bottom">scrollIntoView(false)元素下邊框與視窗底部齊平</span>
        </div>
    </div>
</div>

css

       #myDiv {
            height: 900px;
            background-color: gray;

        }

        #roll_top {
            height: 900px;
            background-color: green;
            color: #FFF;
            font-size: 50px;
            position: relative;
        }

        #bottom {
            position: absolute;
            display: block;
            left: 0;
            bottom: 0;
        }

js

  window.onload = function () {
        document.querySelector("#roll1").onclick = function () {
            document.querySelector("#roll_top").scrollIntoView(false);
        };
        document.querySelector("#roll2").onclick = function () {
            document.querySelector("#roll_top").scrollIntoView(true);
        };
    }

二、滾動監聽

html

<div>
    <h2>scroll</h2>
    <div id="nav">
        <div class="f1">floor1</div>
        <div class="f2">floor2</div>
        <div class="f3">floor3</div>
        <div class="f4">floor4</div>
        <div class="f5">floor5</div>
    </div>
    <p>
        頁面結構
    </p>
    <div class="main">
        <div id="f1">測試1</div>
        <div id="f2">測試2</div>
        <div id="f3">測試3</div>
        <div id="f4">測試4</div>
        <div id="f5">測試5</div>
    </div>
</div>

css

      .main div {
            height: 1000px;
            width: 300px;
            margin: 20px;
            background-color: #C0C0C0;
        }

        #nav {
            position: fixed;
            width: 100px;
            height: 200px;
            top: 40%;
            right: 10px;
        }

        #nav div {
            cursor: pointer;
            text-align: center;

        }

js

    $(function () {
        $(window).scroll(function () {//為頁面添加頁面滾動監聽事件
            var wst = $(window).scrollTop(); //滾動條距離頂端值
            for (var i = 1; i < 6; i++) {             //加循環
                if ($("#f" + i).offset().top <= wst + 10) { //判斷滾動條位置
                    $('#nav div').css("background-color", "white");
                    $(".f" + i).css("background-color", "red");
                }
            }
        });
        $('#nav div').click(function () {
            $('html,body').animate({scrollTop: $("#" + this.className).offset().top}, 500);
//          $("#" + this.className)[0].scrollIntoView(true);//h5 scrollIntoView()

        });
    });

全部代碼

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>h5之scrollIntoView控制頁面元素滾動</title>
    <style>

        /*scrollIntoView*/
        #myDiv {
            height: 900px;
            background-color: gray;

        }

        #roll_top {
            height: 900px;
            background-color: green;
            color: #FFF;
            font-size: 50px;
            position: relative;
        }

        #bottom {
            position: absolute;
            display: block;
            left: 0;
            bottom: 0;
        }

        /*scroll*/
        .main div {
            height: 1000px;
            width: 300px;
            margin: 20px;
            background-color: #C0C0C0;
        }

        #nav {
            position: fixed;
            width: 100px;
            height: 200px;
            top: 40%;
            right: 10px;
        }

        #nav div {
            cursor: pointer;
            text-align: center;

        }
    </style>
</head>

<body>
<div>
    <h2>scrollIntoView</h2>
    <button id="roll1">scrollIntoView(false)</button>
    <button id="roll2">scrollIntoView(true)</button>

    <div>
        <div id="myDiv"></div>
        <div id="roll_top">
            scrollIntoView(ture)元素上邊框與視窗頂部齊平
            <span id="bottom">scrollIntoView(false)元素下邊框與視窗底部齊平</span>
        </div>
    </div>
</div>

<div>
    <h2>scroll</h2>
    <div id="nav">
        <div class="f1">floor1</div>
        <div class="f2">floor2</div>
        <div class="f3">floor3</div>
        <div class="f4">floor4</div>
        <div class="f5">floor5</div>
    </div>
    <p>
        頁面結構
    </p>
    <div class="main">
        <div id="f1">測試1</div>
        <div id="f2">測試2</div>
        <div id="f3">測試3</div>
        <div id="f4">測試4</div>
        <div id="f5">測試5</div>
    </div>
</div>

<script>
    window.onload = function () {
        /*
         如果滾動頁面也是DOM沒有解決的一個問題。為了解決這個問題,瀏覽器實現了一下方法,以方便開發人員如何更好的控制頁面的滾動。
         在各種專有方法中,HTML5選擇了scrollIntoView()作為標準方法。scrollIntoView()可以在所有的HTML元素上調用,
         通過滾動瀏覽器窗口或某個容器元素,調用元素就可以出現在視窗中。如果給該方法傳入true作為參數,或者不傳入任何參數,
         那么窗口滾動之后會讓調動元素頂部和視窗頂部盡可能齊平。如果傳入false作為參數,調用元素會盡可能全部出現在視口中(可能的話,調用元素的底部會與視口的頂部齊平。)不過頂部不一定齊平.
         */

        document.querySelector("#roll1").onclick = function () {
            document.querySelector("#roll_top").scrollIntoView(false);
        };
        document.querySelector("#roll2").onclick = function () {
            document.querySelector("#roll_top").scrollIntoView(true);
        };
    }
</script>

<script src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
    $(function () {
        $(window).scroll(function () {//為頁面添加頁面滾動監聽事件
            var wst = $(window).scrollTop(); //滾動條距離頂端值
            for (var i = 1; i < 6; i++) {             //加循環
                if ($("#f" + i).offset().top <= wst + 10) { //判斷滾動條位置
                    $('#nav div').css("background-color", "white");
                    $(".f" + i).css("background-color", "red");
                }
            }
        });
        $('#nav div').click(function () {
            $('html,body').animate({scrollTop: $("#" + this.className).offset().top}, 500);
//          $("#" + this.className)[0].scrollIntoView(true);//h5 scrollIntoView()

        });
    });
</script>
</body>
</html>
View Code

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()