Reputation: 107
I'm using Ben Alman's Hashchange plugin and I have the code successfully switching up the class of specified divs.
What I can't figure out is how to get it to stop jumping to the anchor tags.. I want it to stay wherever the user is currently positioned scroll-wise.
here's my code...
$(window).hashchange( function(){
$(function(){
$('.eventselected').removeClass('eventselected');
$(location.hash).addClass('eventselected');
});
});
$(window).trigger( 'hashchange' );
The situation is that I want the class added when the user comes to the page from an external link and when a link is clicked while on the parent page. Just don't want it to jump!
the link's class is .side-eventmore
I've tried revising my code with this... but is still doesn't work...
$( ".side-eventmore" ).click(function(e) {
e.preventDefault();
var hash = $(this).attr("href").replace('events.cfm', '') || 'blank';
document.location.hash = hash;
});
$(window).hashchange( function(ev){
ev.preventDefault();
$(function(){
$('.eventselected').removeClass('eventselected');
$(location.hash).addClass('eventselected');
});
});
$(window).trigger( 'hashchange' );
Upvotes: 3
Views: 1402
Reputation: 107
OK, figured it out.
What I had to end up doing was manually change each of my affected div's id's to the hashtag plus "-id", but kept the links to just the hashtag sans "-id" so..
<div id="#hash-id"></div>
<a href="#hash">link</a>
then in the code I applied the new class to only #hash-id ... like so...
$(window).hashchange( function(){
var selectid = location.hash;
selectid += '-id';
$(function(){
$('.eventselected').removeClass('eventselected');
$(selectid).addClass('eventselected');
});
});
$(window).trigger( 'hashchange' );
Upvotes: 2