{"id":113,"date":"2010-08-24T18:45:19","date_gmt":"2010-08-24T22:45:19","guid":{"rendered":"http:\/\/blogs.law.harvard.edu\/djcp\/?p=113"},"modified":"2010-08-24T18:45:19","modified_gmt":"2010-08-24T22:45:19","slug":"pre-loaded-image-rollovers-with-jquery","status":"publish","type":"post","link":"https:\/\/archive.blogs.harvard.edu\/djcp\/2010\/08\/pre-loaded-image-rollovers-with-jquery\/","title":{"rendered":"Pre-loaded Image Rollovers with jQuery"},"content":{"rendered":"<p>I despise implementing image rollovers, but this makes it almost tolerable.  Any &lt;img&gt; tag with a &#8220;hover&#8221; attribute referring to an image URL will have a rollover behavior attached to it. For bonus points, the rollover image will be pre-loaded, so there&#8217;s no momentary delay the first time an on-state image is loaded.<\/p>\n<p>In your HTML source:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;img src=&quot;\/images\/button.gif&quot; hover=&quot;\/images\/button_on.gif&quot; \/&gt;\r\n&lt;img src=&quot;\/images\/another_button.gif&quot; hover=&quot;\/images\/another_button_on.gif&quot; \/&gt;\r\n<\/pre>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\njQuery(document).ready(function(){\r\n  jQuery(&#039;img&#x5B;hover]&#039;).each(function(){\r\n    \/\/ Preload rollover\r\n    var imageEl = jQuery(&quot;&lt;img alt=&quot;&quot; \/&gt;&quot;);\r\n    imageEl.attr(&#039;src&#039;,jQuery(this).attr(&#039;hover&#039;));\r\n\r\n    jQuery(this).hover(\r\n      function(){\r\n        \/\/ swap the image.\r\n        var hoverSrc = jQuery(this).attr(&#039;hover&#039;);\r\n        var regSrc = jQuery(this).attr(&#039;src&#039;);\r\n        jQuery(this).attr(&#039;src&#039;,hoverSrc);\r\n        jQuery(this).attr(&#039;hover&#039;,regSrc);\r\n      }\r\n    );\r\n  });\r\n});\r\n<\/pre>\n<p>Thanks jQuery for being so awesome.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I despise implementing image rollovers, but this makes it almost tolerable. Any &lt;img&gt; tag with a &#8220;hover&#8221; attribute referring to an image URL will have a rollover behavior attached to it. For bonus points, the rollover image will be pre-loaded, &hellip; <a href=\"https:\/\/archive.blogs.harvard.edu\/djcp\/2010\/08\/pre-loaded-image-rollovers-with-jquery\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1984,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4,17295,17294],"class_list":["post-113","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-code","tag-javascript","tag-jquery"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/posts\/113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/users\/1984"}],"replies":[{"embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/comments?post=113"}],"version-history":[{"count":6,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/posts\/113\/revisions"}],"predecessor-version":[{"id":120,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/posts\/113\/revisions\/120"}],"wp:attachment":[{"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/media?parent=113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/categories?post=113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/archive.blogs.harvard.edu\/djcp\/wp-json\/wp\/v2\/tags?post=113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}