2013-01-17 55 views
0

不工作这是我在我的JS JS文件滚动顶值=滚动身高在Chrome

function refreshChat() 
    { 
     //speed up by selecting the div only once 
     var shoutbox = $("#shoutbox"); 

     //get the height of the scroll (if any) 
     var oldScrollH = shoutbox.attr("scrollHeight") - 20; 

     //the ajax request 
     $.ajax({ 
     url: 'shoutbox/update.php', 
     //disable cache 
     cache: false, 
     success: function(html) { 
      //update the shoutbox 
      shoutbox.html(html); 
      //get the heigth of the scroll after the update 
      var newScrollH = shoutbox.attr("scrollHeight") - 20; 
      if(newScrollH > oldScrollH) 
      { 
       //*move* the scroll down using an animation :) 
       shoutbox.animate({scrollTop: newScrollH}, 1); 
      } 
     } 
     }); 
    } 
    //set the refreshChat function to run every *refreshSeconds* 
    setInterval(refreshChat, refreshSeconds); 


}); 

它工作在Firefox和IE的罚款,但与谷歌浏览它不断的笔触。它会在页面加载时滚动到底部,但是当它调用功能refreshChat时,它会在div的一半左右移回。

我也有这个在我<head>

$(document).ready(function(){ 
     //speed up by selecting the div only once 
     var shoutbox = $("#shoutbox"); 

     //get the height of the scroll (if any) 
     var oldScrollH = shoutbox.attr("scrollHeight"); 

     //the ajax request 
     $.ajax({ 
     url: 'shoutbox/update.php', 
     //disable cache 
     cache: false, 
     success: function(html) { 
      //update the shoutbox 
      shoutbox.html(html); 
      //get the heigth of the scroll after the update 
      var newScrollH = shoutbox.attr("scrollHeight"); 
      if(newScrollH > oldScrollH) 
      { 
       //*move* the scroll down using an animation :) 
       shoutbox.animate({scrollTop: newScrollH}, 1); 
      } 
     } 
     }) 
     }); 

,这样它会自动加载上留言在页面加载,这可能与它有冲突?这似乎是合乎逻辑的,但我不希望用户必须等待3秒才能将最大加载速度提高。

+0

你可以在jsfiddle.net上展示你的问题吗? –

+0

@ExplosionPills我真的不知道如何,吼吼箱是PHP编码,所以我不知道我会怎么去做,因为我从来没有用过jsfiddle。 – kira423

+0

您是否在Chrome开发者工具中遇到任何错误? –

回答

1

您需要将字符串转换为int。

scrollHeight是自定义属性,我想它的动态添加,所以它必须字符串多数民众赞成为什么你需要将其转换为int。

parseInt(shoutbox.attr("scrollHeight"));

试试这个,希望这能解决它。

+0

没有工作,但代码中存在缺陷,所以我非常感谢。出于某种原因,我认为这不是因为笑脸图像 – kira423

+0

可能是因为你正在阅读自定义属性,所以只有attr的确很重要,所以我不认为图像有什么可做的。 –