Reputation: 11
I have searched here and on the Internet for code that can re-enable an onMouseOut
that has been disabled in the onClick
code. I have found things but nothing that works in my situation.
On the page there is a container div that holds two divs, one for an image and one for its corresponding text. Under the image are four links (1, 2, 3, 4) laid out in an unordered list. When the visitor rolls over the #2, the image needs to change to img2, the matching text div needs to go from "hidden" to "show". The onMouseOut
resets the image and text to the original versions. The onClick
changes the image/text to whatever # was clicked and then disables the onMouseOut
.
Now I need a way to make the onMOuseOut
work again the next time the visitor rolls over that #link. As I have it now, once the onMouseOut
is disabled, it stays disabled until I reload the page.
Here is the code so far:
<head>
<script>
if (document.images) {
book1 = new Image
book2 = new Image
book3 = new Image
book4 = new Image
book1.src = "/llb/assets/book1.jpg"
book2.src = "/llb/assets/book2.jpg"
book3.src = "/llb/assets/book3.jpg"
book4.src = "/llb/assets/book4.jpg"
}
function swapImage(thisImage,newImage) {
if (document.images) {
document[thisImage].src = eval(newImage + ".src")
}
}
function show_visibility(IDS){
hide_visibility();
document.getElementById(IDS).style.display = 'block';
}
function hide_visibility(){
var sel = document.getElementById('bookleadin').getElementsByTagName('div');
for (var i=0; i<sel.length; i++) { sel[i].style.display = 'none'; }
}
</script>
</head>
<body>
<div id="content">
<div id="books">
<div id="bookimages">
<img id="bookcover" name="bookcover" src="../llb/assets/book1.jpg" />
<ul>
<li><a href="#" onclick="swapImage('bookcover','book1'); show_visibility('bt1'); this.onmouseout=''" onMouseOver="swapImage('bookcover','book1')" onMouseOut="swapImage('bookcover','book1')">1</a></li>
<li><a href="#" onclick="swapImage('bookcover','book2'); show_visibility('bt2'); this.onmouseout=''" onMouseOver="swapImage('bookcover','book2')" onMouseOut="swapImage('bookcover','book1')">2</a></li>
<li><a href="#" onclick="show_visibility('bt3'); swapImage('bookcover','book3')" onMouseOver="swapImage('bookcover','book3')" onMouseOut="swapImage('bookcover','book1')" onclick="swapImage('bookcover','book3')">3</a></li>
<li><a href="#" onclick="show_visibility('bt4'); swapImage('bookcover','book4')" onMouseOver="swapImage('bookcover','book4')" onMouseOut="swapImage('bookcover','book1')" onclick="swapImage('bookcover','book4')">4</a></li>
</ul>
</div><!-- end bookimages -->
<div id="bookleadin">
<div id="bt1" style="display:block;"><p>Writing starts with living...</p>
</div>
<div id="bt2" style="display:none;"><p>The air is silk...</p>
</div>
<div id="bt3" style="display:none;"><p>I lived in the woods...</p>
</div>
<div id="bt4" style="display:none;"><p>I tried to forget...</p>
</div>
</div><!-- end bookleadin -->
<div class="spacer"></div>
</div><!-- ends books -->
</div><!-- end content -->
list item #2 is the example I was testing with. I am still a Javascript beginner/intermediate, so any answer has to be simple enough for me to understand and apply to this project or complete so that I can just copy/paste it.
Upvotes: 1
Views: 844
Reputation: 2610
You could set a flag at onclick
to be verified if it has been clicked if yes then do the swapping onmouseout
else do nothing but do not do this.onmouseout = ""
.
EDIT:
<head>
<script>
if(document.images) {
book1 = new Image
book2 = new Image
book3 = new Image
book4 = new Image
book1.src = "/llb/assets/book1.jpg"
book2.src = "/llb/assets/book2.jpg"
book3.src = "/llb/assets/book3.jpg"
book4.src = "/llb/assets/book4.jpg"
}
var swap_list = {}; // a list of swapped elements
function swapImage(thisImage, newImage) {
if(typeof swap_list[thisImage] == 'undefined' || swap_list[thisImage]=='')
if(document.images) {
document[thisImage].src = eval(newImage + ".src");
swap_list[thisImage] = newImage;
}
else
{
swap_list[thisImage] = '';
document[thisImage].src = "../llb/assets/book1.jpg";
}
}
function show_visibility(IDS) {
hide_visibility();
document.getElementById(IDS).style.display = 'block';
}
function hide_visibility() {
var sel = document.getElementById('bookleadin').getElementsByTagName('div');
for(var i = 0; i < sel.length; i++) {
sel[i].style.display = 'none';
}
}
</script>
</head>
<body>
<div id="content">
<div id="books">
<div id="bookimages">
<img id="bookcover" name="bookcover" src="../llb/assets/book1.jpg" />
<ul>
<li>
<a href="#" onclick="swapImage('bookcover','book1'); show_visibility('bt1'); " onMouseOver="swapImage('bookcover','book1')" onMouseOut="swapImage('bookcover','book1')">1</a>
</li>
<li>
<a href="#" onclick="swapImage('bookcover','book2'); show_visibility('bt2'); " onMouseOver="swapImage('bookcover','book2')" onMouseOut="swapImage('bookcover','book1')">2</a>
</li>
<li>
<a href="#" onclick="show_visibility('bt3'); swapImage('bookcover','book3')" onMouseOver="swapImage('bookcover','book3')" onMouseOut="swapImage('bookcover','book1')" onclick="swapImage('bookcover','book3')">3</a>
</li>
<li>
<a href="#" onclick="show_visibility('bt4'); swapImage('bookcover','book4')" onMouseOver="swapImage('bookcover','book4')" onMouseOut="swapImage('bookcover','book1')" onclick="swapImage('bookcover','book4')">4</a>
</li>
</ul>
</div><!-- end bookimages -->
<div id="bookleadin">
<div id="bt1" style="display:block;">
<p>
Writing starts with living...
</p>
</div>
<div id="bt2" style="display:none;">
<p>
The air is silk...
</p>
</div>
<div id="bt3" style="display:none;">
<p>
I lived in the woods...
</p>
</div>
<div id="bt4" style="display:none;">
<p>
I tried to forget...
</p>
</div>
</div><!-- end bookleadin -->
<div class="spacer"></div>
</div><!-- ends books -->
</div><!-- end content -->
</body>
Upvotes: 0