CSS/JS Implementation Click Anchor point to position offset top

Source: Internet
Author: User

One way to see the StackOverflow is to add a dark anchor to the main content:

The code is as follows Copy Code
<a class= "Target-fix" name= "a-<?php $a->id ();?>" ></a>
<artivle> Main content ...</article>

Offset the anchor point and hide the placeholder:

The code is as follows Copy Code
. target-fix {
position:relative;
Top: -44px; Offset value
Display:block;
height:0;
Overflow:hidden;
}

This is also the most direct way.

I'm using a relatively concise approach: if you support CSS for a modern browser: Target statement, you can set this:

The code is as follows Copy Code
article.a-post:target{
padding-top:44px;
}

For IE, such as backward browser is not supported. You can also use JS to adjust scroll, such as using jquery:

  code is as follows copy code
$ (function () {
  if (location.hash) {
     var target = $ (location.hash);
     if ( target.length==1) {
         var top = Target.Offset (). top-44;
          if (Top > 0) {
              $ (' html,body '). Animate ({scrolltop:top}, 1000);
        }
    }
 }
});

You can use Jquery-hashchange:https://github.com/cowboy/jquery-hash ...

Bind Window.onhashchange Event:

The code is as follows Copy Code

$ (function () {
/* Bind Event/*
$ (window). Hashchange (function () {
var target = $ (location.hash);
if (target.length==1) {
var top = Target.Offset (). top-44;
if (Top > 0) {
$ (' html,body '). Animate ({scrolltop:top}, 1000);
}
}
});
/* Trigger Event * *
$ (window). Hashchange ();
});

About Window.onhashchange event: https://developer.mozilla.org/en-US/d ...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.