关闭通知弹出上单击通知外

问题描述 投票:0回答:1

我写了一些自定义notify.js触发一些通知,当一个产品被添加到购物车在WordPress网站。现在的问题是,我无法在通知外单击以关闭弹出。我试过的窗口中点击功能几个方面,但它did'nt为我工作。

(function(plugin) {

    var component;

    if (jQuery) {
        component = plugin(jQuery);
    }

    if (typeof define == "function" && define.amd) {
        define("notify", function(){
            return component || plugin(jQuery);
        });
    }

})
(function($){

    var containers = {},
        messages   = {},

        notify     =  function(options){

            if ($.type(options) == 'string') {
                options = { message: options };
            }

            if (arguments[1]) {
                options = $.extend(options, $.type(arguments[1]) == 'string' ? {status:arguments[1]} : arguments[1]);
            }

            return (new Message(options)).show();
        };

    var Message = function(options){

        var $this = this;

        this.options = $.extend({}, Message.defaults, options);

        this.uuid    = "ID"+(new Date().getTime())+"RAND"+(Math.ceil(Math.random() * 100000));
        this.element = $([

            '<div class="alert notify-message">',
                '<button type="button" class="close" aria-hidden="true">&times;</button>',
                '<div>'+this.options.message+'</div>',
            '</div>'

        ].join('')).data("notifyMessage", this);

        // status
        if(this.options.status == 'error') {
            this.options.status = 'danger';
        }

        this.element.addClass('alert-'+this.options.status);
        this.currentstatus = this.options.status;

        messages[this.uuid] = this;

        if(!containers[this.options.pos]) {
            containers[this.options.pos] = $('<div class="notify notify-'+this.options.pos+'"></div>').appendTo('body').on("click", ".notify-message", function(){
                $(this).data("notifyMessage").close();
            });
        }
    };


    $.extend(Message.prototype, {

        uuid: false,
        element: false,
        timout: false,
        currentstatus: "",

        show: function() {

            if (this.element.is(":visible")) return;

            var $this = this;

            containers[this.options.pos].css('zIndex', this.options.zIndex).show().prepend(this.element);

            var marginbottom = parseInt(this.element.css("margin-bottom"), 10);

            this.element.css({"opacity":0, "margin-top": -1*this.element.outerHeight(), "margin-bottom":0}).animate({"opacity":1, "margin-top": 0, "margin-bottom":marginbottom}, function(){

                if ($this.options.timeout) {

                    var closefn = function(){ $this.close(); };

                    $this.timeout = setTimeout(closefn, $this.options.timeout);

                    $this.element.hover(
                        function() { clearTimeout($this.timeout); },
                        function() { $this.timeout = setTimeout(closefn, $this.options.timeout);  }
                    );
                }

            });

            return this;
        },

        close: function(instantly) {

            var $this    = this,
                finalize = function(){
                    $this.element.remove();

                    if(!containers[$this.options.pos].children().length) {
                        containers[$this.options.pos].hide();
                    }

                    $this.options.onClose.apply($this, []);

                    delete messages[$this.uuid];
                };

            if(this.timeout) clearTimeout(this.timeout);

            if(instantly) {
                finalize();
            } else {
                this.element.animate({"opacity":0, "margin-top": -1* this.element.outerHeight(), "margin-bottom":0}, function(){
                    finalize();
                });
            }
        },

    });

    Message.defaults = {
        message: "",
        status: "default",
        timeout: 50000,
        pos: 'top-center',
        zIndex: 10400,
        onClose: function() {}
    };

    return $.notify = notify
});

我触发通知时产品添加到购物车像这样

var view_cart = jQuery('.woocommerce-message a.button').text();
  if (view_cart != '') {
     var siteUrl = "<?php echo SITE_URL ?>";
     jQuery.notify({

        // custom notification message
        message: "<i class='fa fa-check-circle'></i> Product added to cart successfully. <a href='"+siteUrl+"/cart/'>GO TO CART</a>",

        // 'default', 'info', 'error', 'warning', 'success'
        status: "success",

        // timeout in ms
        timeout: 5000,

        // 'top-center','top-right', 'bottom-right', 'bottom-center', 'bottom-left'
        pos: 'bottom-center',

        // z-index style for alert container
        zIndex: 10400,

        // Function to call on alert close
        onClose: function() {}

    });
}

需要关闭通知的通知外​​,当点击

javascript jquery wordpress
1个回答
0
投票

希望我的代码将帮助你。 我应该有一个“格”窗口的弹出,所有的处理我的js(与JQ LIB在我的例子)

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {

            //Execute on DOM ready
            $pop_btn = $('#popup-btn'); //Selects 1 Obj
            $pop_stuff = $('.popup-action'); //Selects 2 Obj

            //Binding Jq listeners
            $pop_btn.on('click', function( e ) {
                //This listener works only for static objects
                $pop_stuff.each(function() {
                    $(this).removeClass('el-hidden').addClass('el-shown');
                });
            })

            $(document).on('click', '#popup-listener-layer', function( e ) {
                //This listener works also for dinamically added objects (not needed in this case, just used to show in case)
                $pop_stuff.each(function(){
                    $(this).removeClass('el-shown').addClass('el-hidden');
                });
            });

        });
    </script>
    <style type="text/css">
        #page-title {
            position: absolute;
            top: 2.5%;
            font-size: 3vw;
        }

        #popup-listener-layer {
            position: absolute;
            top: 0; left: 0;
            width: 100%; height: 100%;
            background-color: rgba(0, 0, 0, 0.75);
            z-index: 1;
        }

        #popup-container {
            position: absolute;
            width: 50%; height: 50%;
            background-color: white;
            z-index: 2;
        }

        .center-orr {
            left: 50%;
            transform: translateX(-50%);
        }

        .center-vert {
            top: 50%;
            transform: translateY(-50%);
        }

        .center-middle {
            top: 50%; left: 50%;
            transform: translate(-50%, -50%);
        }

        .el-hidden {
            visibility: hidden;
        }

        .el-shown {
            visibility: visible;
        }

    </style>

</head>
<body>
    <p id = "page-title" class = "center-orr">Lorem ipsum Title</p>
    <p>Lorem ipsum</p>
    <p>Lorem ipsum</p>
    <p>Lorem ipsum</p>
    <p>Lorem ipsum Bla Bla Bla</p>
    <button id = "popup-btn" type="button">Open a Popup</button> 

    <!-- POPUP DIV -->
    <div id = "popup-listener-layer" class = "el-hidden popup-action"></div>
    <div id = "popup-container" class = "center-middle el-hidden popup-action">
        <p>Lorem ipsum popup</p>
        <p><bold>CLICK ANYWERE OUTSIDE ME TO CLOSE ME</bold></p>
    </div>
</body>
</html> 

只是听超越珀普一个层上,但主网页上面(我把它涂黑,但可以设置好的透明的“背景颜色:透明”属性)。 祝你有美好的一天 :) 饸饹

© www.soinside.com 2019 - 2024. All rights reserved.