javascript Onclick 菜单按钮,网站滚动到 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22083873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 22:24:22  来源:igfitidea点击:

Onclick menu button, website scrolls to div

javascriptjqueryhtmlcssscrollto

提问by zot3

I'm making a personal portfolio in an infinte scroll website. Right now I have 4 divs (home, bio, work and contact). I have an header with a menu with 4 names, same as divs. So what I want to do is to have a trigger when I'm at the div "Home" and click on the menu "Work" I want the browser to scroll down to the third div whick is "Work". Hope I make myself clear :)

我正在无限滚动网站上制作个人作品集。现在我有 4 个 div(家庭、生物、工作和联系人)。我有一个带有 4 个名称的菜单的标题,与 div 相同。所以我想要做的是当我在 div “Home” 并点击菜单“Work”时有一个触发器我希望浏览器向下滚动到第三个 div whick 是“Work”。希望我说清楚:)

回答by Jatin

For something really basic use this:

对于一些非常基本的使用这个:

<a href="#work">Work</a>

or you can try jQuery plugin ScrollTo

或者你可以试试 jQuery 插件ScrollTo

Working Fiddle

工作小提琴

回答by pna

If your Work section have a id, for instance workyou can do it with:

例如,如果您的工作部分有一个 id,work您可以使用以下命令:

$('html, body').animate({scrollTop: $("#work").offset().top}, 2000);

with some smooth animation

带有一些流畅的动画

回答by homa

You should use the <a href="#work">Work</a>method as @Jatin already pointed out. However you should also modify your divs for it. For example the work div should look like this:

您应该使用<a href="#work">Work</a>@Jatin 已经指出的方法。但是,您也应该为它修改您的 div。例如,工作 div 应如下所示:

<div id="work">........</div>

For more information check this page.

有关更多信息,请查看此页面