1

我正在使用Pnotify发送通知,但通知仅显示在非常特定的位置。我想将它们附加到我页面的标题类中,但我不知道该怎么做。

JS:

function showNotify(data){
    var notice = $.pnotify({
        type: 'success',
        delay: 300000,
        addclass: (isDevice)? 'body-device stack-topleft':'stack-topleft',
        mouse_reset: false
    }).click(function(e){
        notice.pnotify_remove();
    });
}

我想将我的 pnotify 附加到的 HTML 是:

<div class="header">
    <div class="col-md-12" data-bind="with: activeRoute"></div>
</div>
4

2 回答 2

1

来自http://sciactive.com/pnotify/#demos-simple

function show_stack_context(type) {
    if (typeof stack_context === "undefined") stack_context = {
        "dir1": "down",
        "dir2": "left",
        "context": $("#stack-context")
    };
    var opts = {
        title: "Over Here",
        text: "Check me out. I'm in a different stack.",
        stack: stack_context
    };
    switch (type) {
    case 'error':
        opts.title = "Oh No";
        opts.text = "Watch out for that water tower!";
        opts.type = "error";
        break;
    case 'info':
        opts.title = "Breaking News";
        opts.text = "Have you met Ted?";
        opts.type = "info";
        break;
    case 'success':
        opts.title = "Good News Everyone";
        opts.text = "I've invented a device that bites shiny metal asses.";
        opts.type = "success";
        break;
    }
    $.pnotify(opts);
}

所以你会

function showNotify(data){
    if (typeof stack_context === "undefined") stack_context = {
        "dir1": "down",
        "dir2": "left",
        "context": $(".header")
    };
    var opts = {
        title: "Over Here",
        text: "Notification",
        type: 'success',
        delay: 300000,
        addclass: (isDevice)? 'body-device stack-topleft':'stack-topleft',
        mouse_reset: false
        stack: stack_context
    };
    var notice = $.pnotify(opts).click(function(e){
        notice.pnotify_remove();
    });
}
于 2014-04-02T20:34:46.387 回答
1

更新以解决如何将 pnotify 附加到标头类

在我重新阅读您的问题后,我假设您可能正在寻找类似此更新示例中的内容。数据属性下方提供了 pnotify 消息的标题和文本。

<div class="header" 
          data-title="notify 1" 
          data-text="text 1">Header with autowiring of pnotify</div>

<div class="header" 
          data-title="better notify 2" 
          data-text="other text 2">Header 2</div>

要将 pnotify 与标头类连接起来,您可以添加以下代码:

 function getNotifiyMessage(that){      
      var mytitle = $(that).attr("data-title" );
      var mytext = $(that).attr("data-text");          
      return { title: mytitle, text: mytext};
    };

    $(document).ready(function() {

          $(".header").click(
            function(){
              var notifyMessage = getNotifiyMessage(this);
              $.pnotify(notifyMessage);
            }
          );
    });    

较早的第一个答案

如果你的意思是这个基于样本的pnotify

<button class="btn btn-default source" 
      onclick="$.pnotify({ title: 'Regular Notice'
            , text: 'i am a note from pnotify'});"
  >Regular Notice</button>

您可以通过添加带有激活 pnotify 功能的 onclick 属性来创建 pnotify。请参阅 此示例下面的 data-attribute 提供了 pnotify 消息的标题和文本。

<div class="header" 
  onclick="$.pnotify({ title: 'Regular Notice', text: 'i am a note from pnotify'});">
  <div class="col-md-12" 
       data-bind="with: activeRoute">go click me to see pnotify</div>
</div>

请注意,上面的事件从内部 div 冒泡到外部 div,然后 pnotify 触发。

于 2014-04-02T20:09:49.477 回答