Jump to content

Need Help for automatically highlight button link


sgtelias

Recommended Posts

I try to explain my problem. I've a nav bar like this:

 

<div id="navbar">

<ul>

<li><a>Link1</a></li>

<li><a>Link2</a></li>

<li><a>Link3</a></li>

</ul>

</div>

 

I'm trying to find a mode to automatically highlight the button link, first the link1, after some seconds the link2, etc.

I've tried some code, but nothing, also because i'm studying javascript yet.

Can you help me?

Link to comment
Share on other sites

You could try something like this.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title</title>
<style>
.highlight{
color:#0ff;
background-color:#ddd;
}
</style>
<script>
var highlight;
window.onload = cycle;

function cycle(){
var str;
var list = document.getElementById('navbar').getElementsByTagName('A');
if(highlight != undefined){alert('list['+highlight+'].className='+list[highlight].className);}
if (highlight === undefined){
   highlight = -1;
}else if (highlight === list.length-1){
   if (list[highlight].className.indexOf('highlight') != -1){
      str = list[highlight].className.replace('highlight','');
      list[highlight].className = str;
      highlight = -1;
   }else{
      highlight = -1;
   }
}else{
   if (list[highlight].className.indexOf('highlight') != -1 ){
      str = list[highlight].className.replace('highlight','');
      list[highlight].className = str;
   }
}
highlight++;
if (list[highlight].className.indexOf('highlight') != -1 ){
  // do nothing
}else{
  list[highlight].className += ' highlight';
}
setTimeout(cycle,500);
}

</script>
</head>

<body>

<div id="navbar">
<ul>
<li><a>Link1</a></li>
<li><a>Link2</a></li>
<li><a>Link3</a></li>
</ul>
</div>

</body>
</html>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...