2013-08-23 90 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); 
相关问题