jquery - 消息菜单栏左上角显示的消息数

标签 jquery css ajax

抱歉,如果我没有向您展示完整的代码,因为我不确定如何实现它。我看了一下facebook的消息通知框,如果有新的最近消息,在消息图标的右上角,一个红色的数字会位于那里并覆盖消息框,当你点击这个数字时,它就会消失,新的消息显示给你,我想知道如何实现它,当更多新消息出现时,指示将再次出现多少消息的数字,他们是否使用 ajax 自动刷新 div 来实现?所以假设我有以下消息框:

     <style>
    #messsge{width:100px;height:50px;border: 1px soild #000000;}
    #number{color:#ff0000;position:abosolute; z-index:999;top:-40px;left:-90px;}
    </style>
    <a href="#"><div id="message"></div></a><span id="number">3</span>
  <script>
   $("#message").click(function (e) { 
     e.preventDefault();
     $("#number").hide();
        });
     var auto_refresh = setInterval(function (){
  $('#number').load('number.php').fadeIn("slow");
   }, 1000);//not exactly sure how to refresh the number, I assume probably don't need to retrieve it from another php file.


  </script>

我不确定我是否做了正确的事情,任何人都可以帮助我,非常感谢!

最佳答案

你可以尝试类似的东西

在你的 number.php 中

//get the userid or some id from which you can specify the user from which the request is coming 
//e.g. $id=$_SESSION['CurrentUserID']
//apply some logic to determine if the user has new messages if he has some new msgs
$hasNewMsg="yes";
//calculate the number of messages 
$newMsgs=5;
echo json_encode(new array("hasMsg"=>$hasNewMsg,"newMsgs"=>$newMsgs));

//and if no new msgs
echo json_encode(new array("hasMsg"=>"no"));

客户端

<style>
    #messsge{width:100px;height:50px;border: 1px soild #000000;}
    #number{color:#ff0000;position:abosolute; z-index:999;top:-40px;left:-90px;}
    </style>
    <a href="#"><div id="message"></div></a><span id="number">3</span>

javascript/jquery

$(function(){
$("#message").click(function (e) { 
     e.preventDefault();
     $("#number").hide();
        });

var auto_refresh = setInterval(function (){

$.ajax({
type:"POST",
dataType:"json",
success:function(data){
if(data.hasMsg=="yes"){
$("#number").text(data.newMsgs).fadeIn('slow');
}
else//do nothing    
},
error:function(jxhr){
console.log(jxhr.responseText);
}   
});    
});    
});

免责声明:我不熟悉 php 所以...

关于jquery - 消息菜单栏左上角显示的消息数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7945252/

相关文章:

jquery - 需要 ('jquery' )未使用 Elixir/Gulp 定义

javascript - jquery水平滚动下一项按钮

html - 我需要帮助找到该函数的名称

html - 我如何根据另一个 div 的高度动态调整一个 div 中的 4 张图像?

php - PayPal API 上下文结帐错误

javascript - 如何在循环内使用 jQuery when 函数?

javascript - else 语句逻辑未运行

javascript - 如何计算可变高度

jquery - 当 .off() 嵌套在 .on() 内部时使用

Javascript - jquery, Accordion