代码之家  ›  专栏  ›  技术社区  ›  ryan

Jquery多个通知消息

  •  1
  • ryan  · 技术社区  · 16 年前

    我正在尝试创建stackoverflow类型的通知消息。我从其他帖子中找到了所需的大部分代码,但如果使用多个通知,则无法关闭邮件。理想情况下,如果三个盒子中的第二个盒子关闭了,那么它就会消失,而第三个盒子将取代第二个盒子

        <div class="message" style="display: none;">
            <span>Hey, This is my Message.</span>
            <a href="#" class="close-notify">X</a>
        </div>
    
         <div class="message" style="display: none;">
            <span>Hey, This is my Message.</span>
            <a href="#" class="close-notify">X</a>
        </div>
    
         <div class="message" style="display: none;">
            <span>Hey, This is my Message.</span>
            <a href="#" class="close-notify">X</a>
        </div>
    

    CSS

    div.message {
        font-family:Arial,Helvetica,sans-serif;
        float: left;
        left:0px;
        width:100%;
        height:15px;
        text-align:center;
        font-weight:bold;
        font-size:100%;
        color:white;
        padding:10px 0px 10px 0px;
        background-color:#8E1609;
    }
    
    .message span {
        text-align: center;
        font-size:1.1em;
        width: 95%;
        float:left;
    }
    
    .close-notify {
        white-space: nowrap;
        top: 0px;
        font-size:1.1em;
        float:right;
        margin-right:10px;
        margin-top: -15px;
        color:#fff;
        text-decoration:none;
        border:2px #fff solid;
        padding-left: 6px;
        padding-right: 6px;
    
    }
    

    JQuery公司

    <script type="text/javascript">
        $(document).ready(function() {
            $(".message").fadeIn("slow");
            $(".message a.close-notify").live('click', function() {
                $(this).parent().fadeOut('fast', function() { $(this).parent().remove(); });
                return false;
            });
        });
     </script>
    
    2 回复  |  直到 12 年前
        1
  •  1
  •   karim79    16 年前

    尝试不返回false并使用preventDefault停止跟踪链接,还尝试修改选择器以使用 parent/child ,并使用 closest 而不是fadeOut的匿名函数中的父函数:

    <script type="text/javascript">
        $(document).ready(function() {
            $(".message").fadeIn("slow");
            $(".message > a.close-notify").live('click', function(e) {
                e.preventDefault();
                $(this).parent().fadeOut('fast', function() { $(this).closest('.message').remove(); });
            });
        });
     </script>
    
        2
  •  -1
  •   jacob    16 年前

    如果问题是消息的顺序,则可以使用 z-index . 为每个新消息增加一个变量,并将其分配给消息的z索引。