Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep URL unaffected when anchor link is clicked

I've checked other posts on here, no results of what I'm looking for. I want to click on

<a href="#about">About</a>
<div id="about">Content of this..</div>

and have it scroll to that element without putting www.domain.com/#about in the address bar

As a perfect example please check out this site that I found here and click on some of the links --they don't change the address bar when clicked.

like image 344
nodebase Avatar asked Jun 09 '13 18:06

nodebase


2 Answers

You can do what you want using javascript and jquery, example below (note that this is using an old version of jquery):

<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>

    <script type='text/javascript'>
    jQuery(document).ready(function($) {

        $(".scroll").click(function(event){
            event.preventDefault();
            $('html,body').animate({scrollTop:$(this.hash).offset().top}, 1200);
        });
    });
    </script>
</head>
<body>
    <a class="scroll" href="#codeword">Blue Words</a>
    <div id="codeword"></div>
</body>
</html>
like image 174
n0rman0 Avatar answered Sep 22 '22 09:09

n0rman0


Played around with this myself and here is a summary of my learnings on the subject.

Here's the basic link command:

<A HREF="#codeword">Blue Words</A>

Here's how you denote where the jump will scroll the page:

<A NAME="codeword">

Here's what's happening

The A HREF command is the same as a basic link except the link is to a codeword rather than a URL.

PLEASE NOTICE there is a # sign in front of the codeword. You need that to denote it is an internal link. Without the # sign, the browser looks for something outside the page named after your codeword.

Your "codeword" can be just about anything you want. I try my best to keep it short and make it denote what it is jumping to. There might be a limit to the number of letters you can use--but I haven't found it yet.

The point where the page will jump follows the same general format except you will replace the word HREF with the word NAME.

PLEASE NOTICE there is no # sign in the NAME command.

Note! Where you place the NAME target will appear at the top of the screen browser.

Hope it helps.

like image 20
PatGW Avatar answered Sep 21 '22 09:09

PatGW