js makes the scroll bar scroll to the specified height of the container

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ddddd</title>
</head>
<body>
    <script src="jquery.js"></script>
    <div id="thisMainPanel" style="height:200px;overflow-y: scroll;border:1px solid #f3f3f3;">
        <div class="son-panel">类容区域-1</div>
        <div class="son-panel">类容区域-2</div>
        <div class="son-panel">类容区域-3</div>
        <div class="son-panel">类容区域-4</div>
        <div class="son-panel" style="height:160px;">我是类容区域-5</div>    </div>        <div class="son-panel">Class content Area-8</div>        <div class="son-panel">Class content area-7</div>
        <div class="son-panel">Class content area-6</div>



    <script>
        onload = function () {
          //initialize
          scrollToLocation('#thisMainPanel','.son-panel:last');
        };
        function scrollToLocation(boxCont,scrollTo) {
          var boxContainer = $(boxCont),
          scrollToContainer = boxContainer .find(scrollTo);//Scroll to the last div with class name son-panel in <div id="thisMainPanel">
          //scrollToContainer = boxContainer.find('.son-panel:eq(5)') ;//Scroll to the sixth place named son-panel in <div id="thisMainPanel">
          //non-animation
          console.log(scrollToContainer.offset().top - boxContainer.offset().top + boxContainer.scrollTop())
          boxContainer.scrollTop(
           // scrollToContainer.offset().top - boxContainer.offset().top + boxContainer.scrollTop()
           293
          );
          //animation effect
          // boxContainer.animate({
          // scrollTop: scrollToContainer.offset().top - boxContainer .offset().top + boxContainer.scrollTop()
          // }, 2000);//Slide to the specified position in 2 seconds
        }
    </script>
</body>
</html>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325042188&siteId=291194637