nongfengBooth.js 6.7 KB
/**
 * 广告系统
 * zzf
 */
var DILIAD  = function(arges){
    var config,methods,template,controller,port,animation;
    //配置
    config = {
        httpUrl : "http://apd.nong12.com/apd/getJsonp",//请求地址
        httpId : 0,//请求ID
        httpDataType : "jsonp",//请求数据类型
        httpType : "GET",//请求数据方式
        showType : ["roll", "vertical", "ordinary"],//展示数据方式
        view : ""//当前显示对象
    };

    //数据获取
    methods = {
        getData : function(){//获取数据
            var url = config.httpUrl +"?id="+ config.httpId;
            $.ajax({
                url : url,
                type : config.httpType,
                dataType : config.httpDataType,
                jsonp: 'callback',
                error : function(x, t, e){
                    controller.errorView("服务器code:"+x.status);
                },
                success : function(response){
                    if(response.showType){
                        var page = config.showType[response.showType - 1];
                        animation[page](response);
                    }
                }
            });
        }
    };

    //动画渲染样式
    animation = {
        arges : {
            width : 0,
            length : 0,
            direction : "left",
            dir : true,
            time : null,
            index : 0
        },
        roll : function(data){//横滚
            var view,length,width;
            view = template.lsit(data.data);
            this.calculate(view, data);
            length = data.data.length;
            width = length * data.width;
            config.view.find(".ad-cont").width(width);
            this.slide(data.width, length, "left");
        },
        vertical : function(data){//垂直
            var view,length,height;
            view = template.lsit(data.data);
            this.calculate(view, data);
            length = data.data.length;
            height = length * data.height;
            config.view.find(".ad-cont").height(height);
            this.slide(data.height, length, "top");
        },
        upPage : function(index){
            var view = config.view.find(".ad-page");
            view.find("span").removeClass("on");
            view.find("span").eq(index).addClass("on");
        },
        ordinary : function(data){//图片
            var view;
            view = template.view(data.img);
            this.calculate(view, data);
        },
        slide : function (width, length, direction) {//滑动
            this.arges.index = 0;
            this.arges.width = width;
            this.arges.length = length;
            this.arges.direction = direction;
            this.arges.dir = true;
            this.setTime();
            controller.event();
        },
        sport : function(istime){
            var opt = {};
            opt[animation.arges.direction] = -(animation.arges.index * animation.arges.width);
            config.view.find(".ad-cont").stop().animate(opt, 800);
            animation.upPage(animation.arges.index);
            if(istime){this.setTime()};
        },
        setTime : function(){
            animation.arges.time = setInterval(function(){
                if((animation.arges.index < (animation.arges.length - 1)) && animation.arges.dir){
                    animation.arges.index ++ ;
                }else{
                    if(animation.arges.index <= 0){
                        animation.arges.index ++ ;
                        animation.arges.dir = true;
                    }else{
                        animation.arges.dir=false;
                    };
                    animation.arges.index -- ;
                }
                animation.sport();
            }, 3000);
        },
        calculate : function(view, data){//设置数据
            config.view.html($(view));
            config.view.width(data.width).height(data.height);
            config.view.find("li").width(data.width).height(data.height);
            config.view.find(".ad-page").css("margin-left", -config.view.find(".ad-page").width()/2);
            try{
                $(".ad-roll img").imageLoad(0);
            }catch (error){};
        }
    };

    //模板定义
    template = {
        lsit : function(data){//多图展示类型
            var sub = "";
            var view = "<div class='ad-cont'><ul>";
            for(i in data){
                if("0" === i){
                    sub += "<span class='on'></span>";
                }else {
                    sub += "<span></span>";
                }
                view += '<li><a href="'+data[i].link+'"><img src="http://static.nong12.com/static/newStatic/pnr/images/blank.gif" data-original="'+data[i].image+'"  /></a><p>'+(data[i].text ? data[i].text : "")+'</p></li>';
            }
            view += '</ul></div><div class="ad-page">'+sub+'</div>';
            return view;
        },
        view : function(data){//单图展示类型
            var view = '<div class="ad-cont"><a href="'+data.link+'"><img src="'+data.image+'" /></a></div>';
            return view;
        }
    };

    //控制器
    controller = {
        errorView : function(text){//异常处理
            config.view.html(text);
        },
        init : function(){//初始化
            if(arges.id && arges.viewId){
                config.httpId = arges.id;
                config.view = $("#"+arges.viewId);
                config.view.addClass("ad ad-roll");
                methods.getData();
            }else{
                this.errorView("请绑定视图ID 或者 传入广告ID");
            };
            $("head").append('<link type="text/css" rel="stylesheet" href="http://static.nong12.com/static/newStatic/pnr/nongfengBooth/nongfengBooth.css"/>');
        },
        event : function(width, direction){
            var view = config.view.find(".ad-page");
            view.on("click", "span", function(){
                animation.arges.index = $(this).index();
                clearInterval(animation.arges.time);
                animation.sport(true);
            });
        }
    };

    //外部接口
    port = {};

    controller.init();

    return port;
};

$(function(){
    //首页轮播
    var indexBanner = new DILIAD({
        id : "33333",
        viewId : "banner"
    });
    //1楼
    var index1 = new DILIAD({
        id : "90001",
        viewId : "species1"
    });
    //2楼
    var index2 = new DILIAD({
        id : "90002",
        viewId : "species2"
    });
    //3楼
    var index3 = new DILIAD({
        id : "90003",
        viewId : "species3"
    });
    //4楼
    var index4 = new DILIAD({
        id : "90004",
        viewId : "species4"
    });
    //5楼
    var index5 = new DILIAD({
        id : "90005",
        viewId : "species5"
    });
});