I have a status message box (div box) positioned at the bottom of a web page using position: fixed; and bottom: 0;. Its height is initially 11px.
I want to allow users to double click it when there are more status messages than what can fit within the default hight, make it grow. If they double click it again or move the mouse away from the box, it should shrink again.
I managed to get this working exactly as I want, but it seems to me that it should be possible to write this more elegantly:
<script type='text/javascript'> $(document).ready(function() { $('#footer').dblclick(showStatusBar); }); function showStatusBar() { var footer = $('#footer'); footer.unbind('dblclick', showStatusBar); footer.animate({ height: '100px' }, 200); footer.dblclick(hideStatusBar); footer.bind('mouseleave', hideStatusBar); } function hideStatusBar() { var footer = $('#footer'); footer.unbind('mouseleave', hideStatusBar); footer.unbind('dblclick', hideStatusBar); footer.animate({ height: '11px' }, 200); footer.dblclick(showStatusBar); } </script>
I played around with the toggle event, but was unable to get it working.
You can create one function which acts as a toggle function. Something like this:
This gives the status bar an ‘isHidden’ property, and uses it to check if we are showing or hiding the status bar. This function also works with other elements if you need it to.
(That that you can chain many jQuery commands, as I did once above with the ‘stop’ and ‘animate’ functions.)