简体   繁体   English

如何使Div滚动到底部

[英]How to make a Div scroll to the bottom

i have a textbox and a button. 我有一个文本框和一个按钮。 After pressing the add button to the table inside a div. 在div中按下添加按钮到表后。 I want the div to scroll to the bottom of the page when max-height:100px; 我希望div在max-height:100px;时滚动到页面底部max-height:100px; exceeded overflow-y:auto; 超出overflow-y:auto; addbarcode(); method is a ajax post which will call a jquery dataTable method to populate data on the server side. method是一个ajax post,它将调用jquery dataTable method来填充服务器端的数据。

try 1 试试1

var element = document.getElementById("divcontent");

        element.scrollIntoView(false);

try 2 尝试2

 var element = document.getElementById("divcontent");
                $('#divcontent').scrollTo(element.get(0).scrollHeight);

try 3 尝试3

 var objDiv = document.getElementById("divcontent");
                objDiv.scrollTop = objDiv.scrollHeight;

above attempts all doesnt work. 以上尝试都不起作用。

edit 编辑

<div class="row" id="divcontent" style="max-height:100px; overflow-y:auto">

<div class="col-md-12 col-sm-12">
    <table class="table table-striped table-responsive" id="codelist">
        <thead>
            <tr>
                <th>
                    SN
                </th>
                <th>
                    Serial Number
                </th>

            </tr>
        </thead>
    </table>


</div>

Javascript 使用Javascript

$(document).ready(function () {
    $("#scanner").on('keyup paste', function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 13) { //Enter keycode
            var artno = $(this).val();
            if (artno.length == 32 && ) {
                addbarcode();
                $(this).val("");

            } else {
                $(this).val("");
            }
            var element = document.getElementById("divcontent");

            element.scrollIntoView(false);
        }


    });

})

final working code added animate to allow smooth scrolling . 最终工作代码添加了动画以允许平滑滚动。 also added timer as ajax code run faster than html render .so setting a little delay allows the javascript to capture full height. 还添加了计时器作为ajax代码运行比html渲染更快。所以设置一点延迟允许javascript捕获全高。

  function divscrolldown() {
    setTimeout(function () {
        $('#divcontent').animate({
            scrollTop: $("#divcontent").offset().top
        }, 500);

    }, 200);
element.scrollIntoView(false);

If false , the bottom of the element will be aligned to the bottom of the visible area of the scrollable ancestor. 如果为false ,则元素的底部将与可滚动祖先的可见区域的底部对齐。

MDN: Element.scrollIntoView() MDN: Element.scrollIntoView()

This worked for me: 这对我有用:

$('#scroll').scrollTop(1000000);

There's more answers here: Scroll to bottom of div? 这里有更多答案: 滚动到div的底部?

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

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