简体   繁体   English

跳转到页面的特定部分

[英]Jump to a specific part of page

I've experimented with a simple "feature" on my Shopify theme, where I want a link to jump to a specific part of the page - quite simple, and I've done it like this.我已经在我的 Shopify 主题上尝试了一个简单的“功能”,我想要一个链接跳转到页面的特定部分 - 非常简单,我已经这样做了。

<a href="#jumpto">Jump to section</a>

<span id="jumpto">The section i want to jump to</span>

It works like a charm, but!它就像一个魅力,但是! I have a sticky header, where the "The section i want to jump to" is hidden behind.我有一个粘性标题,其中隐藏了“我想跳转到的部分”。 Is there any way, with css, push it a bit down down, so "The section I want to jump to" is shown, right beneath the header.有没有什么办法,用css,把它向下推一点,这样“我想跳到的部分”就会显示在标题的正下方。

Basically I want the span element to appear for example 50px beneath the header.基本上我希望span元素出现在标题下方,例如 50px。

My setup looks something like this我的设置看起来像这样

<div id="sectiona">
</div>

<span id="jumpto">The section i want to jump to</span>
<div id="sectionb">
</div>

If i use margin og padding, the gab between would be to big.如果我使用边距 og 填充,则两者之间的差距会很大。

You can use JavaScript to solve your problem and make smooth scrolling as well.您可以使用 JavaScript 来解决您的问题并平滑滚动。

$('a[href*="#"]').click(function(){
    $('html, body').animate({
        scrollTop: $( $(this).attr('href') ).offset().top - 50
    }, 500);
    return false;
});

This might not the prettiest solution, but it should do the trick.这可能不是最漂亮的解决方案,但它应该可以解决问题。 By having an empty span ( .jump-marker ) acting as the jumping point you can offset the position where the anchor jumps to.通过将空跨度 ( .jump-marker ) 作为跳跃点,您可以偏移锚点跳转到的位置。

.jump-marker {
  position: absolute;
  margin: -120px 0 0;
}

You could also use JS/jQuery to offset the scroll distance by your header height and also allow smooth scrolling, which is much more pleasing to the eye, but that wasn't asked for!您还可以使用 JS/jQuery 通过标题高度来偏移滚动距离,并允许平滑滚动,这更令人赏心悦目,但这不是要求的! :) :)

Example:例子:

 body { padding-top: 120px; } header { position: fixed; top: 0; left: 0; right: 0; background: rgba(255,0,0,0.25); height: 100px; } .more-content { height: 1000px; background: #f7f7f7; } section { position: relative; } .jump-marker { position: absolute; margin: -120px 0 0; }
 <header></header> <a href="#jumpto">Jump to section</a> <div class="more-content"></div> <section> <div class="section-content"> <span id="jumpto" class="jump-marker"></span> The section i want to jump to </div> </section> <div class="more-content"></div>

I make a basic header fixed and made the links tag link offset from the place I want to link too.我固定了一个基本的标题,并使链接标签链接从我想要链接的地方偏移。 This solution is really rough but can work with good CSS and placing html:这个解决方案非常粗糙,但可以使用良好的 CSS 并放置 html:

<div class="jumpTo">
    <a id="0">0</a>
</div>
<h2>0</h2>

css: css:

.jumpTo{
  margin-bottom: 100px;
  font-size: 0;
}

https://jsfiddle.net/ovcx2t9z/ https://jsfiddle.net/ovcx2t9z/

or a fiddle based on the Jquery solution或基于 Jquery 解决方案的小提琴

You can use JavaScript to solve your problem and make smooth scrolling as well.您可以使用 JavaScript 来解决您的问题并平滑滚动。

$('a[href*="#"]').click(function(){
   $('html, body').animate({
       scrollTop: $( $(this).attr('href') ).offset().top - 50
   }, 500);
   return false;
});

https://jsfiddle.net/qy03xb1r/2/ https://jsfiddle.net/qy03xb1r/2/

How is the page layed out?页面是如何布局的? Have you tried using padding or margin adjustments like.. padding-top:50px;您是否尝试过使用填充或边距调整,例如 .. padding-top:50px; or margin-top:50px.或边距顶部:50px。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM