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

Posted whqbk

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了页面加载时滚动到指定的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>

  

以上是关于页面加载时滚动到指定的div的主要内容,如果未能解决你的问题,请参考以下文章