0

我创建了一个锚标记如下

<a href="index.html#top">Top</a>

并创建了以下标签

<h2 id="top">Header</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
   Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
   when an  unknown printer took a galley of type and scrambled it to make a type 
   specimen book. It has survived not only five centuries, but also the leap into 
   electronic typesetting, remaining essentially unchanged. It was popularised in the 
   1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more 
   recently with desktop publishing software like Aldus PageMaker including versions 
   of Lorem Ipsum.
</p>

单击链接后,它会滚动到该链接,target但不会看不到<h2>标签。所以至少需要在byscroll之上。请帮助我克服这个问题,工作会更有价值。target10px or 20px

4

3 回答 3

0

首先你需要改变锚标签的href,如下所示

<a href="#top">Top</a>

然后你可以在下面编写 jQuery 代码来滚动到目标

    (function() {
       $('a').click(function() {
           $('html, body').animate({
            scrollTop: $("#top").offset().top
            }, 3000);
       });
    });

它会起作用的。

于 2013-10-11T06:40:45.303 回答
0

这样做:在

<h2 id="top" tabindex="1" >Header</h2>

并使用焦点()

$("#top").attr("tabindex",-1).focus();

或者

或者最简单的方法是在 h2 附近添加一个锚标签并添加 id="top"

<a id="top"></a><h2>Header</h2>

但我不会喜欢这个。

于 2013-10-11T06:31:52.290 回答
0

就我个人而言,我通常从 div 的顶部减去一些像素,所以我的标题看起来很正常,这样做:

jQuery('html, body').animate({
  scrollTop: jQuery( jQuery(this).attr('href') ).offset().top - 25 
  /* You can minus the pixels */
}, 500);
于 2013-10-11T06:44:05.750 回答