简体   繁体   English

为什么此喊话框滑块不能正常工作?

[英]Why wont this shout box slider work properly?

I added this shout box from http://www.saaraan.com/2013/04/creating-shout-box-facebook-style a live demo of it can be seen here http://www.saaraan.com/2013/04/creating-shout-box-facebook-style 我从http://www.saaraan.com/2013/04/creating-shout-box-facebook-style添加了此喊话框,可以在此处http://www.saaraan.com/2013/上看到它的实时演示04 / creating-shout-box-facebook-style

I have everything working properly except the slider itself. 除了滑块本身,其他所有东西都可以正常工作。 Every time I try to scroll up, it automatically scrolls back down. 每次尝试向上滚动时,它都会自动向下滚动。 It wont stay in the up position. 它不会停留在上位。 I think the problem is here. 我认为问题就在这里。

// 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('');

More specifically here 更具体地在这里

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

and here 和这里

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

I have changed the 0 to 1 and other numbers and it fixes the scroll to work right but it doesn't show the latest shout, it will show shout 25 which is the last shout to be seen before deletion. 我将0更改为1以及其他数字,它使滚动条可以正常工作,但是它没有显示最新的提示音,它将显示提示音25,这是删除前最后一次显示的提示音。 Im not sure if this makes any sense but any help would be great. 我不确定这是否有意义,但是任何帮助都会很棒。

The first link from top shows the whole code, the second link shows the example 顶部的第一个链接显示了整个代码,第二个链接显示了示例

Try this code, I didn't tested it. 试试这个代码,我没有测试过。 I hope it will work. 我希望它能起作用。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM