Molly
Molly

Reputation: 13

resume function on mouse out

I am working on a page with javascript/jQuery and am almost finished. I am supposed to have rotating content (the function 'rotatecontent') and divs which show up in the rotating content's place when the relevant button is hovered over. I have accomplished all this except when I hover away from a button, all div content disappears and the original rotatecontent function does not resume. I need to implement a function to have it resume once a user hovers away from the button. Here is my JS:

<script type="text/javascript">

var messages;
var curcontentindex = 0;
var prevcontentindex;
var futcontentindex;
var i;



function rotatecontent() {
messages.hide();
curcontentindex = (curcontentindex < messages.length - 1) ? curcontentindex + 1 : 0;
messages.get(curcontentindex).style.display = "block";
}


$(function() {
messages = $('.dyncontent').find('div');
i = setInterval(rotatecontent, 1000);


$('li').hover(
function () {
$(this).addClass("hover");
clearInterval(i); // when mouse is over object
},
function () {
$(this).removeClass("hover");
setInterval(rotatecontent, 1000); //when mouse is no longer over object
}
);

});




</script>

And here is my HTML:

<div class="dyncontent">
<div id="div1">Be A Billiken </div>
<div id="div2" style="display:none">Be A Billiken 2</div>
<div id="div3" style="display:none">Be A Billiken 3</div>
<div id="div4" style="display:none">Be A Billiken 4</div>
<div id="div5" style="display:none">Be A Billiken 5</div>
</div>



<ul id="container" overflow:hidden>

<li><a href="#" onMouseOver="$('div.dyncontent').replaceWith($('#div1').show());" onMouseOut=""><img     src="../Work/Images/BAB.com_web_link_10.jpg"  width="250" height="100" class="Bab-image"></a></li>


<li><a href="#" onMouseOver="$('div.dyncontent').replaceWith($('#div2').show());" onMouseOut=""><img     src="../Work/Images/SLU_on_the_Road.jpg" class="sluotr-image">    </a></li>

<li><a href="#" onMouseOver="$('div.dyncontent').replaceWith($('#div3').show());"     onMouseOut=""><img src="http://slu.edu/Images/graduate/Billiken_Blogs.jpg" class="blogs-    image"></a></li>

<li ><a href="#" onMouseOver="$('div.dyncontent').replaceWith($('#div4').show());"     onMouseOut=""><img src="http://slu.edu/Images/graduate/SLU_Chat.jpg" class="chat-image">    </a></li>

<li><a href="#" onMouseOver="$('div.dyncontent').replaceWith($('#div5').show());"     onMouseOut=""><img src="http://slu.edu/Images/graduate/Viewbook_Button_2011.jpg"     class="view-image"></a>
</li>

</ul>

Upvotes: 0

Views: 504

Answers (1)

Itamar
Itamar

Reputation: 534

The jQuery .hover() function has a special structure which allows you to set what happens once the mouse is over the object, and what happens after the mouse is no longer pointing to the object.

$(".class").hover(
  function () {
    $(this).addClass("hover"); // when mouse is over object
  },
  function () {
    $(this).removeClass("hover"); //when mouse is no longer over object
  }
);

Save the original settings and then use them in the 'mouse-not-over' section

Upvotes: 5

Related Questions