Html anchor to adjust the offset to a fixed header [repeat]

This translation from: offsetting the ADJUST AN to HTML Anchor for Fixed header [Duplicate]

This question already has an answer here: This question has been answered here:

I am trying to clean up the way my anchors work. I'm trying to clean up the anchor work. I have a header that is fixed to the top of the page, so when you link to an anchor elsewhere in the page, the page jumps so the anchor is at the top of the page, leaving the content behind the fixed header (I hope that makes sense). I have a title attached to the top of the page, so when you link to a page other anchor position, the page will jump, so the top of the page anchor, and the contents left behind a fixed title (I hope that makes sense). I need a way to offset the anchor by the 25px from the height of the header. A method to anchor I need a height offset 25 pixels from the header. I would prefer HTML or CSS, but Javascript would be acceptable as well. I prefer HTML or CSS, but Javascript is acceptable.


#1st Floor

Reference: https://stackoom.com/question/j23u/ offset html anchor to adjust to a fixed header - Repeat


#2nd Floor

I found this solution: I found this solution:

<a name="myanchor">
    <h1 style="padding-top: 40px; margin-top: -40px;">My anchor</h1>
</a>

This does not create any gap in the content and anchor links works really nice. It does not cause any gaps in content and anchor links are indeed very good.


#3rd floor

Suggests @moeffju AS, the this CAN BE Achieved with CSS . As @moeffju suggests, this can be achieved by CSS . The issue I ran into (which I 'm surprised I have not seen discussed) is the trick of overlapping previous elements with padding or a transparent border prevents hover and click actions at the bottom of those sections because the following one comes higher in the z-order. my problem (to my surprise, I have not seen the issues discussed) is overlapping elements of previous skills or filled with a transparent border to prevent hover and click at the bottom of these parts operation, because the next stage in the Z.

Best FIX WAS the I found at The Place to in Section A Content divthat IS AT z-index: 1: I found that the best solution is to place the part in z-index: 1the divmiddle:

// Apply to elements that serve as anchors
.offset-anchor {
  border-top: 75px solid transparent;
  margin: -75px 0 0;
  -webkit-background-clip: padding-box;
  -moz-background-clip: padding;
  background-clip: padding-box;
}

// Because offset-anchor causes sections to overlap the bottom of previous ones,
// we need to put content higher so links aren't blocked by the transparent border.
.container {
  position: relative;
  z-index: 1;
}

#4th floor

I ran into this same issue and ended up handling the click events manually, like: I encountered the same problem, finally handled manually click events, such as:

$('#mynav a').click(() ->
  $('html, body').animate({
      scrollTop: $($(this).attr('href')).offset().top - 40
  }, 200
  return false
)

Scroll animation optional, of course. Of course, the scroll animation is optional.


#5th Floor

As this is a concern of presentation, a pure CSS solution would be ideal. Since this is the focus of the presentation, so pure CSS solution would be the ideal choice. However, this question was posed in 2012 , and although relative positioning / negative margin solutions have been suggested, these approaches seem rather hacky, create potential flow issues, and can not respond dynamically to changes in the DOM / viewport. However, the problem is in 2012 proposed, despite the proposed relative positioning / negative margin solutions, but these methods seem clumsy, creates the potential flow problem, and you can not respond dynamically to changes DOM / viewport.

That the I Believe in Mind With a using JavaScript that IS Still (February 2017) at The Best Approach. With this in mind, I believe that the use of JavaScript still is (in February 2017) the best way. Vanilla-JS IS A Below Which Solution by Will both-the respond to Clicks and Resolve at The Anchor Page hash ON the Load (See JSFiddle) . The following is Vanilla-JS solution, it will respond and resolve the page, click the hash anchor when loaded ( see JSFiddle) . At The the Modify .getFixedOffset()Method, IF Dynamic Calculations are required. If you need to dynamically calculate, modify the .getFixedOffset()method. You're a using jQuery IF, here Wallpaper's A Solution with Modified Event Delegation of Better and Smooth scrolling . If you are using jQuery, which is a modified solution with better event delegate and smooth scrolling .

(function(document, history, location) {
  var HISTORY_SUPPORT = !!(history && history.pushState);

  var anchorScrolls = {
    ANCHOR_REGEX: /^#[^ ]+$/,
    OFFSET_HEIGHT_PX: 50,

    /**
     * Establish events, and fix initial scroll position if a hash is provided.
     */
    init: function() {
      this.scrollToCurrent();
      window.addEventListener('hashchange', this.scrollToCurrent.bind(this));
      document.body.addEventListener('click', this.delegateAnchors.bind(this));
    },

    /**
     * Return the offset amount to deduct from the normal scroll position.
     * Modify as appropriate to allow for dynamic calculations
     */
    getFixedOffset: function() {
      return this.OFFSET_HEIGHT_PX;
    },

    /**
     * If the provided href is an anchor which resolves to an element on the
     * page, scroll to it.
     * @param  {String} href
     * @return {Boolean} - Was the href an anchor.
     */
    scrollIfAnchor: function(href, pushToHistory) {
      var match, rect, anchorOffset;

      if(!this.ANCHOR_REGEX.test(href)) {
        return false;
      }

      match = document.getElementById(href.slice(1));

      if(match) {
        rect = match.getBoundingClientRect();
        anchorOffset = window.pageYOffset + rect.top - this.getFixedOffset();
        window.scrollTo(window.pageXOffset, anchorOffset);

        // Add the state to history as-per normal anchor links
        if(HISTORY_SUPPORT && pushToHistory) {
          history.pushState({}, document.title, location.pathname + href);
        }
      }

      return !!match;
    },

    /**
     * Attempt to scroll to the current location's hash.
     */
    scrollToCurrent: function() {
      this.scrollIfAnchor(window.location.hash);
    },

    /**
     * If the click event's target was an anchor, fix the scroll position.
     */
    delegateAnchors: function(e) {
      var elem = e.target;

      if(
        elem.nodeName === 'A' &&
        this.scrollIfAnchor(elem.getAttribute('href'), true)
      ) {
        e.preventDefault();
      }
    }
  };

  window.addEventListener(
    'DOMContentLoaded', anchorScrolls.init.bind(anchorScrolls)
  );
})(window.document, window.history, window.location);

#6th floor

I was looking for a solution to this as well. I am also looking for solutions. In my case, it was pretty easy . For me, it's easy.

I have a list menu with all the links: I have a menu containing a list of all the links:

<ul>
<li><a href="#one">one</a></li>
<li><a href="#two">two</a></li>
<li><a href="#three">three</a></li>
<li><a href="#four">four</a></li>
</ul>

And below that the headings where it should go to. And under the heading underneath.

<h3>one</h3>
<p>text here</p>

<h3>two</h3>
<p>text here</p>

<h3>three</h3>
<p>text here</p>

<h3>four</h3>
<p>text here</p>

Now because I have a fixed menu at the top of my page I can not just make it go to my tag because that would be behind the menu. Now, because I have a fixed menu at the top of the page, I can not just be I moved to the label, because that would later menu.

Instead, I put a span tag inside my tag with the proper id. Instead, I will put in a span tag with the correct ID's.

<h3><span id="one"></span>one</h3>

Now use 2 lines of CSS to position them properly. Now use 2 lines of CSS to place them correctly.

h3{ position:relative; }
h3 span{ position:absolute; top:-200px;}

Change the top value to match the height of your fixed header (or more). Changing the maximum value to match the height of a fixed header (or more). Now I assume this would work with other elements as well. Now, I think it can also be used with other elements.

Original articles published 0 · won praise 73 · views 550 000 +

Guess you like

Origin blog.csdn.net/w36680130/article/details/105296309