美文网首页
16.面向对象的编程——豆瓣电影

16.面向对象的编程——豆瓣电影

作者: 最爱喝龙井 | 来源:发表于2019-12-26 17:01 被阅读0次

案例是在github上发现的,地址https://github.com/5Iris5/doubanmovie
这里只粘贴一下js的代码,html、css就不粘了

var Helper = {
    isToEnd: function($viewport, $content) {
        return $viewport.height() + $viewport.scrollTop() + 10 > $content.height()
    },
    createNode: function(movie) {
        var template = `<div class="item">
        <a href="#">
        <div class="cover">
        <img src="" alt="">
            </div>
        <div class="detail">
        <h2></h2>
        <div class="extra"><span class="score"></span>分 / <span class="collect"></span>收藏</div>
        <div class="extra"><span class="year"></span> / <span class="type"></span></div>
        <div class="extra">导演: <span class="director"></span></div>
        <div class="extra">主演: <span class="actor"></span></div>
      </div>
      </a>
      </div>`
        var $node = $(template);
        $node.find('a').attr('href', movie.alt)
        $node.find('.cover img').attr('src', movie.images.medium )
        $node.find('.detail h2').text(movie.title)
        $node.find('.score').text(movie.rating.average )
        $node.find('.collect').text(movie.collect_count )
        $node.find('.year').text(movie.year)
        $node.find('.type').text(movie.genres.join(' / '))
        $node.find('.director').text(function() {
            var directorsArr = [];
            movie.directors.forEach(function(item) {
                directorsArr.push(item.name)
            })
            return directorsArr.join('、')
        })
        $node.find('.actor').text(function() {
            var actorsArr = [];
            movie.casts.forEach(function(item) {
                actorsArr.push(item.name)
            })
            return actorsArr.join('、');
        })
        return $node;
    }
}

var Top250 = {
    init: function() {
        this.$container = $('#top250');
        this.$content = $('.container');
        this.index = 0;
        this.isFinish = false;
        this.isLoading = false;
        this.bind();
        this.start();
    },
    bind: function() {
        var _this = this;
        _this.$container.scroll(function() {
            if(!_this.isFinish && Helper.isToEnd(_this.$container, _this.$content)) {
                _this.start()
            }
        })
    },
    start: function() {
        var _this = this;
        _this.getData(function(data) {
            _this.render(data)
        })
    },
    getData: function(callback) {
        var _this = this;
        if(_this.isLoading) return;
        _this.isLoading = true;
        _this.$container.find('.loading').show();
        $.ajax({
            url: 'http://api.douban.com/v2/movie/in_theaters?apikey=0df993c66c0c636e29ecbb5344252a4a',
            data: {
                start: _this.index || 0
            },
            dataType: 'jsonp'
        }).done(function(res) {
            _this.index += 20;
            if(_this.index >= res.total) {
                _this.isFinish = true;
            }
            callback&&callback(res);
        }).fail(function() {
            console.log('数据错误')
        }).always(function() {
            _this.isLoading = false;
            _this.$container.find('.loading').hide();
        })

    },
    render: function(data) {
        var _this = this;
        data.subjects.forEach(function(movie) {
            _this.$content.append(Helper.createNode(movie))
        })
    }
}
$(function() {
    Top250.init()
})

相关文章

  • 16.面向对象的编程——豆瓣电影

    案例是在github上发现的,地址https://github.com/5Iris5/doubanmovie这里只...

  • 面向对象基础

    面向对象编程包括: 面向对象的分析(OOA) 面向对象的设计(OOD) 面向对象的编程实现(OOP) 面向对象思想...

  • 面向对象_初识

    目录 面向对象编程介绍 类与对象介绍 私有属性与私有方法 面向对象编程 1. 面向对象编程介绍 面向对象编程:Ob...

  • PHP全栈学习笔记8

    面向对象的基本概念,面向对象编程,oop,面向对象,面向对象的分析,面向对象的设计,面向对象的编程,什么是类。 类...

  • PHP全栈学习笔记8

    面向对象的基本概念,面向对象编程,oop,面向对象,面向对象的分析,面向对象的设计,面向对象的编程,什么是类。 类...

  • 谈谈面向对象编程

    何为面向对象编程 面向对象编程简介 面向对象编程(Object-oriented Programming,缩写:O...

  • 2017-08-14

    面向对象编程用对象的思想去写代码,就是面向对象编程-面向过程-面向对象面向对象编程的特点1.抽象 抽取一样的东西...

  • 面向对象浅析

    ### 面向对象编程和面向对象编程语言 面向对象编程的英文缩写是 OOP,全称是 Object Oriented ...

  • 面向对象编程,类和对象

    面向对象编程 Java是面向对象的一门编程语言,所以余姚使用者具备面向对象编程的思想。 那么,什么是面向对象编程呢...

  • python-day14

    一、面向对象编程 编程思想:1.面向对象编程 --> 算法,逻辑2.函数式编程 --> 函数3.面向对象编程 ...

网友评论

      本文标题:16.面向对象的编程——豆瓣电影

      本文链接:https://www.haomeiwen.com/subject/hqrloctx.html