亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 32
粉絲 0
評論 0
訪問量 27816
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
07-15作業(yè):懶加載的實現(xiàn)原理,選項卡的實現(xiàn)原理
Yx的博客
原創(chuàng)
1396人瀏覽過

1.懶加載的代碼實例演示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>懶加載代碼實例演示</title>
</head>
<body >
<div class="container" style="...">
    <img src="http://smashinghub.com/wp-content/uploads/2014/08/cool-loading-animated-gif-3.gif" alt="1" data-src="http://cdn.jirengu.com/book.jirengu.com/img/1.jpg">
    <img src="http://smashinghub.com/wp-content/uploads/2014/08/cool-loading-animated-gif-3.gif" alt="2" data-src="http://cdn.jirengu.com/book.jirengu.com/img/2.jpg">
    <img src="http://smashinghub.com/wp-content/uploads/2014/08/cool-loading-animated-gif-3.gif" alt="3" data-src="http://cdn.jirengu.com/book.jirengu.com/img/3.jpg">
    <img src="http://smashinghub.com/wp-content/uploads/2014/08/cool-loading-animated-gif-3.gif" alt="4" data-src="http://cdn.jirengu.com/book.jirengu.com/img/4.jpg">
    <img src="http://smashinghub.com/wp-content/uploads/2014/08/cool-loading-animated-gif-3.gif" alt="5" data-src="http://cdn.jirengu.com/book.jirengu.com/img/5.jpg">
    <img src="http://smashinghub.com/wp-content/uploads/2014/08/cool-loading-animated-gif-3.gif" alt="6" data-src="http://cdn.jirengu.com/book.jirengu.com/img/6.jpg">
    .
    .
</div>
<script>
    var container = document.createElement('div');
    var frag = document.createDocumentFragment();
    for (var i = 1; i <= 16; i++) {
        var imgUrl = 'http://cdn.jirengu.com/book.jirengu.com/img/' + i + '.jpg';
        var img = document.createElement('img');

        img.setAttribute('src', 'http://smashinghub.com/wp-content/uploads/2014/08/cool-loading-animated-gif-3.gif');
        img.setAttribute('data-src', imgUrl);
        img.setAttribute('style', 'width:600px;height:350px;margin: 5px;');
        frag.appendChild(img);
    }
    container.appendChild(frag);
    document.body.insertBefore(container, document.body.firstElementChild);

    window.addEventListener('scroll',  lazyLoaded, false);

    function lazyLoaded() {
        var scrollTop = document.documentElement.scrollTop;
        var clientHeight = document.documentElement.clientHeight;

        var imgArr = Array.prototype.slice.call(document.images, 0);
        imgArr.forEach(function (img) {
            if (img.offsetTop <= (scrollTop + clientHeight)) {
                img.setAttribute('src', img.dataset.src);
            }
        });
    }


    window.addEventListener('load', lazyLoaded, false);

</script>

</body>
</html>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例

2.選項卡的實現(xiàn) 并代碼實例演示

<head>
    <meta charset="UTF-8">
    <title>選項卡</title>
    <style>
        ul,li {
            margin: 0;
            padding: 0;
            list-style: none;
        }
        a {
            text-decoration: none;
        }
        a:hover {
            text-decoration-color: red;
            text-decoration-line: underline;
        }
        .tab-container {
            width: 300px;
            height: 200px;
        }

        .tab-nav {
            overflow: hidden;
        }

        .tab-nav ul li {
            float: left;
            width: 100px;
            height: 30px;
            line-height: 30px;
            text-align: center;
            cursor: pointer;
        }

        .active {
            background-color: lightcoral;
        }

        .tab-content .detail{
            line-height: 30px;
            min-height: 200px;
            padding-top: 10px;
            display: none;
        }

        .detail.active {
            display: block;
        }
    </style>
</head>
<body>
<div class="tab-container">
    <div class="tab-nav">
        <ul>
            <li class="active" data-index="1">選項卡1</li>
            <li data-index="2">選項卡2</li>
            <li data-index="3">選項卡3</li>
        </ul>
    </div>

    <div class="tab-content">
        <div class="detail active" data-index="1">
            <ul>
                <li><a href="">1.皚如山上雪,皎若云間月。</a></li>
                <li><a href="">2.聞君有兩意,故來相決絕。</a></li>
                <li><a href="">3.今日斗酒會,明旦溝水頭</a></li>
            </ul>
        </div>
        <div class="detail" data-index="2">
            <ul>
                <li><a href="">01.躞蹀御溝上,溝水東西流。</a></li>
                <li><a href="">02.凄凄復(fù)凄凄,嫁娶不須啼。</a></li>
                <li><a href="">03.愿得一心人,白頭不相離。</a></li>
            </ul>
        </div>
        <div class="detail" data-index="3">
            <ul>
                <li><a href="">001.竹竿何裊裊,魚尾何簁簁!</a></li>
                <li><a href="">002.男兒重意氣,何用錢刀為!</a></li>
                <li><a href="">003.出自兩漢卓文君的《白頭吟》</a></li>
            </ul>
        </div>
    </div>
</div>

<script>
    var tabNav = document.getElementsByClassName('tab-nav').item(0);
    var tabList = tabNav.firstElementChild.children;

    var tabArr = Array.prototype.slice.call(tabList, 0);
    var detail = document.getElementsByClassName('detail');
    var detailArr = Array.prototype.slice.call(detail, 0);
    tabNav.addEventListener('click', showDetail, false);

    function showDetail(evt) {
        tabArr.forEach(function (tab) { tab.classList.remove('active') });
        evt.target.classList.add('active');

        detailArr.forEach(function (detail) {detail.classList.remove('active')});
        detailArr.forEach(function (detail) {

            if (detail.dataset.index === evt.target.dataset.index) {
                detail.classList.add('active');
            }
        });
    }

</script>
</body>
</html>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例


批改狀態(tài):合格

老師批語:這二個作業(yè)的關(guān)鍵在于實現(xiàn)的原理和代碼的DOM結(jié)構(gòu)
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
作者最新博文
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費學(xué)