nick9995
04-29-2008, 08:02 PM
Hi all. I am trying to make a slide out menu in javascript but I am having several problems. My goal is for the submenu to slide out when the mouse hovers over a div object. I made a menu in css which accomplishes this but I would like the menu to come out smoothly. I just feel it would make my website feel more dynamic and attractive to visitors. Here is my code:
<html>
<head>
<style type="text/css">
#object1 {
position: absolute;
top: 0px;
left: 0px;
display: block;
z-index: 2;
width: 40px;
height: 40px;
}
#image1 {
position: absolute;
top: 0px;
left: 80px;
color: #CCCCCC;
display: block;
z-index: 1;
width: 15px;
height: 40px;
}
</style>
<SCRIPT language="JavaScript">
<!--
var xpos = 0;
function moveright() {
if (document.all) {
var object = document.all.image1.style;
if (xpos < 400)
{
object.left = xpos;
xpos += 10;
setInterval("moveright('+xpos+');", 50);
}
}
}
function moveleft() {
if (document.all) {
var object = document.all.image1.style;
if (xpos > 70)
{
object.left = xpos;
xpos -= 10;
setInterval("moveleft('-xpos-');", 50);
}
}
}
//-->
</SCRIPT>
</head>
<div onmouseover="moveright()" onmouseout="moveleft()" id="object1"><h1>Menu</h1></div>
<div id="image1"><h1>Submenu</h1></div>
</html>
When I run it and put my cursor over the div 'menu', the div ‘image1’ moves to the right, however when I take my cursor away it becomes erratic jumping back and forth. I feel like both functions are running at the same time and ‘fighting’ each other. I know moveright() starts when it’s called however does it stop when moveleft() is called? Also if anybody could explain either why this method doesn’t work or advise me to try another method that would be great. Thanks.
NIck
<html>
<head>
<style type="text/css">
#object1 {
position: absolute;
top: 0px;
left: 0px;
display: block;
z-index: 2;
width: 40px;
height: 40px;
}
#image1 {
position: absolute;
top: 0px;
left: 80px;
color: #CCCCCC;
display: block;
z-index: 1;
width: 15px;
height: 40px;
}
</style>
<SCRIPT language="JavaScript">
<!--
var xpos = 0;
function moveright() {
if (document.all) {
var object = document.all.image1.style;
if (xpos < 400)
{
object.left = xpos;
xpos += 10;
setInterval("moveright('+xpos+');", 50);
}
}
}
function moveleft() {
if (document.all) {
var object = document.all.image1.style;
if (xpos > 70)
{
object.left = xpos;
xpos -= 10;
setInterval("moveleft('-xpos-');", 50);
}
}
}
//-->
</SCRIPT>
</head>
<div onmouseover="moveright()" onmouseout="moveleft()" id="object1"><h1>Menu</h1></div>
<div id="image1"><h1>Submenu</h1></div>
</html>
When I run it and put my cursor over the div 'menu', the div ‘image1’ moves to the right, however when I take my cursor away it becomes erratic jumping back and forth. I feel like both functions are running at the same time and ‘fighting’ each other. I know moveright() starts when it’s called however does it stop when moveleft() is called? Also if anybody could explain either why this method doesn’t work or advise me to try another method that would be great. Thanks.
NIck