Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用javascript编写“scrollTop”?_Javascript_Jquery - Fatal编程技术网

如何用javascript编写“scrollTop”?

如何用javascript编写“scrollTop”?,javascript,jquery,Javascript,Jquery,我正试图用JavaScript验证我的表单,所以我想向上滚动,以便用户可以看到表单上方的错误div。我在项目中使用过它 <script type="text/JavaScript"> var amount_payable = $("#amount_payable").val(); var error = document.getElementById("show_result"); if(amount_payable ==""){ erro

我正试图用JavaScript验证我的表单,所以我想向上滚动,以便用户可以看到表单上方的错误div。

我在项目中使用过它

<script type="text/JavaScript">
    var amount_payable = $("#amount_payable").val();

    var error = document.getElementById("show_result");

    if(amount_payable ==""){
        error.class Name="alert alert-danger";
        error.inner HTML="<center><strong>Error:</strong> Amount Payable is 
        missing, return to Student's Dashboard, and try again.</center>";
        $('html, body').animate({
        scroll Top: $("#show_result").offset().top
        }, 800);
    }
// #show_result is the div id targeted 
</script>

我已经这样做了,谢谢。请添加一些代码,以便我们知道您的问题的上下文,您可以使用或只是使用。
function scrollTo(element, to, duration) {
    var start = element.scrollTop,
    change = to - start,
    currentTime = 0,
    increment = 20;

    var animateScroll = function(){
        currentTime += increment;
        var val = Math.easeInOutQuad(currentTime, start, change, duration);
        element.scrollTop = val;
        if(currentTime < duration) {
            setTimeout(animateScroll, increment);
        }
    };
    animateScroll();
}
Math.easeInOutQuad = function (t, b, c, d) {
    t /= d/2;
    if (t < 1) return c/2*t*t + b;
    t--;
    return -c/2 * (t*(t-2) - 1) + b;
};

var objDiv = document.getElementById("messageBody");
var element = document.querySelector('.bodyChat');
scrollTo(element, objDiv.scrollHeight, 1250);