樓梯跳躍代碼web

來源:互聯網
上載者:User

標籤:架構   ext   off   offset   list   char   doc   enter   idt   

<!DOCTYPE html><html><head lang="en">    <meta charset="UTF-8">    <title></title>    <style>        *{margin: 0;padding: 0;}        html,body{width: 100%;height: 100%;}        ul,ol{list-style: none;}        ul{width: 100%;height: 100%;}        ul li{width: 100%;height: 100%;}        ol{position: fixed;top: 100px;left: 100px;}        ol li{            width: 50px;            height: 50px;            border: 1px solid #000;            margin-top: -1px;            text-align: center;            font: 400 22px/50px "simsun";            cursor: pointer;        }    </style></head><body>    <ul id="ul">        <li>男裝區</li>        <li>女裝區</li>        <li>童裝區</li>        <li>孕裝區</li>        <li>老年裝區</li>    </ul>    <ol id="ol">        <li>男裝</li>        <li>女裝</li>        <li>童裝</li>        <li>孕裝</li>        <li>老年</li>    </ol></body></html><script>    //需求:給所有盒子添加顏色,點擊ol的li,頁面跳轉到ul中相應的li    //步驟:    //1、利用數組給所有盒子添加顏色    //2、緩動架構移動最頂端    //3、移動到指定位置(給ol中的li綁定索引值,擷取對應的ul中的li距離頂端的距離,賦值給target,好讓頁面跳轉    //4、螢幕滑動,記錄螢幕的位置    var ul = document.getElementById("ul");    var ol = document.getElementById("ol");    var ulLis = ul.children;    var olLis = ol.children;    var arr = ["pink","yellow","green","purple","orange"];    var timer = null,target = 0,leader = 0;    window.onscroll = function () {        //螢幕滑動,給螢幕目前所在的位置賦值。        leader = scroll().top;    }    for(var i=0;i<olLis.length;i++){        //為每一個盒子上色,ul和ol中的li對應顏色        ulLis[i].style.backgroundColor = arr[i];        olLis[i].style.backgroundColor = arr[i];        olLis[i].index = i;        olLis[i].onclick = function(){            //給目標位置賦值(小盒子對應的大盒子距離頂部的距離)            target = ulLis[this.index].offsetTop;            clearInterval(timer);            timer = setInterval(function(){                var step = (target-leader)/10;                step = step>0?Math.ceil(step):Math.floor(step);                leader = leader+step;                window.scrollTo(0,leader);                if(target == leader){                    clearInterval(timer);                }            },30)        }    }</script>

 

樓梯跳躍代碼web

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.