Solved

Jquery open tab based on url

Posted on 2015-02-24
2
290 Views
Last Modified: 2015-02-25
See my code below. How do I adapt it so it automatically opens the tab based on the url and scroll down to the correct position on the page.

i.e. mydomain.com/page.php#gallery   will open gallery tab and move down.

Jquery
$(function () {
    var info_tabContainers = $('#Tabs-Container-Contents > div');
    
    $('div.info_tabs ul.info_tabNavigation a').click(function () {
        info_tabContainers.hide().filter(this.hash).show();
        
        $('div.info_tabs ul li').removeClass('Info_Tabs_selected');
        $(this).parent().addClass('Info_Tabs_selected');
        
        return false;
    }).filter(':first').click();
	});

Open in new window


html
<div id="Info_Tabs">

<div class="info_tabs">
  <!-- tabs -->
  <ul class="info_tabNavigation">
    <li class="Info_Tabs_selected"><a href="#what">What You'll See</a></li>
    <li><a href="#gallery">Showroom Gallery</a></li>
    <li><a href="#visitor">Showroom Location</a></li>
  </ul>

  <div id="Tabs-Container-Contents">
  <!-- tab containers -->
  <div id="what" class="Info_Tabs-Container">
      
     tab 1 content
      
  </div>
  <div id="gallery" class="Info_Tabs-Container">
    
    tab 2 content
    
  </div>
  <div id="visitor" class="Info_Tabs-Container">
     tab 3 content
  </div>
  </div>
</div>

</div>

Open in new window

0
Comment
Question by:petewinter
2 Comments
 
LVL 18

Accepted Solution

by:
nap0leon earned 500 total points
ID: 40629138
To scroll to a hash based on a page load event, you need to add anchors with the name of the hash.
e.g.,
<a name="gallery">

Open in new window


Now to make the page scroll to that location when you update the hash outside of a pageload event, you can trigger the ".animate" method inside of your .click() routine that does the hide/show stuff.  Here is a function I found on stackoverflow that works nicely:
function scrollToAnchor(aid){
    var aTag = $("a[name='"+ aid +"']");
    $('html,body').animate({scrollTop: aTag.offset().top},'slow');
}

Open in new window


and add this line to your .click() routine:
scrollToAnchor(this.hash);

Open in new window


Altogether, it looks like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Q_28623324.html</title>
	<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

<style type="text/css">
.make_page_tall_enough_to_cause_scrolling { height:1000px; }
</style>
</head>

<body>
<div id="Info_Tabs">

	<div class="info_tabs">
	  <!-- tabs -->
	  <ul class="info_tabNavigation">
	    <li class="Info_Tabs_selected"><a href="#what">What You'll See</a></li>
	    <li><a href="#gallery">Showroom Gallery</a></li>
	    <li><a href="#visitor">Showroom Location</a></li>
	  </ul>
	
	  <div id="Tabs-Container-Contents">
	  <!-- tab containers -->
	  <div id="what" class="Info_Tabs-Container">
	     <a name="what">
	     tab 1 content
	      
	  </div>
	  <div id="gallery" class="Info_Tabs-Container">
     	    <a name="gallery">
	    tab 2 content
	    
	  </div>
	  <div id="visitor" class="Info_Tabs-Container">
     	    <a name="visitor">
	     tab 3 content
	  </div>
	  </div>
	</div>
</div>
<div class="make_page_tall_enough_to_cause_scrolling">
	<p>Some stuff here</p>
</div>
<script>
function scrollToAnchor(aid){
    var aTag = $("a[name='"+ aid +"']");
    $('html,body').animate({scrollTop: aTag.offset().top},'slow');
}

$(function () {
    var info_tabContainers = $('#Tabs-Container-Contents > div');
    
    $('div.info_tabs ul.info_tabNavigation a').click(function () {
        info_tabContainers.hide().filter(this.hash).show();
        
        $('div.info_tabs ul li').removeClass('Info_Tabs_selected');
        $(this).parent().addClass('Info_Tabs_selected');
	scrollToAnchor(this.hash);
        
        return false;
    }).filter(':first').click();
});
</script>
                                  
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:petewinter
ID: 40630099
Many thanks
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question