Changing multiple div background onclick , onmouseout and onmouseover
I'm working in a project here for over 30 hours and I might not be thinking straight.
Here's my situation. I have basically 4 divs that changes they background in diferents ways depending on the mouse action.
Lets call those divs here as div_1, div_2, div_3 and div_4.
When the user pass the mouse over the bottons on the menu the div_1 and div_2 should change they background to the background related to the button.
When the user click on a botton all the 4 divs should change they background and keep it even if the user move the mouse elsewhere.
And the last situation that you need to imagine is the one where the user clicked on one botton and after he pass the mouse over another botton. On this case, the div_1 and div_2 will still change but if the user remove the mouse from the menu div_1 and div_2 should return to the background related to the same background as div_3 and div_4.
I would appreciate if someone could help me to realize whats wrong with my code. Tnx.
Here is my code:
<ul>
<li id="menu-a1" onclick="Menu('a1','click');" onmouseover="('a1','over');" onmouseout="Menu('a1','out');" > <a href="#">Menu_a1</a> </li>
<li id="menu-a2" onclick="Menu('a2','click');" onmouseover="('a2','over');" onmouseout="Menu('a2','out');" > <a href="#">Menu_a2</</a> </li>
<li id="menu-a3" onclick="Menu('a3','click');" onmouseover="('a3','over');" onmouseout="Menu('a3','out');" > <a href="#">Menu_a3</</a> </li>
</ul>
<div id=div_1></div>
<div id=div_2></div>
<div id=div_3></div>
<div id=div_4></div>
heres my java script:
function Menu(where, action) {
switch (action) {
case 'click':
if ($('#menu-'+where).hasClass('active')) {
ClearMenu();
$('#menu-'+where).removeClass('active');
} else {
$('#menu-'+where).addClass('active');
ClearMenu();
ActiveMenu(where);
}
break;
case 'over':
ActiveMenu(where);
OverMenu(where);
break;
case 'out':
ActiveMenu(where);
break;
default: break;
}
}
function ClearMenu(){
// Removing Classes
$('#div_1').removeClass('a1 a2 a3');
$('#div_2').removeClass('a1 a2 a3');
$('#div_3').removeClass('a1 a2 a3');
$('#div_4').removeClass('a1 a2 a3');
function OverMenu(where){
$('#div_1').addClass(where);
$('#div_2').addClass(where);
}
function ActiveMenu(where){
// Adding Classes
$('#div_1').addClass(where);
$('#div_2').addClass(where);
$('#div_3').addClass(where);
$('#div_4').addClass(where);
}
and here is my css:
#div_1.a1 {background:url(background_div1_a1.jpg)}
#div_1.a2 {background:url(background_div1_a2.jpg)}
#div_1.a3 {background:url(background_div1_a3.jpg)}
#div_2.a1 {background:url(background_div2_a1.jpg)}
#div_2.a2 {background:url(background_div2_a2.jpg)}
#div_2.a3 {backgr开发者_Python百科ound:url(background_div2_a3.jpg)}
#div_3.a1 {background:url(background_div3_a1.jpg)}
#div_3.a2 {background:url(background_div3_a2.jpg)}
#div_3.a3 {background:url(background_div3_a3.jpg)}
#div_4.a1 {background:url(background_div4_a1.jpg)}
#div_4.a2 {background:url(background_div4_a2.jpg)}
#div_4.a3 {background:url(background_div4_a3.jpg)}
In your OverMenu
function, you've got 'where'
in quotes, presumably you want to reference the function parameter and not a string? Get rid of the quotes here.
Also, your CSS is wrong: you can't just give it a filename and expect it to know what to do, you have to set the background-image
property:
#div_1.a1 { background-image: url(background_div1_a1.jpg); }
Also, in the onmouseover
bits in your HTML, you're not actually calling a function. Presumably you want Menu
in here.
UPDATE:
I would use jQuery for this. I've done an example on jsFiddle - http://jsfiddle.net/GrahamClark/gLGUQ/4/ - hopefully this is enough to get you started.
精彩评论