单击按钮时如何滚动到顶部? [英] How to scroll to top when a button is clicked?

查看:83
本文介绍了单击按钮时如何滚动到顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有一个按钮,我希望当它被单击时,页面滚动到0位置.

I have a button on a page and I want that when it is clicked, the page scrolls to the 0 position.

HTML

<button id="button">Back to top </button>

jquery

$(document).scroll(function(){

        var scroll_pos = $(window).scrollTop()
    if(scroll_pos > 10){

        $('#button').click(function(){

        // what code to enter here??


        });

    }
    });

推荐答案

尝试以下代码:

$("#button").on("click", function() {
    $("body").scrollTop(0);
});

方法onclick事件绑定到按钮,并且scrollTop将您的body滚动到0位置.

Method on binds a click event to the button and scrollTop scrolls your body to 0 position.

这篇关于单击按钮时如何滚动到顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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