CSS Div 锚点滚动太远

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

Div anchors scrolling too far

cssanchor

提问by Koen027

I'm using a static bar at the top of my site, about 20px high. When I click an anchor link(for those who don't know, the navigation on wikipedia works like that. Click a title and the browser goes down to it) part of the text disappears behind that top bar.

我在我的网站顶部使用了一个静态栏,大约 20 像素高。当我点击一个锚链接(对于那些不知道的人,维基百科上的导航是这样工作的。点击一个标题,浏览器就会转到它)部分文本消失在顶部栏后面。

Is there any way to stop this from happening? I'm not in a position where I can use an iFrame. Onlything I can think of is make it scroll back a bit each time, but is there another way? Some CSS setting to manipulate the body or something?

有没有办法阻止这种情况发生?我无法使用 iFrame。我唯一能想到的就是让它每次都向后滚动一点,但还有其他方法吗?一些CSS设置来操纵身体还是什么?

采纳答案by Christoph

To fix this with CSS you can add a padding to the Elements you want to jump to:

要使用 CSS 解决此问题,您可以向要跳转到的元素添加填充:

Example

例子

Alternatively, you could add a border:

或者,您可以添加边框:

div{ 
  height: 650px; 
  background:#ccc; 
  /*the magic happens here*/
  border-top:42px solid #fff;
}
ul{
  top: 0; 
  width: 100%; 
  height:20px; 
  position: fixed; 
  background: deeppink; 
  margin:0;
  padding:10px; 
}
li{
  float:left;
  list-style:none;
  padding-left:10px;
}
div:first-of-type{ 
  margin-top:0; 
}
<!-- content to be placed inside <body>…</body> -->
<ul>
  <li><a href="#s1">link 1</a>
  <li><a href="#s2">link 2</a>
  <li><a href="#s3">link 3</a>
  <li><a href="#s4">link 4</a>
</ul>
<div id="s1" class="first">1</div>
<div id="s2">2</div>
<div id="s3">3</div>
<div id="s4">4</div>

However, this is not always applicable.

然而,这并不总是适用。

For a javascript solution you could use a click event attached to the anchor elements that scrolls an adjusted amount of pixels like following:

对于 javascript 解决方案,您可以使用附加到锚元素的点击事件,滚动调整后的像素数量,如下所示:

document.querySelector("a").addEventListener("click",function(e){
    // dynamically determining the height of your navbar
    let navbar = document.querySelector("nav");
    let navbarheight = parseInt(window.getComputedStyle(navbar).height,10);
    // show 5 pixels of previous section just for illustration purposes 
    let scrollHeight = document.querySelector(e.target.hash).offsetTop - navbarheight - 5;
    /* scrolling to the element taking the height of the static bar into account*/
    window.scroll(0,scrollHeight);
    /*properly updating the window location*/
    window.location.hash = e.target.hash;
    /* do not execute default action*/
    e.preventDefault();
});
nav{
  position:fixed;
  top:0;
  left:0;
  right:0;
  height:40px;
  text-align:center;
  background:#bada55;
  margin:0;
}
a{
  display:block;
  padding-top:40px;
}
#section1{
  height:800px;
  background:repeating-linear-gradient(45deg,#606dbc55,#606dbc55 10px,#46529855 10px,#46529855 20px);
}
#section2{
  height:800px;
  background:repeating-linear-gradient(-45deg,#22222255,#22222255 10px,#66666655 10px,#66666655 20px);
}
<nav>static header</nav>
<a href="#section2">jump to section 2</a> 
<div id="section1">Section 1</div>
<div id="section2">Section 2</div>

回答by FredTheLover

You could just use CSS without any javascript.

您可以只使用 CSS 而不使用任何 javascript。

Give your anchor a class:

给你的主播上课:

<a class="anchor"></a>

You can then position the anchor an offset higher or lower than where it actually appears on the page, by making it a block element and relatively positioning it. -250px will position the anchor up 250px

然后,您可以通过将锚定为块元素并对其进行相对定位,将锚定为比它实际出现在页面上的位置高或低的偏移量。-250px 将定位点向上 250px

a.anchor{display: block; position: relative; top: -250px; visibility: hidden;}

By Jan see offsetting an html anchor to adjust for fixed header

由 Jan 查看偏移 html 锚点以调整固定标题

回答by Ana

CSS-only: it's a little dirty, but :target {padding-top: 20px;}would work if you are linking to a block element(I assumed you do, since your question says div). However, it might not look so good when you scroll manually afterwards. Example http://dabblet.com/gist/3121729

CSS-only:它有点脏,但如果你链接到块元素:target {padding-top: 20px;}会起作用(我假设你这样做,因为你的问题是 div)。但是,当您之后手动滚动时,它可能看起来不太好。示例http://dabblet.com/gist/3121729

Still, I think that using a bit of JavaScript to fix this would be nicer.

不过,我认为使用一些 JavaScript 来解决这个问题会更好。

回答by PJ Brunet

I had the same problem. Here's a jQuery solution

我有同样的问题。这是一个 jQuery 解决方案

$('a[href^="#"]').on('click',function (e) {
    e.preventDefault();
    var target = this.hash;
    var $trget = $(target);
    // Example: your header is 70px tall.
    var newTop = $trget.offset().top - 70; 
    $('html, body').animate ({
        scrollTop: newTop
    }, 500, 'swing', function () {
        window.location.hash = target;
    }); 
});

回答by RAN

Try with window.scrollBy(xnum,ynum);

试试 window.scrollBy(xnum,ynum);

xnum: How many pixels to scroll by, along the x-axis (horizontal) ynum: How many pixels to scroll by, along the y-axis (vertical)

xnum:沿 x 轴(水平)滚动多少像素 ynum:沿 y 轴(垂直)滚动多少像素

For example: http://www.w3schools.com/js/tryit.asp?filename=try_dom_window_scrollby

例如:http: //www.w3schools.com/js/tryit.asp?filename=try_dom_window_scrollby