如果不从数据库中获取数据,那么如果我们插入消息,滚动会正常下来。但是,如果我们从数据库中获取数据,则无法向下滚动。
从数据库表单插入和获取数据
<div class="panel-body msg_container_base">
<?php foreach ($customer_to_supplier as $row ) { ?>
<div class="row msg_container base_sent" >
<div class="col-md-10 col-xs-10">
<div class="messages msg_sent">
<p ><?php echo $row->message; ?></p>
</div>
</div>
</div>
<div class="row msg_container base_receive">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p><?php // echo $row->message; ?></p>
</div>
</div>
</div>
<chat_log> . </chat_log>
<?php } ?>
</div>
<form class="form-horizontal msg_fixed_bottom" method="POST" role="form" action="<?php echo base_url(); ?>index.php/Profile_cntrl/supplier_communication">
<div class="panel-footer" id="myForm" >
<div class="input-group submit_group">
*
<!-- <input type ="hidden" name="suppid" id="suppid" value="<?php // echo $row->supplier_id; ?>" class="form-control" />
<input type ="hidden" name="proid" id="proid" value="<?php // echo $row->product_id; ?>" class="form-control" />
<input type ="hidden" name="custid" id="custid" value="<?php // echo $row->Customer_id; ?>" class="form-control" />-->
<input id="messagee" name="messagee" type="text" class="form-control input-sm chat_input" placeholder="Write your message here..." />
<span class="input-group-btn">
<button class="btn btn-primary btn-sm" id="submit" name="submit">Send</button>
</span>
</div>
</div>
</form>
脚本
<script>
$("#submit").click(function () {
var data = $("#messagee").val();
//console.log(data);
$('chat_log').append('<div class="row msg_container base_sent"><div class="col-md-10 col-xs-10"><div class="messages msg_receive"><p>' + .message + '</p></div></div></div>');
clearInput();
console.log($('.msg_container_base').innerHeight())
$('.msg_container_base').scrollTop($('.msg_container_base')[0].scrollHeight)
});
// function clearInput() {
// $("#myForm :input").each(function () {
// $(this).val(''); //hide form values
// });
// }
//
// $("#myForm").submit(function () {
// return false; //to prevent redirection to save.php
// });
</script>
最佳答案
请检查我下面的代码
$(document).ready(function() {
scrollDown();
$("#submit").click(function(e) {
e.preventDefault();
var message = $("#messagee").val();
$('#chat_log').append('<div class="row msg_container base_sent active"><div class="col-md-10 col-xs-10"><div class="messages msg_receive"><p>' + message + '</p></div></div></div>');
$('#messagee').val('');
scrollDown();
});
});
function scrollDown() {
$('.msg_container_base').animate({scrollTop: $('.msg_container_base').prop("scrollHeight")}, 500);
}
.text-danger {
color: #ff0500;
font-size: 10px;
float: right;
font-weight: bold;
}
/*newmessage css*/
.msg_container_base{
background: #e5e5e5;
margin: 0;
padding: 0 10px 10px;
max-height:80vh;
overflow-x:hidden;
}
.top-bar {
background: #666;
color: white;
padding: 10px;
position: relative;
overflow: hidden;
}
.msg_receive{
padding-left:0;
margin-left:0;
}
.msg_sent{
padding-bottom:20px !important;
margin-right:0;
}
.messages {
background: white;
padding: 10px;
border-radius: 2px;
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
max-width:100%;
}
.messages > p {
font-size: 13px;
margin: 0 0 0.2rem 0;
}
.messages > time {
font-size: 11px;
color: #ccc;
}
.msg_container {
padding: 10px;
overflow: hidden;
}
.base_receive > .avatar:after {
content: "";
position: absolute;
top: 0;
right: 0;
width: 0;
height: 0;
border: 5px solid #FFF;
border-left-color: rgba(0, 0, 0, 0);
border-bottom-color: rgba(0, 0, 0, 0);
}
.base_sent {
justify-content: flex-end;
align-items: flex-end;
}
.base_sent > .avatar:after {
content: "";
position: relative;
bottom: 0;
left: 0;
width: 0;
height: 0;
border: 5px solid white;
border-right-color: transparent;
border-top-color: transparent;
box-shadow: 1px 1px 2px rgba(black, 0.2);
}
.msg_sent > time{
float: right;
}
.msg_container_base::-webkit-scrollbar-track
{
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,0.3);
background-color: #F5F5F5;
}
.msg_container_base::-webkit-scrollbar
{
width: 12px;
background-color: #F5F5F5;
}
.msg_container_base::-webkit-scrollbar-thumb
{
-webkit-box-shadow: inset 0 0 6px rgba(0,0,0,.3);
background-color: #555;
}
.btn-group.dropup{
position:relative;
left:0px;
bottom:0;
}
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<div class="panel-body msg_container_base">
<div class="row msg_container base_sent" >
<div id="chat_log">
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
<div class="row msg_container base_sent">
<div class="col-md-10 col-xs-10">
<div class="messages msg_receive">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p>
</div>
</div>
</div>
</div>
</div>
</div>
<form class="form-horizontal msg_fixed_bottom send_message_form" method="POST" role="form">
<div class="panel-footer" id="myForm" >
<div class="input-group submit_group">
<input id="messagee" name="messagee" type="text" class="form-control input-sm chat_input" placeholder="Write your message here..." />
<span class="input-group-btn">
<button class="btn btn-primary btn-sm" id="submit" name="submit">Send</button>
</span>
</div>
</div>
</form>
</body>
</html>
让我知道是否有效。
关于javascript - 如果我们从数据库中获取数据,则无法使用javascript和codeigniter向下滚动,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44602495/