Ajax请求电影列表

  <style>
        * {
            margin: 0;
            padding: 0;
        }

        :root {
            --theme: red;
        }

        .content {
            width: 1200px;
            margin: 0 auto;
            padding-top: 30px;

        }

        .nav {
            display: flex;
            justify-content: center;
        }

        .nav-item {
            width: 120px;
            line-height: 60px;
            border: 1px solid var(--theme);
            margin: 0 20px;
            text-align: center;
        }

        .nav-item.act {
            background-color: var(--theme);
            color: #fff;
        }

        .page {
            display: flex;
            flex-wrap: wrap;
        }

        .page span {
            width: 40px;
            line-height: 20px;
            border: 1px solid var(--theme);
            margin: 10px 10px;
            text-align: center;
        }

        .page span.act {
            background-color: var(--theme);
        }

        .main {
            display: flex;
            flex-wrap: wrap;
            margin-top: 50px;

        }

        .main .item {
            float: left;
            width: 22%;
            height: 200px;
            border: 1px solid #333;
            margin: 2% 1%;
            font-size: 14px;
        }

        .main .item img {
            float: left;
            width: 50%;
            height: 100%;
        }

        .mask {
            display: none;
            position: fixed;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, .5);
        }

        .mask-content {
            width: 400px;
            height: 300px;
            position: absolute;
            left: 50%;
            top: 50%;
            transform: translate(-50%, -50%);
            background-color: #fff;
            overflow: hidden;
            font-size: 14px;
        }

        .loading {
            display: none;
            width: 80px;
            height: 40px;
            margin: 0 auto;
            margin-top: 100px;
        }

        .loading span {
            display: inline-block;
            width: 8px;
            height: 100%;
            border-radius: 4px;
            background: lightgreen;
            -webkit-animation: load 1s ease infinite;
        }

        @-webkit-keyframes load {

            0%,
            100% {
                height: 40px;
                background: lightgreen;
            }

            50% {
                height: 70px;
                margin: -15px 0;
                background: lightblue;
            }
        }

        .loading span:nth-child(2) {
            -webkit-animation-delay: 0.2s;
        }

        .loading span:nth-child(3) {
            -webkit-animation-delay: 0.4s;
        }

        .loading span:nth-child(4) {
            -webkit-animation-delay: 0.6s;
        }

        .loading span:nth-child(5) {
            -webkit-animation-delay: 0.8s;
        }
    </style>
<body>
    <div class="content">
        <div class="nav">
            <div data-type="hot" class="nav-item act">
                正在上映
            </div>
            <div data-type="will" class="nav-item">
                即将上映
            </div>
            <div data-type="top" class="nav-item">
                top250
            </div>

        </div>

        <div class="page">
            <span>1</span>
            <span>2</span>
            <span>3</span>

        </div>
        <div class="main">
            <div class="item">
                <img src="" alt="">
                <p>名称</p>
                <p>时长</p>
                <p>类型</p>
                <p>平均分</p>
                <p>编剧</p>
                <p>导演</p>
            </div>
            <div class="item">

            </div>
            <div class="item">

            </div>
            <div class="item">

            </div>
            <div class="item">

            </div>
            <div class="item">

            </div>
            <div class="item">

            </div>
            <div class="item">

            </div>

        </div>
        <!-- 蒙层  -->
        <div class="mask">
            <div class="mask-content">
                <!-- <div class="text">
                    电影简介
                </div> -->
            </div>
            <div class="loading">
                <span></span>
                <span></span>
                <span></span>
                <span></span>
                <span></span>
            </div>

        </div>
    </div>
<script>
        //渲染页面
        // var xhr = new XMLHttpRequest();
        // xhr.onreadystatechange = function () {
        //     if (xhr.readyState == 4 && xhr.status == 200) {
        //         var res = JSON.parse(xhr.response).data;
        //         console.log(res);
        //         //渲染分页器
        //         var pagestr = ''
        //         for (var j = 1; j <= res.totalPage; j++) {
        //             pagestr += `<span>${j}</span>`
        //         }
        //         $('.page').html(pagestr);
        //         //渲染内容
        //         var str = '';
        //         for (var i = 0; i < res.list.length; i++) {
        //             str += `<div class="item">
        //         <img src="${res.list[i].large}"" alt="">
        //         <p>名称:${res.list[i].title}</p>
        //         <p>时长:${res.list[i].longtime}</p>
        //         <p>类型:${res.list[i].subtype}</p>
        //         <p>平均分:${res.list[i].rating_average}</p>
        //         <p>编剧:${res.list[i].scriptwriter}</p>
        //         <p>导演:${res.list[i].director}</p>
        //     </div>`
        //         }
        //         $('.main').html(str);
        //     }
        // }
        // xhr.open('GET', 'http://www.bufantec.com/api/douban/movie/in_theaters');
        // xhr.send(null);


        //初始化页面
        get(api.hot, render);


        //绑定导航的点击事件
        $('.nav').on('click', '.nav-item', function () {
            var type = $('.nav-item.act').attr('data-type');
            $(this).addClass('act').siblings().removeClass('act');
            console.log(type);
            get(api[type], render)
        })

        //分页器点击事件
        $('.page').on('click', 'span', function () {
            // 
            var type = $('.nav-item.act').attr('data-type');
            console.log(type);
            //点击的span页码,获取此时从哪一页开始
            var num = $(this).text();
            get(api[type] + `?start=${num}&limit=10`, render)
        })


        //点击卡片出现蒙层
        $('.main').on('click', '.item', function () {
            $('.mask').fadeIn().children('.mask-content').hide().next().show();
            var id = this.dataset.id;
            get(api.desc + `?mId=${id}`, function (res) {
                // 关闭loading 
                $('.loading').hide().prev().show();
                // 生成内容简介
                console.log(res);
                $('.mask-content').html(`<div class="text">
                    ${res.summary}
                </div>`)
            })
        })

        //点击蒙层消失
        $('.mask').click(function () {
            $(this).hide()
        })





        //封装请求
        function get(url, callback) {
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var res = JSON.parse(xhr.response).data;
                    // console.log(res);
                    callback(res)
                }
            }
            xhr.open('GET', url);
            xhr.send();
        }

        //封装渲染页面
        function render(res) {
            //渲染分页器
            var pagestr = ''
            for (var j = 1; j <= res.totalPage; j++) {
                pagestr += `<span>${j}</span>`
            }
            $('.page').html(pagestr);
            //当前页数高亮
            // pageNumber属性表示当前页码
            $('.page span').eq(res.pageNumber - 1).addClass('act').siblings().removeClass('act');
            //渲染内容
            var str = '';
            for (var i = 0; i < res.list.length; i++) {
                str += `<div data-id="${res.list[i].mId}" class="item">
                <img src="${res.list[i].large}"" alt="">
                <p>名称:${res.list[i].title}</p>
                <p>时长:${res.list[i].longtime}</p>
                <p>类型:${res.list[i].subtype}</p>
                <p>平均分:${res.list[i].rating_average}</p>
                <p>编剧:${res.list[i].scriptwriter}</p>
                <p>导演:${res.list[i].director}</p>
            </div>`
            }
            $('.main').html(str);
        }
    </script>

<script src="js/jquery-3.3.1.min.js"></script>(此文件是一个jquery插件,需要自行去网上下载)

    <script src="js/api.js"></script>(此js文件的内容是整理的api.内容如下:

var host = 'http://www........'

var api = {

    hot: host + '.........',

    will: host + '........',

    top: host + '.......',

    desc: host + '......'

}

)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值