how can I control my scroll of a Chatroom? [closed]

0

my problem is that the scroll remains in the messages that already passer and does not go down when there is a new message I want to control the scroll as well as the facebook if someone knows how to do that please tell me Thank you.

 <script type="text/javascript">



//esto envia los mensages a la base de datos 


        $(document).ready(function() {

            //method to trigger when user hits enter key
            $("#mensaje").keypress(function(evt) {
                if(evt.which == 13) {

                        //send data to "shout.php" using jQuery $.post()
                         enviar_insert();
                    }

            });
        }); 

    function enviar_insert(){
        var mensaje = document.getElementById("mensaje").value;

        var paramentros = {
                "mmensaje" : mensaje,
                "pconsulta" : "consulta",
        };


        $.ajax({
            data: paramentros,
            url: 'submit_chat.php?v=<?php echo $id; ?>',
            type: 'POST',
            success: function(response) { 

        }
        });
        limpiar_datos();
    }

/////aqui se eliminan los datos 

        function enviar_DELETE(){
        var mensaje = document.getElementById("mensaje").value;
        var paramentros = {
                "mmensaje" : mensaje,
                "pconsulta" : "DELETE",
        };
        $.ajax({
            data: paramentros,
            url: 'submit_chat.php?v=<?php echo $id; ?>',
            type: 'POST',
        });
        limpiar_datos();
    }




//tiempo real del chat 1000 es un segundo 
//aqui nos deja ver el chat 
function mostrar_enviado() {
            var parametros = {
            "pconsulta" : "mostrar_mensajes",
        }   


    $.ajax({
        data: parametros,
        url: 'submit_chat.php?v=<?php echo $id; ?>',
        success: function(data) {
            $('#mostrar_mensaje').html(data);

                var abajo=$("#mostrar_mensaje").prop("scrollHeight");
                $("#mostrar_mensaje").scrollTop(abajo)  

             setTimeout(function(){//time real
                    mostrar_enviado();
            }, (Math.floor(Math.random()*1000)))


        },

    });
}   
mostrar_enviado();


//limpia la caja de los mensages    

function limpiar_datos(){
    document.getElementById("mensaje").value="";
}

</script>
    
asked by Shareiv 22.02.2017 в 22:13
source

3 answers

4

@Shareiv I have created an example of a concept on how you can address this:

$(document).on('DOMNodeInserted', function(e) {
    if ( $(e.target).hasClass('nueva_msg') ) {
        var $newMessage = $('#newMessageAnchor'),
            $msgContainer = $('.messageContainer'),
            $newMessage = $newMessage.position().top + $msgContainer.scrollTop();
        $msgContainer.animate({scrollTop: $newMessage}, 1000);
    }
});

// El código abajo no importa realmente
var addMsgItem = setInterval(function(){
    var numItems = $('.nueva_msg').length;
    $('.nueva_msg').last().after('<div class="nueva_msg">Nueva mensaje!</div>');
    if ( numItems >= 6) {
        clearInterval(addMsgItem);
    }
}, 2000);
.messageContainer {
    height: 90vh;
    width: 50vw;
    overflow: auto;
}
.nueva_msg {
    height: 50px;
    padding: 10px;
    margin-bottom: 50px;
    border: 1px solid #c1c1c1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="messageContainer">
    <div class="nueva_msg">último mensaje</div>
    <div class="nueva_msg">otra mensaje</div>
    <div id="newMessageAnchor"></div>
</div>
    
answered by 23.02.2017 / 02:12
source
1

I do not add comment because I have not allowed it yet, even so we would need you to show the source code in order to solve your problem, for now I recommend that you add the position style to your scroll:

static-bottom o fixed-bottom
    
answered by 22.02.2017 в 22:23
1

You could send the scroll to the end of the page when you have retrieved the new item. This way

window.scrollTo(0,document.body.scrollHeight);

I hope you serve

    
answered by 22.02.2017 в 22:28