1

I've downloaded a wonderful template and I made some changes to it.

The problem is when I click on some links it automatically scrolls up.

How can I prevent it from scrolling up?

DavidPostill
  • 7,734
  • 9
  • 41
  • 60

2 Answers2

1

The only reason why a page would scroll up on a click of a link is because of the anchors... Please check your links' href, and make sure they do not contain a # . Usually, downloadable templates use placeholder links and put just the # inside the href so it acts like a link but points to the top of the page.

Salketer
  • 14,263
  • 2
  • 30
  • 58
  • 1
    It is a correct investigation but incomplete answer. The [full answer](http://stackoverflow.com/a/29004028/295783) is given by Roko – mplungjan Mar 12 '15 at 07:34
  • @mplungjan IMO, preventing the defaults on a click of a #'d anchor is a band aid solution. Removing the # is the only real solution... – Salketer Mar 12 '15 at 07:38
  • I am not a purist. The "#" has been used by millions to denote a placeholder for links that execute script so it does not give error when clicked and to get a pointer for free. I am by the way not convinced that removing the # will make a difference since the link will then likely load "/" instead but I have not tested that – mplungjan Mar 12 '15 at 07:41
1

You can event.preventDefault on every anchor that has hash # as href:

// Remove after testing
$("[href='#']").click(function( e ){
   e.preventDefault();
});
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313