Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何导航到略高于锚定标记的位置?_Jquery_Ruby On Rails_Twitter Bootstrap_Navigation_Anchor - Fatal编程技术网

Jquery 如何导航到略高于锚定标记的位置?

Jquery 如何导航到略高于锚定标记的位置?,jquery,ruby-on-rails,twitter-bootstrap,navigation,anchor,Jquery,Ruby On Rails,Twitter Bootstrap,Navigation,Anchor,我知道如何导航到页面上给定的锚定标记-我试图做的是链接到目标锚定上方30-40像素的页面部分。这样做的原因是我有一个引导导航,如果我喜欢直接指向页面的锚定部分,它最终会覆盖部分相关内容 目前我有以下链接: %a{:href => root_path + "#how_it_works"} How It Works 并在该页面上链接到: .span12#how_it_works 关于如何让链接导航到略高于.span12的页面部分,您有什么想法吗?您可以通过在css中添加一些额外的填充来解决

我知道如何导航到页面上给定的锚定标记-我试图做的是链接到目标锚定上方30-40像素的页面部分。这样做的原因是我有一个引导导航,如果我喜欢直接指向页面的锚定部分,它最终会覆盖部分相关内容

目前我有以下链接:

%a{:href => root_path + "#how_it_works"} How It Works
并在该页面上链接到:

.span12#how_it_works

关于如何让链接导航到略高于.span12的页面部分,您有什么想法吗?

您可以通过在css中添加一些额外的填充来解决这个问题,但最可靠的方法是使用javascript:

$('a[href="#how_it_works"]').on('click',function(e){
  // prevent normal scrolling action
  e.preventDefault();
  // grab the target url from the anchor's ``href``
  var target = $(this.hash);
  target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
     if (target.length) {
           window.scrollTo(0, target.offset().top - 50); // <-- our offset in px adjust as necessary
      return false;
  }
});

你试过jQuery scrollTo插件吗?似乎这已经被一些调整用来做你想要的。请参见此处的情景/答案

这对您有用吗?

我已将顶部填充添加到锚点:
CSS

a.anchor{
background-color:pink;
padding-top:30px;
display:block;
margin-top:-30px; /* if you want to cancel out the padding-top */
}  
 <a href="#section1">section1</a><br>
  <a href="#section2">section2</a><br>
  <a href="#section3">section3</a><br>
  <a href="#section4">section4</a><br>  
  ....  
  <a name="section1" class="anchor">section1</a>  
  etc  
<body class="welcome">  
....  
 body.welcome {
padding-top:40px;
}  
HTML

a.anchor{
background-color:pink;
padding-top:30px;
display:block;
margin-top:-30px; /* if you want to cancel out the padding-top */
}  
 <a href="#section1">section1</a><br>
  <a href="#section2">section2</a><br>
  <a href="#section3">section3</a><br>
  <a href="#section4">section4</a><br>  
  ....  
  <a name="section1" class="anchor">section1</a>  
  etc  
<body class="welcome">  
....  
 body.welcome {
padding-top:40px;
}  
如果您只需要清除所选页面上的导航栏,请在有问题的页面上的body标记中添加一个类,并仅针对这些特定页面,例如
HTML

a.anchor{
background-color:pink;
padding-top:30px;
display:block;
margin-top:-30px; /* if you want to cancel out the padding-top */
}  
 <a href="#section1">section1</a><br>
  <a href="#section2">section2</a><br>
  <a href="#section3">section3</a><br>
  <a href="#section4">section4</a><br>  
  ....  
  <a name="section1" class="anchor">section1</a>  
  etc  
<body class="welcome">  
....  
 body.welcome {
padding-top:40px;
}  

我有个好主意骗你的浏览器。不需要javascript

创建一个div标记,给它一个名称。让div为空。然后在CSS中使用定位将其向上移动到您想要的距离

position: relative;
top: -165px;
你可以根据你想要的位置来选择相对位置或绝对位置

然后简单地将该div标记设为锚标记

<div class="anchor_placement" id="anchor"></div>


当您转到锚点时,它将转到div标记所在的任何位置

GU3文件-这对我来说非常有效-我正在使用带有持久导航栏的锚

.anchorplace{position: relative;
top: -74px; }

<div class="anchorplace" id="challenge"></div>
.anchorplace{位置:相对;
顶部:-74px;}

您是否正在使用twitter引导?如果是的话,在你的问题上加上标签。你是为了这个目的使用的吗?