Improve company productivity with a Business Account.Sign Up

x
?
Solved

Jquery open tab based on url

Posted on 2015-02-24
2
Medium Priority
?
460 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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article discusses how to implement server side field validation and display customized error messages to the client.
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)

595 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