webapp弹层


</pre><pre name="code" class="html">/*
* Zepto 1.1.4
*
* wangxianfa
*
* 2015-07-03
*
* */
(function ($) {
    function Popup(option) {

        this.option = $.extend({
            target: $('<div></div>'),
            isFixed: true,
            isMask: true,
            maskClick: false
        }, option || {});

    }

    Popup.prototype = {

        dom: function () {
            return this.option.target;
        },

        show: function (fn) {
            var ml,
                mt;

            if (this.option.isMask) {
                this.option.mask
                    .show()
                    .css({
                        opacity: '.5'
                    });
            }

            this.option.target.show();

            ml = -this.option.target.width() / 2;
            mt = -this.option.target.height() / 2;

            if (this.option.isFixed) {
                this.option.target.css('position', 'fixed');
            } else {
                mt += document.getElementsByTagName('body')[0].scrollTop;

            }
            this.option.target.css({
                marginLeft: ml,
                marginTop: mt
            });

            if (typeof fn === 'function') {
                fn();
            }
        },

        hide: function (fn) {

            this.option.target.hide();
            this.option.mask.hide();

            if (typeof fn === 'function') {
                fn();
            }
        },
        basic: function () {

            var self = this,
                body = $('body');

            this.option.target
                .hide()
                .css({
                    left: '50%',
                    top: '50%',
                    zIndex: 999
                });

            body.append(this.option.target);

            if (this.option.isMask) {
                self.option.mask = $('<div></div>');
                self.option.mask
                    .hide()
                    .addClass('popupMask')
                    .css({
                        left: 0,
                        top: 0,
                        width: '100%',
                        height: '100%',
                        backgroundColor: '#000',
                        zIndex: 998,
                        position: 'fixed'
                    });
                body.append(self.option.mask);
            }

            this.dom().find('*').each(function () {
                if ($(this).attr('data-lightbox') === 'close') {
                    $(this).bind('click', function () {
                        self.hide();
                    })
                }
            });

            if (self.option.maskClick) {
                self.option.mask.on('click', function () {
                    self.hide();
                });
            }
        }
    };

    window.Popup = Popup;

})(Zepto);



实例化

var lightBox = function (){ 
        var lightbox = new Popup({
            target: $('.weixin').clone(),
            isMask: true,
            isFixed: true,
            maskClick: true
        });
        lightbox.basic();

        return {
            show: function(callback) {
                lightbox.show(callback);
            },
            hide: function(callback) {
                lightbox.hide(callback);
            },
            dom: function(){
                return lightbox.dom();
            }
        }
    }();




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值