页面加载时滚动到指定的div

页面加载时滚动到指定的div

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="/Scripts/jquery-1.8.3.min.js" type="text/javascript"></script>
    <body>
        <div id="a1" style="width: 500px;height: 600px;background:#666;">

        </div>
        <div id="a2" style="width: 500px;height: 600px;background:red;">

        </div>
        <div id="a3" style="width: 500px;height: 600px;background:blue;">

        </div>
        <div id="a4" style="width: 500px;height: 600px;background:#999;">

        </div>
        <script type="text/javascript">

            $(function(){
                var scroll_offset = $("#a3").offset(); //得到这个div层的offset,包含两个值,top和left 
                $("body,html").animate({ 
                scrollTop:scroll_offset.top //让body的scrollTop等于a3的top,就实现了滚动 
                },100); 
             })

</script>
</body>

</html>

  

posted @ 2020-07-08 18:20  挥不去的执念  阅读(689)  评论(0编辑  收藏  举报