Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Javascript Timed Tabs

Posted on 2008-10-04
8
Medium Priority
?
294 Views
Last Modified: 2013-11-19
I have a box with three tabs at top: when clicked each tab displays the latest posts from a blog (so there are three blogs total). It works fine, but in addition I would like each tab to cycle through every 8 seconds or so without the user clicking the tabs, to give each blog equal face time on the home page (the first blog, with the tab farthest left, is the one that opens by default on page load). Also, when the user clicks on one of the blogs, I want the timer to be deactivated so that the user just remains on the selected blog.
CURRENT JAVASCRIPT FOR THE TABS:
 
function TchangeOpinionsTab(Twhich,Ttab) {
	document.getElementById("Ttab-display").innerHTML = document.getElementById("Tpromo" + Twhich).innerHTML;
	if (Ttab != TopinionsTabOpen) {
		document.getElementById(Ttab).className = "feature-tab-open";
		document.getElementById(TopinionsTabOpen).className = " ";
		TopinionsTabOpen = Ttab;	
	}
}
 
THE HTML (ignore the PHP calls and commands): 
 
<script>
	  // Change the Ttab# to whichever tab should be the featured tab
	  var TopinionsTabOpen = "Ttab1";
	  </script> 
 
<div id="feature-tabs"> 
<ul class="tabs"> 
 
	<li id="Ttab1" class="feature-tab-open" style="border-left:#FFF;">
	<a href="javascript:TchangeOpinionsTab('1','Ttab1');" title="Talk">Life Eternal</a>
	</li> 
 
	<li id="Ttab2">
	<a href="javascript:TchangeOpinionsTab('2','Ttab2');" title="On Faith">The Digital World</a>
	</li>
 
 	<li id="Ttab3">
	<a href="javascript:TchangeOpinionsTab('3','Ttab3');" title="PostGlobal">Music</a>
	</li> 
 
</ul> 
 
<div class="clearboth"></div> 
 
<div id="Ttab-display">  
 
<div class="clearboth"></div> 
 
<div class="tab-box-bg"> 
 
<div class="tab-box-pad"> 
 
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii"> 
 
<div class="inner-top"> 
<div id="JesusChrist"><a href="http://www.berrettfamily.org/index.php?scustom=ThisisLifeEternal"><img style="margin-left:5px; margin-bottom:5px" src="http://www.berrettfamily.org/skins/v1/test/JesusChrist.jpg"></a></div>
<%ItemListEX(9,,,,LifeEternal)%>
 
<div class="clearboth"></div> </div> 
 
<div class="clearboth"></div> 
 
 
 
</div> </div>
 
<div class="clearbothplus"></div>
 
 </div> 
 
<div id="Tpromo1">
 
<div class="clearboth"></div> 
<div class="tab-box-bg"> 
<div class="tab-box-pad"> 
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii"> 
<div class="inner-top"> 
<div id="JesusChrist"><a href="http://www.berrettfamily.org/index.php?scustom=ThisisLifeEternal"><img style="margin-left:5px; margin-bottom:5px" src="http://www.berrettfamily.org/skins/v1/test/JesusChrist.jpg"></a></div>
<%ItemListEX(9,,,,LifeEternal)%>
<div class="clearboth"></div> </div> 
<div class="clearboth"></div> 
</div> </div> 
<div class="clearbothplus"></div> </div> 
 
<div id="Tpromo2"> 
<div class="clearboth"></div> 
<div class="tab-box-bg"> 
<div class="tab-box-pad"> 
<div class="inner-top"> 
<img style="margin-left:12px; border: solid 0px #C7C7C7;" src="http://www.berrettfamily.org/skins/v1/test/The-Digital-World.jpg">
<%otherblog(tech,v1/headlines,9)%>
<div class="clearboth"></div> </div> 
<div class="clearboth"></div> 
</div> </div> 
<div class="clearbothplus"></div> </div> 
 
<div id="Tpromo3"> 
<div class="clearboth"></div> 
<div class="tab-box-bg"> 
<div class="tab-box-pad"> 
<div class="inner-top"> 
<img style="margin-left:7px; border: solid 0px #C7C7C7;margin-bottom:5px;" src="http://www.berrettfamily.org/skins/v1/test/Music-Blog.jpg">
<%otherblog(musicblog,v1/headlines,9)%>
<div class="clearboth"></div> </div> 
<div class="clearboth"></div> 
</div> </div> 
 
<div class="clearbothplus"></div> </div> 
 
 
</div> <div class="clearboth"></div> 
 
AND THE CSS:
 
/****************************************************************************************
Blog Box
****************************************************************************************/
 
ul {margin:0; padding:0; list-style-type:none;}
 
ul li {display:inline-block; padding:0 0 0 0px; line-height:1.4;}
 
.clearboth {clear:both;}
 
.clearbothplus {clear:both; height:1px; overflow:hidden; margin-top:-1px;}
 
/* BEGIN OPINIONS TABS */
 
#hp #feature-tabs {float:left; width:296px; border: solid 1px #9E9C9C; background:#FFF; margin-left:16px; margin-top:10px; margin-bottom:50px;}
 
#hp #feature-tabs ul.tabs {float:left; width:100%; margin:0; padding:0; font:bold 11px Arial; color:#333;}
 
#hp #feature-tabs ul.tabs {margin:0; padding:0; list-style:none;}
 
#hp #feature-tabs ul.tabs li 
{
background:#D8D7D7;
border-left: 1px solid #999; 
border-top: 0px solid #999; 
border-right: 0px solid #999; 
border-bottom: 1px solid #999; 
float:left; margin:0; padding:2px 4.5px 2px 4px; margin-right:0px; font:bold 13px Arial, Helvetica, sans-serif;}
 
 
#hp #feature-tabs ul.tabs a {color: #014377; outline:none; float:left; display:block; ; padding:6px 8px 5px 8px; text-decoration:none;}
 
#hp #feature-tabs ul.tabs .feature-tab-open {border-bottom:0px solid #999;background:#FFF;}
 
#hp #feature-tabs ul.tabs .feature-tab-open a {padding-bottom:6px; color:#014377;}
 
#hp #promo1 {display:none; visibility:hidden;}
 
#hp #promo2 {display:none; visibility:hidden;}
 
#hp #container {width:970px; margin:0 0 0 10px; padding:10px 0;}
 
#hp #Tpromo1 {display:none; visibility:hidden;}
 
#hp #Tpromo2 {display:none; visibility:hidden;}
 
#hp #Tpromo3 {display:none; visibility:hidden;}
 
#hp #feature-tabs #tab-display {position:relative;}
 
#hp #feature-tabs #Ttab-display {position:relative;}
 
 
#hp #feature-tabs .tab-box-pad {padding:14px 9px 0 9px;}
 
#hp #feature-tabs .inner-top {float:left; width:290px; padding-bottom:3px;}
 
#hp #feature-tabs .inner-top .lft {float:left; width:182px; margin-right:8px;}
 
#hp #feature-tabs .inner-top .rt {float:left; width:80px; padding-top:4px;}

Open in new window

0
Comment
Question by:dainberrett
[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
  • 4
  • 4
8 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22643526
try:
<script>
//CURRENT JAVASCRIPT FOR THE TABS:
 
function TchangeOpinionsTab(Twhich,Ttab) {
	document.getElementById("Ttab-display").innerHTML = document.getElementById("Tpromo" + Twhich).innerHTML;
	if (Ttab != TopinionsTabOpen) {
		document.getElementById(Ttab).className = "feature-tab-open";
		document.getElementById(TopinionsTabOpen).className = " ";
		TopinionsTabOpen = Ttab;	
	}
}
 
//THE HTML (ignore the PHP calls and commands): 
 
	  // Change the Ttab# to whichever tab should be the featured tab
	  var TopinionsTabOpen = "Ttab1";
	  
 
var last=-1;
var tabCount=3;
 
window.onload=init;
function init(){
	last = (last + 1) % tabCount;
	TchangeOpinionsTab( (last+1), 'Ttab'+(last+1) )
	setTimeout(init,8000);
}
</script>

Open in new window

0
 

Author Comment

by:dainberrett
ID: 22645281
Wow, hielo, you are indeed a genius. Thank you so much for your help! That worked perfect!!

At the risk of being extremely obnoxious, I thought of something else. Would it be possible to have the tab change be a fade? I have no idea if there is a ready Javascript command for that, or if it involves a ton of other stuff. Either way, it doesn't really matter. What you gave me works great. But is there a simply way to do that?
0
 
LVL 82

Expert Comment

by:hielo
ID: 22645416
>> I have no idea if there is a ready Javascript command for that
No, you would need to make that effect your self. There are javascript libraries (jQuery, scriptaculous,etc.) that provide such effect, but you will need to get familiar with them to put them to good use.
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

Author Comment

by:dainberrett
ID: 22645496
Awesome, Hielo, I was just curious. Thanks again for the help!
0
 

Author Comment

by:dainberrett
ID: 22645551
Hielo...one last question on something I just noticed on this...

It works great, cycling through each tab. The only thing is that when I actually click on one of the tabs, it still keeps cycling through. Would it be possible to make it so that once clicked, the rotation is deactivated so the user can browse the chosen tab contents as long as they like?
0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 22645882
try this:
<script>
//CURRENT JAVASCRIPT FOR THE TABS:
 
function TchangeOpinionsTab(Twhich,Ttab) {
	document.getElementById("Ttab-display").innerHTML = document.getElementById("Tpromo" + Twhich).innerHTML;
	if (Ttab != TopinionsTabOpen) {
		document.getElementById(Ttab).className = "feature-tab-open";
		document.getElementById(TopinionsTabOpen).className = " ";
		TopinionsTabOpen = Ttab;	
	}
}
 
//THE HTML (ignore the PHP calls and commands): 
 
 
	  // Change the Ttab# to whichever tab should be the featured tab
	  var TopinionsTabOpen = "Ttab1";
 
var last=-1;
var tabCount=3;
 
var timer=null;
window.onload=init;
function init(){
	last = (last + 1) % tabCount;
	TchangeOpinionsTab( (last+1), 'Ttab'+(last+1) )
	timer = setTimeout(init,8000);
}
 
function resetTimer(){
	clearTimeout(timer);
timer=null;
}
	  </script> 
 
 
AND update your tabs with this:
<ul class="tabs"> 
 
	<li id="Ttab1" class="feature-tab-open" style="border-left:#FFF;">
	<a href="javascript:resetTimer();TchangeOpinionsTab('1','Ttab1');" title="Talk">Life Eternal</a>
	</li> 
 
	<li id="Ttab2">
	<a href="javascript:resetTimer();TchangeOpinionsTab('2','Ttab2');" title="On Faith">The Digital World</a>
	</li>
 
 	<li id="Ttab3">
	<a href="javascript:resetTimer();TchangeOpinionsTab('3','Ttab3');" title="PostGlobal">Music</a>
	</li> 
 
</ul> 

Open in new window

0
 

Author Closing Comment

by:dainberrett
ID: 31503049
Wow again, thanks Hielo. You are awesome, it works perfectly. I really appreciate the help! Best wishes!
0
 
LVL 82

Expert Comment

by:hielo
ID: 22646732
you are welcome. Take care.
0

Featured Post

Looking for a new Web Host?

Lunarpages' assortment of hosting products and solutions ensure a perfect fit for anyone looking to get their vision or products to market. Our award winning customer support and 30-day money back guarantee show the pride we take in being the industry's premier MSP.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

721 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