Witam!
Mam zrobiony czat i chciałbym jak ktoś wejdzie na chat to od razu pokazywała się ostatnia wiadomość na dole tzn by scroll znajdował się na dole a nie na górze. po wpisaniu tekstu owszem scroll przeskakuje w dół. Próbowałem już w wartościach w style ale coś nie wychodzi.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<style>
p:first-letter {
text-transform:capitalize;
}
</style>
<title>Chat</title>
<link rel="stylesheet" type="text/css" href="reset.css" media="screen">
<style type="text/css">
body {
font-family: 'Open Sans';
}
* {
box-sizing: border-box;
}
.chatContainer {
width: 100%;
height: 550px;
border: 3px solid #eee;
}
.chatContainer > .chatHeader {
width: 100%;
background: #fff;
padding: 5px;
border-bottom: 1px solid #ddd;
}
.chatContainer > .chatHeader h3 {
font-weight: 400;
color: #666;
}
.chatContainer > .chatMessages {
height: 470px;
border-bottom: 1px solid #ddd;
overflow-y: scroll;
}
.chatContainer > .chatBottom form input[type="submit"] {
padding: 6px;
background: #fff;
border: 1px solid #ddd;
cursor: pointer;
}
.chatContainer > .chatBottom form input[type="text"] {
width: 85%;
padding: 8px;
padding-left: 12px;
border: 1px solid #ddd;
border-radius: 5px;
margin: 5px;
height: 30px;
}
.chatMessages li:nth-child(2n) {
background: #eeeeee;
}
.msg {
list-style: none;
border-bottom: 1px solid #ddd;
padding: 5px;
color: #222222;
}
</style>
<body>
<div class="chatContainer">
<div class="chatHeader">
</div>
<div class="chatMessages"></div>
<div class="chatBottom">
<form action="#" onSubmit='return false;' id="chatForm">
<p class="chatBottom p">
<input type="hidden" id="username" value="
<?php echo $username; ?>"/>
<input type="text" username="text" id="text" value="" placeholder="Wpisz wiadomość" />
<input type="submit" username="submit" value="Wyślij" />
</p>
</form>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(document).on('submit', '#chatForm', function(){
var text = $.trim($("#text").val());
var username = $.trim($("#username").val());
if(text != "" && username != "") {
$.post('ChatPoster.php', {text: text, username: username}, function(data){
$(".chatMessages").append(data);
$(".chatMessages").scrollTop($(".chatMessages")[0].scrollHeight);
$("#text").val('');
});
} else {
alert("Musisz wpisac wiadomość!");
}
});
$(document).on('keyup', '#text', function(){
var text = $("#text").val();
String.prototype.ucfirst = function()
{
return this.charAt(0).toUpperCase() + this.substr(1);
}
$("#text").val(text.ucfirst());
});
function getMessages() {
$.get('GetMessages.php', function(data){
var amount = $(".chatMessages li:last-child").attr('id');
$(".chatMessages").html(data);
var countMsg = data.split('<li').length - 1;
array = [countMsg, amount];
});
return array;
}
setInterval(function(){
var num = getMessages();
if(num[0] < num[1]) {
$(".chatMessages").scrollTop($(".chatMessages")[-1].scrollHeight);
}
},1000);
});
</script>
</body>
</html>
Widzę że ciężki temat!
Czyli musiał by gdzieś dopisać w skrypcie wartości na wysokość położenia scrolla przy otwieraniu strony z chatem. Tylko gdzie?
Czy jest ktoś w stanie się tym zainteresować. Bo męcze się z tym już drugi dzień.
I jak to zrobić?
Ten post edytował woxala123 10.04.2016, 12:31:55