2013-01-17 52 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的確很重要,所以我不認爲圖像有什麼可做的。 –