jQuery 滚动到 Div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5284814/
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-08-26 18:53:43  来源:igfitidea点击:

jQuery Scroll to Div

jqueryhtml

提问by Christopher

I am making an FAQ page and have buttons across the top to jump to a category (it jumps to the ptag that I use as the category label, ex. <p id="general">for my general category). Instead of just jumping right to the category, I want to add a scroll effect. I want something like http://www.dynamicdrive.com/dynamicindex3/scrolltop.htmthat scrolls to the desired part of my page. That link is a script that goes to the top of the page with a nice scrolling effect. I need something similar that will scroll to where I link to. For example, if I want to go to a misc. category, I want to just be able to have <a href="#misc">Miscellaneous</a>and have it scroll to that section of the page.

我正在制作一个常见问题页面,顶部有按钮跳转到一个类别(它跳转到p我用作类别标签的标签,例如<p id="general">我的一般类别)。我想添加滚动效果,而不是直接跳到类别。我想要类似http://www.dynamicdrive.com/dynamicindex3/scrolltop.htm 的东西,它可以滚动到我页面的所需部分。该链接是一个脚本,它转到页面顶部,具有很好的滚动效果。我需要类似的东西可以滚动到我链接到的地方。例如,如果我想去杂项。类别,我希望能够将<a href="#misc">Miscellaneous</a>其滚动到页面的该部分。

回答by Brian

It is oftenrequired to move both body and html objects together.

这是经常需要移动身体和HTML对象在一起。

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

ShiftyThomas is right:

ShiftyThomas 是对的:

$("#divToBeScrolledTo").offset().top + 10 // +10 (pixels) reduces the margin.

So to increase the margin use:

所以要增加保证金使用:

$("#divToBeScrolledTo").offset().top - 10 // -10 (pixels) would increase the margin between the top of your window and your element.

回答by FarligOpptreden

$(function() {
  $('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
      var target = $(this.hash);
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
      if (target.length) {
        $('html,body').animate({
          scrollTop: target.offset().top
        }, 1000);
        return false;
      }
    }
  });
});

Check this link: http://css-tricks.com/snippets/jquery/smooth-scrolling/for a demo, I've used it before and it works quite nicely.

检查这个链接:http: //css-tricks.com/snippets/jquery/smooth-scrolling/一个演示,我以前用过它,它工作得很好。

回答by Guffa

Something like this would let you take over the click of each internal link and scroll to the position of the corresponding bookmark:

像这样的事情会让你接管每个内部链接的点击并滚动到相应书签的位置:

$(function(){
  $('a[href^=#]').click(function(e){
    var name = $(this).attr('href').substr(1);
    var pos = $('a[name='+name+']').offset();
    $('body').animate({ scrollTop: pos.top });
    e.preventDefault();
  });
});

回答by JimmyJammed

Could just use JQuery position function to get coordinates of your div, then use javascript scroll:

可以使用 JQuery 位置函数来获取 div 的坐标,然后使用 javascript 滚动:

var position = $("div").position();
scroll(0,position.top);

回答by Stoic

if the link element is:

如果链接元素是:

<a id="misc" href="#misc">Miscellaneous</a>

and the Miscellaneous category is bounded by something like:

杂项类别受以下限制:

<p id="miscCategory" name="misc">....</p>

you can use jQuery to do the desired effect:

您可以使用 jQuery 来实现所需的效果:

<script type="text/javascript">
  $("#misc").click(function() {
    $("#miscCategory").animate({scrollTop: $("#miscCategory").offset().top});
  });
</script>

as far as I remember it correctly.. (though, I haven't tested it and wrote it from memory)

据我记得正确..(虽然,我还没有测试它并从记忆中写出来)

回答by Paul0515

I ran into the same. Saw an example using this: https://github.com/flesler/jquery.scrollTo

我遇到了同样的情况。看到一个例子:https: //github.com/flesler/jquery.scrollTo

I use it as follows:

我使用它如下:

$('#arrow_back').click(function () {
    $.scrollTo('#features_1', 1000, { easing: 'easeInOutExpo', offset: 0, 'axis': 'y' }); 
});

Clean solution. Works for me!

清洁溶液。为我工作!

回答by lflier

The script below is a generic solution that works for me. It is based on ideas pulled from this and other threads.

下面的脚本是适用于我的通用解决方案。它基于从这个和其他线程中提取的想法。

When a link with an href attribute beginning with "#" is clicked, it scrolls the page smoothly to the indicated div. Where only the "#" is present, it scrolls smoothly to the top of the page.

单击带有以“#”开头的 href 属性的链接时,它会将页面平滑地滚动到指定的 div。如果只有“#”,它会平滑地滚动到页面顶部。

$('a[href^=#]').click(function(){
    event.preventDefault();
    var target = $(this).attr('href');
    if (target == '#')
      $('html, body').animate({scrollTop : 0}, 600);
    else
      $('html, body').animate({
        scrollTop: $(target).offset().top - 100
    }, 600);
});

For example, When the code above is present, clicking a link with the tag <a href="#">scrolls to the top of the page at speed 600. Clicking a link with the tag <a href="#mydiv">scrolls to 100px above <div id="mydiv">at speed 600. Feel free to change these numbers.

例如,当上面的代码存在时,单击带有标签的链接会以<a href="#">600 的速度滚动到页面顶部。单击带有标签的链接会以 600 的速度<a href="#mydiv">滚动到上方 100 像素<div id="mydiv">。随意更改这些数字。

I hope it helps!

我希望它有帮助!

回答by nickRO87

You can also use 'name' instead of 'href' for a cleaner url:

您还可以使用 'name' 而不是 'href' 来获得更清晰的 url:

    $('a[name^=#]').click(function(){
    var target = $(this).attr('name');
    if (target == '#')
      $('html, body').animate({scrollTop : 0}, 600);
    else
      $('html, body').animate({
        scrollTop: $(target).offset().top - 100
    }, 600);
});