user3871
user3871

Reputation: 12718

Image highlighting

Bergi (stack overflow user) so graciously helped me come up with a solution for image swap using jQuery. It swaps on hover now, but id like to make it so that it stays highlighted on your current sub site (for example, I click on "Comics" page, and "Comics" stays highlighted). Is that possible? Thanks!

    <script type="text/javascript">
        var images = {
            "comicssubsite": [
                "./images/SiteDesign/Comics_subsites_hover.png",
                "./images/SiteDesign/Comics_subsites.png"
            ],
            "artworksubsite": [
                "./images/SiteDesign/Artwork_subsites_hover.png",
                "./images/SiteDesign/Artwork_subsites.png"
            ],
            "aboutsubsite": [
                "./images/SiteDesign/About_subsites_hover.png",
                "./images/SiteDesign/About_subsites.png"
            ],
            "blog": [
                "./images/SiteDesign/Blog_othersites_hover.png",
                "./images/SiteDesign/Blog_othersites.png"
            ],
            "facebook": [
                "./images/SiteDesign/Facebook_othersites_hover.png",
                "./images/SiteDesign/Facebook_othersites.png"
            ]
        };

        jQuery(document).ready(function($) {
            $("#comicssubsite, #artworksubsite, #aboutsubsite, #blog, #facebook").hover(function(e) {
                 // mouseover handler
                 if (this.id in images) // check for key in map
                     this.src = images[this.id][0];
            }, function(e) {
                 // mouseout handler
                 if (this.id in images)
                     this.src = images[this.id][1];
            });
        });

Upvotes: 0

Views: 140

Answers (2)

Silvester
Silvester

Reputation: 516

Zotal Toth's answer should work, but you should consider changing your mouseout-handler like so, unless you want the image to change back, once a user hovers over it:

// mouseout handler
var section = document.location.href.split("/")[url.split("/").length-1].split(".")[0];
if (this.id in images) {
   if this.id != section
       this.src = images[this.id][1];
}

Upvotes: 2

Zoltan Toth
Zoltan Toth

Reputation: 47667

The following assumes your pages named comicssubsite.*, artworksubsite.* etc.:

var url = window.location.href;

for (key in images) {
    if ( url.indexOf( key ) > -1 ) {
        alert(key);

        // highlight your menu item
        $( "#" + key ).attr( "src", images[key][0] );
    }
}

Upvotes: 1

Related Questions