I spent an hour trying to get the jQuery UI accordion to work and it just wasn’t right. So I wrote my own. And then, the next day, I made it simpler.
Get the files here.
Programming by Ethan Georgi
I spent an hour trying to get the jQuery UI accordion to work and it just wasn’t right. So I wrote my own. And then, the next day, I made it simpler.
Get the files here.
For a client’s site, we had some partner logos in the lower left corner. Rather than let them take up a lot of space, we decided to have them rotate. Here’s the HTML.
<div id="partners"> <a href="http://www.nationalgrid.com" target="_blank"><img src="/wp-content/themes/CEG/images/footer_logos/botlogosNATGRID.gif" alt="National Grid" /></a> <br /> <a href="http://www.nystar.state.ny.us/" target="_blank"><img src="/wp-content/themes/CEG/images/footer_logos/botlogosNYSTAR.gif" alt="NYSTAR" /></a> <br /> <a href="http://www.mep.nist.gov/" target="_blank"><img src="/wp-content/themes/CEG/images/footer_logos/botlogosMEP.gif" alt="NIST/MEP" /></a> <br /> <a href="http://www.nist.gov/" target="_blank"><img src="/wp-content/themes/CEG/images/footer_logos/botlogosNIST.gif" alt="NIST/MEP" /></a> <br /> </div>
And here’s the jQuery.
var imageFader; $(document).ready(function() { // get the images in the lower left going $('#partners').each(function() { $(this).children('br').hide(); var speed = 1500; var pause = 5000; var next = 1; var counter = 0; // how many images are there? var max = $(this).children('a').length; $(this).children('a:gt(0)').hide(); function fadeImages() { if (counter >= max) counter = 0; next = counter + 1; if (next >= max) next = 0; $("#partners").children('a:eq(' + counter.toString() + ')').slideUp(speed, function() { $("#partners").children('a:eq(' + next.toString() + ')').slideDown(speed); }); counter++; } // end function fadeImages imageFader = setInterval(fadeImages, pause); }); });
Here’s some handy jQuery code to show/hide menu items in Wordpress’s sidebar.
function sidebarHide(element) { // this function hides the ul // also calls sidebarPlus $('#sidebar ul ' + element +' ul').each(function() { $(this).hide('fast'); }); sidebarPlus(element); return false; } function sidebarShow(element) { // this function shows the ul // also calls sidebarMinus $('#sidebar ul ' + element +' ul').each(function() { $(this).show('fast'); }); sidebarMinus(element); return false; } function sidebarPlus(element) { // function appends a + to the title // this plus is a link to show the contents $('#sidebar ul ' + element + ' h2').each(function() { var text = $(this).text(); var plus = text.lastIndexOf(" -"); if (plus > 0) { text = text.substring(0, plus); } var link = '<a href="#" onclick="return sidebarShow(\'' + element + '\');">' + text + ' +</a>'; $(this).html(link); }); } function sidebarMinus(element) { // function appends a - to the title // this minus is a link to hide the contents $('#sidebar ul ' + element + ' h2').each(function() { var text = $(this).text(); var minus = text.lastIndexOf(" +"); if (minus) { text = text.substring(0, minus); } var link = '<a href="#" onclick="return sidebarHide(\'' + element + '\');">' + text + ' -</a>'; $(this).html(link); }); } $(document).ready(function() { $('#sidebar ul li').each(function() { var n = $(this).attr('id'); if (n != '') { sidebarHide('#' + n); } }); });
Common problem when building web pages. You want a DIV that’s the same height as the browser window, but you don’t know what the height of the browser window is. It changes from user to user. Well, jQuery to the rescue.
var browser_window_height = 0; $(document).ready(function() { // this sets the height of the div on init $('#fullscreen').each( function() { browser_window_height = $(window).height(); $('#info').text(browser_window_height.toString()); $(this).css('height', browser_window_height.toString() + 'px'); } ); // this says, if they resize the window, change the height of the div $(window).resize(function() { var new_browser_window_height = $(window).height(); if (new_browser_window_height < browser_window_height) { // if the new window size is smaller, resize quickquickquick $('#fullscreen').css('height', new_browser_window_height.toString() + 'px'); } else { $('#fullscreen').animate({ 'height' : new_browser_window_height.toString() + 'px' }, "slow"); } browser_window_height = new_browser_window_height; $('#info').text(browser_window_height.toString()); }); });
View the page (and full source) here.
This kind of table has been around for years. It’s nice to finally know how to make them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | <html>
<head>
<title>jQuery day 2 of 15</title>
<script type="text/javascript" src="jquery.min.js"></script>
<script>
$(document).ready(function() {
// this stripes our table very nicely
$("table tbody tr:odd").addClass("alt");
// now add the hover bit
$("table tbody tr").hover(
function() {
// over
$(this).addClass("highlight");
},
function() {
// out
$(this).removeClass("highlight");
}
);
});
</script>
<style>
table {
margin: 50px auto 0 auto;
border: 1px solid #666666;
}
thead tr {
background-color: #666666;
}
th {
font-weight: bold;
color: #ffffff;
padding: 5px;
}
td {
width: 200px;
padding: 5px;
}
.alt {
background-color: #cccccc;
}
.highlight {
background-color: #ff9900;
}
.highlight td {
border-top: 1px solid #666666;
border-bottom: 1px solid #666666;
}
</style>
</head>
<body>
<table cellspacing="0" cellpadding="0">
<thead>
<tr>
<th>One</th>
<th>Two</th>
<th>Three</th>
</tr>
</thead>
<tbody>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
<tr>
<td>One</td>
<td>Two</td>
<td>Three</td>
</tr>
</tbody>
</table>
</body>
</html> |
We have a column of gallery names on the left, and a grid of thumbnails on the right. Each gallery name corresponds to a thumbnail, and they are both links to a gallery page. Like this:
![]()
We want the thumbnail to “light up” when you hover over the gallery name, and we want the gallery name to light up when you hover over the thumbnail. The solution was original written with a javascript function and a set of onMouseOver() onMouseOut() events. You had two huge javascript function calls for each text link and two huge javascript function calls for each thumbnail link. Ick.
So I rewrote it with jQuery, which I know nothing about. I was able to remove all the onMouseOver() and onMouseOut() crap from all of the links, and I added this jQuery code:
function textToNum(num) { if (num == "one") { return "1"; } if (num == "two") { return "2"; } if (num == "three") { return "3"; } if (num == "four") { return "4"; } if (num == "five") { return "5"; } if (num == "six") { return "6"; } if (num == "seven") { return "7"; } if (num == "eight") { return "8"; } if (num == "nine") { return "9"; } return "zero"; } function numToText(num) { if (num == "1") { return "one"; } if (num == "2") { return "two"; } if (num == "3") { return "three"; } if (num == "4") { return "four"; } if (num == "5") { return "five"; } if (num == "6") { return "six"; } if (num == "7") { return "seven"; } if (num == "8") { return "eight"; } if (num == "9") { return "nine"; } return "0"; } $(document).ready(function(){ // this adds the code to highlight the thumbnails when you hover over the text $("#secondaryNavFive ul li a span").hover( function() { // over var span_id = $(this).attr('id'); var image_id = textToNum(span_id); // one highlights image with id 1 $("#" + image_id).css('border-color', '#f89828'); }, function() { // out (not hovering any more) var span_id = $(this).attr('id'); var image_id = textToNum(span_id); // one highlights image with id 1 $("#" + image_id).css('border-color', '#333333'); } ); // this adds the code to highlight the TEXT when you hover over the THUMBNAIL $("#mainContentFour .thumbs a img").hover( function() { // over var image_id = $(this).attr('id'); var span_id = numToText(image_id); $("#" + span_id).css('color', '#f89828'); }, function() { // out (not hovering any more) var image_id = $(this).attr('id'); var span_id = numToText(image_id); $("#" + span_id).css('color', '#aaaaaa'); } ); });