首页 > 代码库 > 常用的public.js

常用的public.js

var publicFunc = {

    back : function(num){

        var _num = num ? num : -1;

        if(navigator.userAgent.indexOf(‘Android‘) > -1){

            window.callAndroid.back(_num);

        }else{

            if(_num ==1 || _num ==3){

                var arr = new Array();

                arr[1] = ‘/bao/index‘;

                arr[2] = ‘/task/index‘;

                arr[3] = ‘/user/index‘;

                window.location.href = http://www.mamicode.com/arr[_num];

            }

            window.history.go(_num);

        }

    }

};

jQuery(function ($) {

    $("img.lazy").lazyload();

});

 

/**

 * author yangchengsheng@eoemarket.com

 * desc 消息提醒

 * used  $.messageAlert($message)

 */

jQuery.extend({

    messageAlert: function ($message) {

        if ($(".message_alert").length < 1) {

            var div = $(‘<div></div>‘);

            div.addClass(‘message_alert‘);

            div.appendTo(‘body‘);

        }

        $(".message_alert").html("<span>" + $message + "</span>");

        $(".message_alert").fadeIn();

        setTimeout(function () {

            $(".message_alert").fadeOut();

            $(".message_alert").html("");

        }, 3000)

        return true;

    },

    /**

     * desc 拍照

     * used  $.takePhoto(type,param,callback)

     */

    takePhoto: function (type,param,callback) {

        if ($(".setface_alert").length < 1) {

            var html = ‘<div class="setface_alert">‘ +

                ‘<span id="take_phone_alert">拍照</span>‘ +

                ‘<span class="album" id="select_phone_alert">从相册中选择</span>‘ +

                ‘<span class="cancel">取消</span></div>‘ +

                ‘<div class="shade"></div>‘;

            $(‘body‘).append(html);

            $(".setface_alert .cancel").unbind("click");

            $(".setface_alert .cancel").on("click", function () {

                $(".setface_alert").hide();

                $(".shade").hide();

            });

        };

        $("#take_phone_alert").unbind("click");

        $("#select_phone_alert").unbind("click");

        //调用Android拍照

        $("#take_phone_alert").on("click", function () {

            $(".setface_alert").hide();

            $(".shade").hide();

            $(this).unbind("click");

            window.callAndroid.takePhoto(type, param, callback);

 

        });

        //调用Android相册

        $("#select_phone_alert").on("click", function () {

            $(".setface_alert").hide();

            $(".shade").hide();

            $(this).unbind("click");

            window.callAndroid.selectPhoto(type, param, callback);

        });

        $(".setface_alert").fadeIn();

        $(".shade").show();

    },

    /**

     * 事件统计

     * @param tag

     * @param label

     * @param duration

     */

    onEvent:function (tag, label, duration) {

        prompt("event", JSON.stringify({tag:tag,label:label, duration:duration}));

    }

});

 

jQuery.fn.extend({

    /**

     * desc 分页

     * used  $.(select).pager(url,param)

     */

    pager: function (url, param) {

        var _this = this;

        var _param = param ? param : new Object();

        var _page = _param.page ? _param.page : 2;

        var _limit = _param.limit ? _param.limit : 20;

        var is_load_more = false;

        if (_this.children().size() >= _limit) {

            if ($(".Loading").length < 1) {

                $(‘body‘).append(‘<div class="Loading">正在加载更多...</div>‘);

            }

            is_load_more = true;

        }

        $(window).scroll(function () {

            if (_page < 51 && is_load_more == true) {

                //拖到页底时,自动AJAX加载新数据

                var pageH = $(document.body).height();

                var winH = $(window).height();//可视区高

                var scrollT = $(window).scrollTop();

                var pageRest = (pageH - winH - scrollT) / winH;

                if (pageRest>0.02) {

                    is_load_more = false;

                    _param.page = _page;

                    _param.limit = _limit;

                    $.get(url, _param, function (data) {

                        if ($.trim(data)) {

                            _page += 1;

                            _this.append(data);

                            is_load_more = true;

                            $("img.lazy").lazyload();

                        } else {

                            $(‘.Loading‘).hide();

                        }

                    });

                }

            } else {

                $(‘.Loading‘).hide();

            }

        });

    }

});

常用的public.js