views:

493

answers:

1

Hi! I'm very much a beginner when it comes to Javascript and would appreciate any help you can give! I'm creating a feature box on my home page where three headlines will share one picture spot. I've found a script that changes the image when the headlines are rolled over, but it's hard to tell when the page opens that the first headline goes with the first picture. How do I get my hover style to appear already selected, and then stay with the last headline that was rolled over, so it's apparent what headline goes with the photo showing? Here's my example

Here's the code I'm using:

HOVER STYLE:

a.feature:hover {
    font-size: 0.9em;
    font-family: "trebuchet ms", Arial, Helvetica, sans-serif;
    color: #b0171f;
    font-weight: bold;
    background-image: url(../zimgart/nav/bgfeature.jpg);
    background-repeat: no-repeat;
    text-decoration: none;
    padding: 5px 0 5px 10px;
    display:block;
}

JAVASCRIPT:

<script>

/*Rollover effect on different image script-
By JavaScript Kit (http://javascriptkit.com)
Over 200+ free scripts here!
*/

function changeimage(towhat,url){
    if (document.images){
        document.images.targetimage.src=towhat.src
        gotolink=url
    }
}
function warp(){
    window.location=gotolink
}
</script>

<script language="JavaScript1.1">
var myimages=new Array()
var gotolink="#"

function preloadimages(){
    for (i=0;i<preloadimages.arguments.length;i++){
        myimages[i]=new Image()
        myimages[i].src=preloadimages.arguments[i]
    }
}


preloadimages("photos/feature1.jpg",
              "photos/feature2.jpg",
              "photos/feature3.jpg")
</script>
+2  A: 

Generally you should do such thing with JS code, simplest of course would be to use jQuery. With jQuery it would look like this:

$(document).ready(function(){
  $('A.feature').mouseover(functiond(e){
    $('A.feature').removeClass('a_hover');
    $(this).addClass('a_hover');
    $('#bigimage').attr('src',$(this).attr('rel')); // big image effect, just example
  })
});

I assume that A-links have attribute rel='bigimageulr'. To install jQuery just put in header:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"&gt;&lt;/script&gt;
Thinker
I'm afraid I don't understand where to place that first code or what to substitute "#bigimage" with to make it work. Is there a website for learning jQuery that you'd recommend?
Cape Gazette
I updated the code, you can put it anywhere in html.#bigimage is the image on the right to your hover-links, and #name means, that you should set ID=bigimage as attribute to it.
Thinker
*image on the left (not right)
Thinker
Thank you for your help, but I am lost, because nothing happens at http://delmarvaquarterly.com/index2.html!In head:<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js">$(document).ready(function(){ $('A.feature').mouseover(functiond(e){ $('A.feature').removeClass('a_hover'); $(this).addClass('a_hover'); $('#featureimage').attr('src',$(this).attr('rel')); })});</script>Image: <img id="featureimage" src="photos/feature1.jpg" border="0">
Cape Gazette
Link:<a class="feature" href="stories/test1.html" rel="http://delmarvaquarterly.com/photos/feature1.jpg">Delmarva Dwellings<br />
Cape Gazette
Okay, those comments didn't post as planned. Is there a better way to show you the code without making you go look at the source?
Cape Gazette
you copied my code with typo - functiond - should be function.
Thinker
Oops! Thanks again for trying to help someone as clueless to JavaScript as I. I fixed the typo, and still nothing ...
Cape Gazette
You cant use '[script src=...] CODE [/script]' - CODE is omitted. You must write [script src=...][/script] and [script] CODE [/script] - when you set src atribute of script, code inside tags wont be passed.
Thinker
Thanks! Pictures are now changing, but how do I get the hover style to stay active on the last link that was rolled over, so it's apparent what story goes with the picture showing?
Cape Gazette
It's in the code :) Set CSS for .a_hover same as A:hover.
Thinker
It works! Thank you, thank you, thank you for being so patient with me!! *dances*
Cape Gazette