Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery scroll on div
    text
    copied!<p>I'm trying to set up a simple chat system with <code>jQuery</code> / <code>Ajax</code>, but I'm having a strange behaviour with the scroll part. When I post something, or when I receive something, the scroll goes up instead of going down as requestes. Here's the code :</p> <pre><code> $('form[name="iview-messages-chat"]').submit(function(e) { e.preventDefault(); var data = {}; data.msg = $('textarea', this).val(); data.time = $('#iview-messages-pane .iview-messages-item:last').attr('data-time'); data.uid = $('input[name="uid"]', this).val(); $.ajax({ type: 'POST', url: 'index.php?page=messages&amp;action=send', data: data, dataType: 'json', }).done(function(data) { $.each(data, function(index,value) { var nm = '&lt;div class="clearfix iview-messages-item" style="height:auto;min-height:60px;width:730px;margin:0;" data-cid="'+value.id+'" data-time="'+value.time+'"&gt;'; nm += '&lt;img style="margin-top: 5px;" src="'+value.picture+'" /&gt;'; nm += '&lt;div class="iview-messages-item-info" style="width:630px;"&gt;'; nm += '&lt;div class="iview-messages-item-info-row" style="width:inherit;"&gt;'; nm += '&lt;a href="'+value.profile+'"&gt;'+value.first_name+' '+value.last_name+'&lt;/a&gt;'; nm += '&lt;span style="float:right;font-size:11px;"&gt;'+value.elapsed_time+'&lt;/span&gt;'; nm += '&lt;/div&gt;'; nm += '&lt;div class="iview-messages-item-info-row"&gt;'; nm += '&lt;span&gt;'+value.content+'&lt;/span&gt;'; nm += '&lt;/div&gt;'; nm += '&lt;/div&gt;'; nm += '&lt;/div&gt;'; $('#iview-messages-pane').append(nm); }); $('form[name="iview-messages-chat"] textarea').val(''); if (!$.isEmptyObject(data)) { $('#iview-messages-pane').animate({scrollTop: $('#iview-messages-pane').height()}, 800); } }).fail(function() { console.log('error'); }); return false; }); </code></pre> <p><strong>markup</strong></p> <pre><code>div id="iview-messages-pane"&gt; &lt;div id="iview-messages-pane-handler" class="clearfix"&gt; &lt;div class="clearfix iview-messages-item" style="height:auto;min-height:60px;width:730px;margin:0;" data-cid="36" data-time="1340591805"&gt; &lt;img style="margin-top: 5px;" src="http://myiview.me/upload/1/4fda8a23cb3c3_200_200.jpg" /&gt; &lt;div class="iview-messages-item-info" style="width:630px;"&gt; &lt;div class="iview-messages-item-info-row" style="width:inherit;"&gt; &lt;a href="http://myiview.me/index.php?page=profile&amp;uid=1"&gt;Filipe Matias&lt;/a&gt; &lt;span style="float:right;font-size:11px;"&gt;19 hours ago&lt;/span&gt; &lt;/div&gt; &lt;div class="iview-messages-item-info-row"&gt; &lt;span&gt;Hmmmmmm yeeelloowww!&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>Any tips?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload