Didn"t forge 2011-07-13 09:49 采纳率: 25%
浏览 444
已采纳

滚动到元素

I have this input element:

<input type="text" class="textfield" value="" id="subject" name="subject">

Then I have some other elements, like other text inputs, textareas, etc.

When the user clicks on that input with #subject, the page should scroll to the last element of the page with a nice animation. It should be a scroll to bottom and not to top.

The last item of the page is a submit button with #submit:

<input type="submit" class="submit" id="submit" name="submit" value="Ok, Done.">

The animation should not be too fast and should be fluid.

I am running the latest jQuery version. I prefer to not install any plugin but to use the default jQuery features to achieve this.

转载于:https://stackoverflow.com/questions/6677035/jquery-scroll-to-element

  • 写回答

25条回答 默认 最新

  • MAO-EYE 2011-07-13 09:52
    关注

    Assuming you have a button with the id button, try this example:

    $("#button").click(function() {
        $([document.documentElement, document.body]).animate({
            scrollTop: $("#elementtoScrollToID").offset().top
        }, 2000);
    });
    

    I got the code from the article Smoothly scroll to an element without a jQuery plugin. And I have tested it on the example below.

    <html>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
        <script>
            $(document).ready(function (){
                $("#click").click(function (){
                    $('html, body').animate({
                        scrollTop: $("#div1").offset().top
                    }, 2000);
                });
            });
        </script>
        <div id="div1" style="height: 1000px; width 100px">
            Test
        </div>
        <br/>
        <div id="div2" style="height: 1000px; width 100px">
            Test 2
        </div>
        <button id="click">Click me</button>
    </html>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(24条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测