0

I have created a page based on this responsive template

https://html5up.net/uploads/demos/phantom/

I would like the blocks to have an anchor on the same page: when you click on it, it shows you the content (no redirect to a different page)

What is the best "modern" way to achieve this? plain anchor link/jquery/css other?

Can you show me an example?

thanks

Sam Provides
  • 269
  • 2
  • 6
  • 17

3 Answers3

5

The best way according to me is to place the target content on the same page with an ID(obviously unique) <div id="generic"> ..content.. </div> and on your <a> tag's href, target the required div with the ID with href="#generic". like so

<a href="#generic">
  <h2>Magna</h2>
  <div class="content">
     <p>Sed nisl arcu euismod sit ame.</p>
  </div>
</a>
.....
.....
.....
<div id="generic">..</div>

Additional to this add smooth scrolling to make it look like its sliding up/down to the targeted content.

To not add the #generic to your url try using a super simple JQuery fix additional to the above code:

$("article a").click(function(e){
    var $anchor = $(this);
    $('html, body').stop().animate({
        scrollTop: $($anchor.attr('href')).offset().top
    }, 1500);
    e.preventDefault(); //this is the important line.
});

I have tested this on your website & it works like a charm! Let me know if any issues.

Nikhil Nanjappa
  • 6,454
  • 3
  • 28
  • 44
0

Google... single page application(SPA) I know it is possible using AngularJS etc. but there might me some solutions to achieve this only with Jquery/Javascript

Take a look at Architecture for single page application (JavaScript)

Also take a look at http://singlepageappbook.com/single-page.html

Community
  • 1
  • 1
Raul
  • 77
  • 1
  • 13
0

There are plenty of possibilities to do so, the easiest and most dumb way is create and anchor link like this:

<a href="#myContainerId">To my container</a>

In which the href is the id of the container you want to reach with the click. Of course this method jumps without any animation. A good example of an animated scrolling with anchor is found here:

CSS tricks smooth scroll

Let me know if that helps.

Cheers

Roberto Lonardi
  • 569
  • 2
  • 10