JS Anchor Point

Source: Internet
Author: User

Anchor Point, Http://localhost:8000/Demo1/js the anchor point in the address bar. Html#a jump to the specified location can be the name of the <a> tag or the ID of the tag.

I've seen a page with an anchor, but it's hard to see the tag in the code for the name or ID for that anchor point. On the Internet a check, the original can use Window.location.hash this is to get or set the anchor point. Is this not a solid foundation of performance? There's this thing on the W3school.

Look at the code

<!DOCTYPE HTML><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head>    <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" />    <title>JS Anchor Point</title>    <Scriptsrc= "Js/jquery-1.9.1.min.js"></Script>    <Scripttype= "Text/javascript">        $(function () {            $("#demo a"). On ("Click", function () {                var Self= $( This); varHash=self.attr ("href"). Split ('#')[1]; Window.location.hash= "m -" +Hash;        });    }); </Script></Head><Body>    <DivID= "Demo">        <ahref= "#a">Jump A</a>        <ahref= "#b">Jump B</a>    </Div>    <Divstyle= "Height:500px;background-color: #00ced1"></Div>    <DivID= "M-a"style= "Height:200px;background-color: #b6ff00">    </Div>    <DivID= "M-b"style= "Height:200px;background-color: #808080">    </Div></Body></HTML>

There is a small problem, the address bar has an anchor point when the F5 refresh page does not jump to the specified location, you can do so.

$ (function  () {            var hash = Window.location.hash;             var id = hash.split (' # ') [1];             if (ID) {                var t = $ (' #m-' + ID). Offset (). Top;                $ (window). scrolltop (t);            }        });


This will make the refresh jump past. This method can also be modified, add an animation, point the anchor point after the page slowly go to the anchor point.

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.