weixin_33670713 2017-01-30 15:38 采纳率: 0%
浏览 16

淡出所有div?

I have built a function that will display a notification each time an ajax request is send and I just call the showAjaxAlert() function :

function showAjaxAlert(message, type) {
    var rand = randomNumber(100000, 1000000);
    document.getElementById('notification_area').innerHTML = document.getElementById('notification_area').innerHTML + '<div class="ajax-notification ajax-notification-' + type + ' ajax-notification-' + rand + ' alert">' + message + '</div>';
    $('.ajax-notification-' + rand).delay(500).fadeOut('slow');
}

I use a random number for each div to keep track of what im fading out, here is the random number function:

function randomNumber(min, max)
{
    return Math.floor(Math.random()*(max-min+1)+min);
}

I store all my ajax notifications in 1 parent div with the id of "notification_area"

<div id="notification_area"></div>

Here is my CSS:

#notification_area {
    position: fixed;
    top: 2em;
    opacity: 0.8;
    right: 2em;
}

.ajax-notification{
    color: #fff;
    background-color: red;
}

But the issue is, if the first one hasn't faded before another alert shows, it stays there forever and only the most recent alert fades out and its like I have to wait for the previous alert to fade out before I can send another ajax alert. Why is this happening, and is there any way to fix it?

  • 写回答

3条回答 默认 最新

  • weixin_33708432 2017-01-30 15:51
    关注

    Since you're using jQuery you could use append() instead of innerHTML so it will just add the new notification without touching the old ones so the delay will work fine, check the example below.

    Hope this helps.

    function showAjaxAlert(message, type) {
      var rand = randomNumber(100000, 1000000);
    
      $('#notification_area').append('<div class="ajax-notification ajax-notification-' + type + ' ajax-notification-' + rand + ' alert">' + message + '</div>');
    
      $('.ajax-notification-' + rand).delay(1000).fadeOut('slow');
    }
    
    function randomNumber(min, max){
      return Math.floor(Math.random()*(max-min+1)+min);
    }
    
    $('#show').on('click', function(){
      showAjaxAlert('Test Message','Success');
    })
    #notification_area {
      position: fixed;
      top: 2em;
      opacity: 0.8;
      right: 2em;
    }
    
    .ajax-notification{
      color: #fff;
      background-color: red;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    <div id="notification_area"></div>
    <button id="show">Show notification</button>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题