5 秒后自动将页面滚动到 div [英] Automatic scroll the page to a div after 5 seconds

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

问题描述

我是 javascript 新手,现在我正在尝试这样做,作为标题,我有一个页面顶部有一个 div,它与带有视频的页面一样大,然后是像这样的几个部分:

I'm new in javascript, now I'm trying to do that, as the title, I've a page which has a div at the top that is as big as the page with a video in it, followed by several sections like this:

<div id="first" style="height:100%; width:100%"></div>
<section id="second" style="height:100%; width:100%"></section>
<section id="third" style="height:100%; width:100%"></section>

现在我需要在页面加载 5 秒后自动将页面滚动到 #second.我尝试了很多方法,但都失败了,也没有找到任何可以正常工作的方法.

Now I need 5 seconds after the page is loaded to scroll automatically the page to #second. I've tried many ways but have failed and haven't found nothing that works properly.

谢谢

推荐答案

我觉得很慷慨,所以这次我只给你代码.

I'm feeling generous, so I'll just give you the code this time.

$(window).load(function () {
    //normally you'd wait for document.ready, but you'd likely to want to wait
    //for images to load in case they reflow the page
    $('body').delay(5000) //wait 5 seconds
        .animate({
            //animate jQuery's custom "scrollTop" style
            //grab the value as the offset of #second from the top of the page
            'scrollTop': $('#second').offset().top
        }, 300); //animate over 300ms, change this to however long you want it to animate for
});

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

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