Simple hover button?

Discussion in 'HTML & Website Design' started by Solong, Jul 1, 2013.

  1. #1
    I've added a button to my page that looks like this:

    <input onClick="randomImg()" type="image" src="pilhoger.png"
        name="input_img"  width="63" height="64">
    Code (markup):
    Now I simply want to add a "hover image" that appears when I hover my mouse over the button. I've searched for hover buttons but just find complicated codes. Isn't there a way just to add like onMousehover="hover.jpg" into the code I'm already using?
     
    Solong, Jul 1, 2013 IP
  2. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #2
    Use CSS and add a hover state to it:


    
     
    <input onClick="randomImg()" type="image" src="pilhoger.png"
        name="input_img"  width="63" height="64">
    
    HTML:
    
     
    #yourDiv img:hover {
     
    }
    Code (markup):
     
    scottlpool2003, Jul 1, 2013 IP
  3. aidanriley629

    aidanriley629 Banned

    Messages:
    429
    Likes Received:
    23
    Best Answers:
    3
    Trophy Points:
    175
    #3
    The best way.. well, my favorite way to do it is to preload the images using JavaScript. In case someone has slow Internet, the image won't appear for them until after a few seconds unless you preload them first. The code isn't too difficult, I'll write it now for you.

    This is the easiest way, but again the images aren't preloaded.
     
    Last edited by a moderator: Jul 1, 2013
    aidanriley629, Jul 1, 2013 IP
  4. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #4
    You could use inline CSS but that's not good practice.
     
    scottlpool2003, Jul 1, 2013 IP
  5. aidanriley629

    aidanriley629 Banned

    Messages:
    429
    Likes Received:
    23
    Best Answers:
    3
    Trophy Points:
    175
    #5
    Ok so I'm stuck on someone else's computer without any programs. I just wrote this in Notepad. Pure unadulterated hell, lol. I've left you comments in the code; it's kind long but efficient.

    <!--html/javascript image preloader & onmouseover display image script, 7/01/2013-->
    <h1>Pic onhover with preloader</h1>
    <script language="javascript">
    <!--
     
    if(document.images){
     
    // This is the preloader.
    // Uncomment images 2-5 (and add more if you need) if you want to use them. To uncomment, delete the /* and */
    image1 = new Image();
    /*
    image2 = new Image();
    image3 = new Image();
    image4 = new Image();
    image5 = new Image(); */
     
       
    image1.src="http://us.123rf.com/400wm/400/400/ronstik/ronstik1208/ronstik120800014/14793762-portrait-of-young-man-thinking.jpg";
    /* image2.src="";
    image3.src="";
    image4.src="";
    image5.src=""; */
        }
    function ShowPic(id,Source) {
    if (Source=="1"){
    if (document.layers) document.layers[''+id+''].visibility = "show"
    else if (document.all) document.all[''+id+''].style.visibility = "visible"
    else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible"
    }
    else
    if (Source=="0"){
    if (document.layers) document.layers[''+id+''].visibility = "hide"
    else if (document.all) document.all[''+id+''].style.visibility = "hidden"
    else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
    }
    }
    </script>
    <style type="text/css">
    #Style
     
    {
        position:absolute;
        visibility:hidden;
        border:solid 1px #CCC;
        padding:5px;
       
    }
    </style>
     
    <a href="#" onMouseOver="ShowPic('Style',1)" onMouseOut="ShowPic('Style',0)">Hover over me</a>
    <div id="Style"><img src="http://us.123rf.com/400wm/400/400/ronstik/ronstik1208/ronstik120800014/14793762-portrait-of-young-man-thinking.jpg"></div>
    
    Code (markup):
     
    aidanriley629, Jul 1, 2013 IP
  6. Solong

    Solong Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6

    Ok, I'm a real newbie in webdesign, and I appreciate the effort. But I don't understand much of it and I want to keep it simple so that I learn more than just copy/pasting. I'll probably get back and use this code when I have more knowledge though... As you say, it's more efficient.

    For now I wanna use the first code, and I have some questions about it. I don't understand divisions that much. This is the code I wrote but it didn't work. The #hoverpic hoverbutton.jpg:hover code is under a CSS-script, so I'm pretty sure it's in the right place, the code is probably wrong though...

    #hoverpic hoverbutton.jpg:hover {
     
    }
     
    <div id="hoverpic">
    <input onClick="randomImg()" type="image" src="pilhoger.png"
        name="input_img"  width="63" height="64">
    </div>
    Code (markup):
    Can you explain it in a more "newbie friendly" way?:oops:
     
    Solong, Jul 1, 2013 IP
  7. malky66

    malky66 Acclaimed Member

    Messages:
    3,997
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #7
    I'm pretty sure you didn't write the "ShowPic()" function, you just copied it from here:
    http://www.webdeveloper.com/forum/showthread.php?249768-JavaScript-Show-image-on
     
    malky66, Jul 1, 2013 IP
    ryan_uk likes this.
  8. aidanriley629

    aidanriley629 Banned

    Messages:
    429
    Likes Received:
    23
    Best Answers:
    3
    Trophy Points:
    175
    #8
    aidanriley629, Jul 1, 2013 IP
  9. malky66

    malky66 Acclaimed Member

    Messages:
    3,997
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #9
    you changed the name of the function from ShowPicture() to ShowPic() and changed the image sources,

    Just happened to have the snippet saved on *someone elses* computer i suppose
    Yeah must have been hell for you passing off someone elses work as your own....
     
    malky66, Jul 1, 2013 IP
    ryan_uk likes this.
  10. aidanriley629

    aidanriley629 Banned

    Messages:
    429
    Likes Received:
    23
    Best Answers:
    3
    Trophy Points:
    175
    #10
    I had the snippet saved in Evernote, and never claimed those two functions were my code. I didn't change the name of anything, and I don't have to explain myself to you.
     
    aidanriley629, Jul 1, 2013 IP
  11. aidanriley629

    aidanriley629 Banned

    Messages:
    429
    Likes Received:
    23
    Best Answers:
    3
    Trophy Points:
    175
    #11

    It needs to be more like this, I'm just not sure what to put in the { } because I don't know what your function randomImg() or what hoverbutton.jpg is.

    <style type="text/css">
    #hover
    img:hover {
     
    }
    </style>
    Code (markup):
     
    aidanriley629, Jul 1, 2013 IP
  12. Solong

    Solong Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #12

    The hoverbutton.jpg is the picture I want to show when I hover the mouse over the button. randomImg() is the function I want to call when I press the button. What am I supposed to do with the code you wrote?
     
    Solong, Jul 1, 2013 IP
  13. aidanriley629

    aidanriley629 Banned

    Messages:
    429
    Likes Received:
    23
    Best Answers:
    3
    Trophy Points:
    175
    #13
    You need to put it in your stylesheet. If you don't have one, you can either make one or use inline styling, but I don't recommend doing so. Can you paste or PM me the code for randomImg() so I can see what it's doing? Or paste your site?
     
    aidanriley629, Jul 1, 2013 IP