Solved

Jquery open tab based on url

Posted on 2015-02-24
2
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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)

749 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