Results 1 to 7 of 7

Thread: Change image on rollover

  1. #1
    Join Date
    Apr 2010

    Change image on rollover

    I essentially have this code below and what I am trying to do is change the image when the mouse rolls over it but change it back when the mouse leaves. What am I doing wrong?

    HTML Code:
    <script type="text/javascript">
    function ShowFlip(number)
    document.getElementById( number ).innerHTML = '<img src="../events/1flip.jpg">';
    function HideFlip(number)
    document.getElementById( number ).innerHTML = '<img src="../events/1.jpg">';
    <a id="1" style="cursor:default;" 
    <img src="../events/1.jpg">
    I have also tried assigning the id to the image and came up with this code but it still doesn't change anything. Just shows the original image. Any help is appreciated! Thanks!

    HTML Code:
    <script type="text/javascript">
    function ShowFlip(number)
    document.getElementById('a').src = "../events/1flip.jpg";
    function HideFlip(number)
    document.getElementById('a').src = "../events/1.jpg";
    	<img id="a" src="../events/1.jpg" onMouseOver="ShowFlip('a');"

  2. #2
    Join Date
    Apr 2003
    A different approach using css:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <style type="text/css">
    * {margin:0;padding:0;}
    #a1 {
    background:blue url(image1.jpg) no-repeat;
    #a1:hover {
    background:blue url(image2.jpg) no-repeat;
    	<a href="index.htm" id="a1" title="foobar"></a>
    A number id is invalid: http://www.w3.org/TR/html401/types.html#type-name
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Mar 2010
    You really need only 1 function to handle the image swapping.

    Personally, I prefer to preload the images, especially if they are large, because using css :hover might not always give an instant image swap if the images are large and/or uncached.

    Maybe use this as a template:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    html xmlns="http://www.w3.org/1999/xhtml">
    script type="text/javascript">

    imgPaths = new Array();
    imgPaths = ['pic1.jpg',
    myImgs = new Array();
    //preload the images
    for(var i in imgPaths) {
    myImgs[i] = new Image();
    myImgs[i].src imgPaths[i];
    picNum 0;
    swapImg(elemId) {
    document.getElementById(elemId).src myImgs[picNum].src;
    picNum == 1) {
    picNum 0;   
        } else {
    picNum 1;


    img id="myPic" src="pic1.jpg" alt="myPic" onmouseover="swapImg(this.id);" onmouseout="swapImg(this.id);">


  4. #4
    Join Date
    Dec 2003
    Bucharest, ROMANIA
    Well,hm... I have never trusted the preloader codes. After all, the new Image() constructor creates a javascript object, and it does not load the image into the browser's cache by all means. In theory it should, but the browsers' interpreters do not always follow the theory

    I remember I have made once a simple test in IE6 and IE7. I have used new Image() to create the object but without displaying it, actually. I have checked the browser's cache (IE Temporary File) but no image was there . maybe IE8 has bypassed this problem... I think I should check that again

  5. #5
    Join Date
    Mar 2010
    Preloaded images are stored locally, but whether it is in the browser's normal cache or elsewhere I don't know. I really don't care where they are stored locally.

    In my experience, preloaded images definitely load virtually instaneously when called in some sort of animation (image swap, slideshow etc). With non preloaded images I sometimes notice a delay in the image appearing when called because the browser downloads it from the server rather than get it locally.

    I currently support IE8, IE7, FF3.6, OP9, OP10, Safari (for Windows) and Chrome. In all these browsers preloaded images, especially large ones, load very much quicker than non preloaded images in my experience.

    I will always preload images that will be used in some kind of animation. Images that are not used in animations, I may or may not preload depending on the circumstances.

    For anyone interested, an interesting read on javascripts Image object.

    Last edited by tirna; 04-21-2010 at 05:07 AM.

  6. #6
    Join Date
    Dec 2003
    Bucharest, ROMANIA
    Quote Originally Posted by tirna View Post
    Preloaded images are stored locally, but whether it is in the browser's normal cache or elsewhere I don't know. I really don't care where they are stored locally.
    You should care. If the images are not stored locally and the code uses absolute URL, some browsers will load the images again and again from the server. Which is exactly what we try to avoid

  7. #7
    Join Date
    Mar 2010
    Quote Originally Posted by Kor View Post
    You should care........
    I think you are miising the point I was making.

    Preloading images using the Image object will result in the images being stored locally somewhere. Whether that is in the browser's default cache folder, some other local folder or even in RAM I do not care at all and I don't see why I need to care. For me, there are a lot more important things going on in our world at the moment to care about than where some browser is storing images

    The intention behind my preloading images is to ensure they are located locally when they are called upon by a javascript.
    Last edited by tirna; 04-21-2010 at 05:01 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.12958 seconds
  • Memory Usage 2,935KB
  • Queries Executed 15 (?)
More Information
Template Usage (36):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (1)bbcode_code
  • (2)bbcode_html
  • (1)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (7)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (7)postbit
  • (7)postbit_onlinestatus
  • (7)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates