2013-08-23 148 views
0

我從http://www.saaraan.com/2013/04/creating-shout-box-facebook-style 加入此留言框它的現場演示可以在這裏看到http://www.saaraan.com/2013/04/creating-shout-box-facebook-style爲什麼這個留言框滑塊不能正常工作?

我所擁有的一切,除了滑塊本身工作正常。每次我嘗試向上滾動時,它都會自動向下滾動。它不會停留在上面的位置。我認爲問題在這裏。

// load messages every 1000 milliseconds from server. 
load_data = {'fetch':1}; 
window.setInterval(function(){ 
$.post('shout.php', load_data, function(data) { 
$('.message_box').html(data); 
var scrolltoh = $('.message_box')[0].scrollHeight; 
$('.message_box').scrollTop(scrolltoh); 
}); 
}, 1000); 

//method to trigger when user hits enter key 
$("#shout_message").keypress(function(evt) { 
if(evt.which == 13) { 
var iusername = $('#shout_username').val(); 
var imessage = $('#shout_message').val(); 
post_data = {'username':iusername, 'message':imessage}; 

//send data to "shout.php" using jQuery $.post() 
$.post('shout.php', post_data, function(data) { 

//append data into messagebox with jQuery fade effect! 
$(data).hide().appendTo('.message_box').fadeIn(); 

//keep scrolled to bottom of chat! 
var scrolltoh = $('.message_box')[0].scrollHeight; 
$('.message_box').scrollTop(scrolltoh); 

//reset value of message box 
$('#shout_message').val(''); 

更具體地說這裏

var scrolltoh = $('.message_box')[0].scrollHeight; 
$('.message_box').scrollTop(scrolltoh); 

這裏

//keep scrolled to bottom of chat! 
var scrolltoh = $('.message_box')[0].scrollHeight; 
$('.message_box').scrollTop(scrolltoh); 

我已經改變了0到1等數字和它修復滾動工作的權利,但它並不顯示最新的留言,它會顯示留言25,這是刪除前最後看到的留言。林不知道這是否有道理,但任何幫助將是偉大的。

從頂部的第一個鏈接顯示整個代碼,第二個鏈接顯示的例子

回答

0

試試這個代碼,我沒有測試它。我希望它會起作用。

window.setInterval(function() { 
    $.post('shout.php', load_data, function(data) { 
    var old_data = $(".message_box").html(); 
    if (old_data != data) { 
     $(".message_box").html(data); 

      // get scrollHeight 
     var scrollHeight = $(".message_box").get(0).scrollHeight, 
      // get current scroll position 
      scrollTop  = $(".message_box").scrollTop(), 
      // calculate current scroll percentage 
      percentage = Math.round((100/scrollHeight) * scrollTop);; 

     // make sure user is not scrolled to top 
     if (percentage > 80) { 
     $(".message_box").scrollTop(scrollTop); 
     } 
    } 
    }); 
}, 1000); 
相關問題