JavaScript滚动到div ID [英] Javascript scroll to div id

查看:696
本文介绍了JavaScript滚动到div ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google上搜索了解决问题的方案,但我无法理解为什么我已经为所有人编写了代码,但对我来说没有。



我写过:

 < head> 
< meta charset =utf-8/>
< title>< / title>
< script type =text / javascript>
function scrollTo(){
$('html,body')。animate({scrollTop:$('#div_id')。offset()。top},'slow');
返回false;
}
< / script>
< style type =text / css>
.uno {
height:1000px;
背景:#808080;
}
.due {
margin-top:300px;
height:500px;
背景:#ff00ff;
}
< / style>
< / head>
< body>
< div class =unoonclick =scrollTo()>
Clicca
< / div>
< div class =dueid =div_id>< / div>
< / body>


解决方案

 < HTML> 
< head>
< meta charset =utf-8/>
< title>< / title>
< script src =https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js>< / script>
< script type =text / javascript>
function scrollTo(){
$('html,body')。animate({scrollTop:$('#div_id')。offset()。top},'slow');
返回false;
}
< / script>
< style type =text / css>
.uno {
height:1000px;
背景:#808080;
}
.due {
margin-top:300px;
height:500px;
背景:#ff00ff;
}
< / style>
< / head>
< body>
< div class =unoonclick =scrollTo()>
Clicca
< / div>
< div class =dueid =div_id>< / div>
< / body>
< / html>

试试这个:

I've searched on google the solution to my problem, and I can't understand why the code I've written work to all, but to me don't.

I've written this:

<head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript">
        function scrollTo() {
            $('html, body').animate({ scrollTop: $('#div_id').offset().top }, 'slow');
            return false;
        }
    </script>
    <style type="text/css">
        .uno {
            height: 1000px;
            background: #808080;
        }
        .due {
            margin-top: 300px;
            height: 500px;
            background: #ff00ff;
        }
    </style>
</head>
<body>
    <div class="uno" onclick="scrollTo()"> 
        Clicca
    </div>
    <div class="due" id="div_id"></div>
</body>

解决方案

<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script type="text/javascript">
        function scrollTo() {
            $('html, body').animate({ scrollTop: $('#div_id').offset().top }, 'slow');
            return false;
        }
    </script>
    <style type="text/css">
        .uno {
            height: 1000px;
            background: #808080;
        }
        .due {
            margin-top: 300px;
            height: 500px;
            background: #ff00ff;
        }
    </style>
</head>
    <body>
        <div class="uno" onclick="scrollTo()"> 
        Clicca
        </div>
        <div class="due" id="div_id"></div>
    </body>
</html>

Try this:

这篇关于JavaScript滚动到div ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆