@@ -335,13 +335,16 @@ def image_path(source)
335
335
#
336
336
# ==== Options
337
337
# You can add HTML attributes using the +options+. The +options+ supports
338
- # two additional keys for convienence and conformance:
338
+ # three additional keys for convienence and conformance:
339
339
#
340
340
# * <tt>:alt</tt> - If no alt text is given, the file name part of the
341
341
# +source+ is used (capitalized and without the extension)
342
342
# * <tt>:size</tt> - Supplied as "{Width}x{Height}", so "30x45" becomes
343
343
# width="30" and height="45". <tt>:size</tt> will be ignored if the
344
344
# value is not in the correct format.
345
+ # * <tt>:mouseover</tt> - Set an alternate image to be used when the onmouseover
346
+ # event is fired, and sets the original image to be replaced onmouseout.
347
+ # This can be used to implement an easy image toggle that fires on onmouseover.
345
348
#
346
349
# ==== Examples
347
350
# image_tag("icon") # =>
@@ -356,15 +359,23 @@ def image_path(source)
356
359
# <img alt="Icon" height="32" src="/icons/icon.gif" width="32" />
357
360
# image_tag("/icons/icon.gif", :class => "menu_icon") # =>
358
361
# <img alt="Icon" class="menu_icon" src="/icons/icon.gif" />
362
+ # image_tag("mouse.png", :mouseover => "/images/mouse_over.png") # =>
363
+ # <img src="/images/mouse.png" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" alt="Mouse" />
364
+ # image_tag("mouse.png", :mouseover => image_path("mouse_over.png")) # =>
365
+ # <img src="/images/mouse.png" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" alt="Mouse" />
359
366
def image_tag ( source , options = { } )
360
367
options . symbolize_keys!
361
368
362
369
options [ :src ] = path_to_image ( source )
363
370
options [ :alt ] ||= File . basename ( options [ :src ] , '.*' ) . split ( '.' ) . first . capitalize
364
371
365
- if options [ :size ]
366
- options [ :width ] , options [ :height ] = options [ :size ] . split ( "x" ) if options [ :size ] =~ %r{^\d +x\d +$}
367
- options . delete ( :size )
372
+ if size = options . delete ( :size )
373
+ options [ :width ] , options [ :height ] = size . split ( "x" ) if size =~ %r{^\d +x\d +$}
374
+ end
375
+
376
+ if mouseover = options . delete ( :mouseover )
377
+ options [ :onmouseover ] = "this.src='#{ image_path ( mouseover ) } '"
378
+ options [ :onmouseout ] = "this.src='#{ image_path ( options [ :src ] ) } '"
368
379
end
369
380
370
381
tag ( "img" , options )
0 commit comments