Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent hash change from scrolling

Sorry for the ugly layout example below... http://www.wthdesign.net/test/test2.html

I managed to append my id name to the url:

function generateUrl(el)
{
    var currentId = $(el).attr('id');
    document.location.hash = currentId;
}

and add:

<a id="abc2" onClick="generateUrl(this)" >this is an anchor btn</a>

but this end up having the same effect as:

<a id="abc2" href="#abc2" >this is an anchor btn</a>

Everything is fine but I just don't want it to scroll when when I click on the link How should I do that? Many thanks in advance.

like image 315
Vincent Chua Avatar asked Apr 09 '13 03:04

Vincent Chua


1 Answers

If the ids aren't necessary, just an href="#some-value will change the window location without scrolling the page. If you do need the id in the document (on the a tags in this case) then the location change will cause a scroll. You can work around this by using the history object in modern browsers or by storing the scroll location on the link click, then resetting it using the hashchange event.

I will use this markup for both solutions:

Sample markup:

<div class="filler"></div>
<a id="abc1" href="#abc1" class="my-class">this is an anchor btn</a>
<div class="filler"></div>
<a id="abc2" href="#abc2" class="my-class">this is an anchor btn</a>
<div class="filler"></div>
<a id="abc3" href="#abc3" class="my-class">this is an anchor btn</a>
<div class="filler"></div>

history.replaceState or history.pushState

Live demo (click).

//get element referneces
var elems = document.getElementsByClassName('my-class');

//iterate over the references
for (var i=0; i<elems.length; ++i) {
  //add click function to each element
  elems[i].addEventListener('click', clickFunc);
}

//this will store the scroll position
var keepScroll = false;

//fires when a ".my-class" link is clicked
function clickFunc(e) {
  //prevent default behavior of the link
  e.preventDefault();
  //add hash
  history.replaceState({}, '', e.target.href);
}

scrollTop and hashchange event

Live demo (click).

JavaScript:

//get element referneces
var elems = document.getElementsByClassName('my-class');

//iterate over the references
for (var i=0; i<elems.length; ++i) {
  //add click function to each element
  elems[i].addEventListener('click', clickFunc);
}

//this will store the scroll position
var keepScroll = false;

//fires when a ".my-class" link is clicked
function clickFunc(e) {
  //if the location hash is already set to this link
  if (window.location.hash === '#'+e.target.id) {
    //do nothing
    e.preventDefault(); 
  }
  else {
    //the location will change - so store the scroll position
    keepScroll = document.body.scrollTop;
  }
}

window.addEventListener('hashchange', function(e) {
  //the location has has changed

  //if "keepScroll has been set
  if (keepScroll !== false) {
    //move scroll position to stored position
    document.body.scrollTop = keepScroll;
    //set "keepScroll" to false so that this behavior won't affect irrelevant links
    keepScroll = false;
  }
});
like image 117
m59 Avatar answered Oct 11 '22 21:10

m59