Ridz
Ridz

Reputation: 407

Javascript: how to affect other element when hovering?

Ok, so I want to have the following: when I hover an element, another element should be animated. But so far, this has been to no avail. I'm fairly new to javascript/jquery so please don't be to harsh :p

The javascript/jquery:

$("block").hover(function(){ 
    $(this).find("site").stop(true, false).fadeTo("slow", 0.2);

    $(this).find("textOnImage").stop(true, false).animate({ 
        opacity: "1"
    }, "slow");

    var id = jQuery(this).attr("id");
    if (id == "site1") {
        $("siteLarge").animate({
            backgroundImage: "images/site1large.png"
        }, "slow"); 
    } else if (id == "site2") {
        $("siteLarge").animate({
            backgroundImage: "images/site2large.png"
        }, "slow");
    } else if (id == "site3") { 
        $("siteLarge").stop(true, false).animate({
            backgroundImage: "images/site1large.png"
        }, "slow");
    }
}, function() {         
    $(this).find("site").stop(true, false).fadeTo("slow", 1);
    $(this).find("textOnImage").stop(true, false).animate({ 
        opacity: "0"
    }, "slow");
});

I've tested it with alerts and the source identifying works. However, the 'siteLarge' is not affected by this.

The html:

<div id="centerBlock">
        <block id="site1">
            <site style="position: absolute; margin-left: 10px; margin-top: 10px; border: 1px solid black;">
                <img src="./images/site1.png" alt="some_text"/>
            </site>
            <textOnImage style="position: absolute; margin-left: 10px; margin-top: 10px;">
                Mijn eerste site. Best wel leeg, maar een leuk begin. Een combinatie van html en css.
            </textOnImage>
        </block>

        <block id="site2">
            <site style="position: absolute; margin-left: 10px; margin-top: 163px; border: 1px solid white;">
                <img src="./images/site2.png" alt="some_text"/>
            </site>
            <textOnImage style="position: absolute; margin-left: 10px; margin-top: 163px;">
                Mijn tweede site. Een hele grote vooruitgang ten opzichte van mijn eerste site! Hier wordt interactieve css toegepast.
            </textOnImage>
        </block>

        <block id="site3">
            <site style="position: absolute; margin-left: 10px; margin-top: 316px; border: 1px solid black;">
                <img src="./images/site3.png" alt="some_text"/>
            </site>
            <textOnImage style="position: absolute; margin-left: 10px; margin-top: 316px;">
                Toekomstige plannen: <br> php, msql, ajax.
            </textOnImage>
        </block>

        <large>
            <siteLarge style="position: absolute; margin-left: 250px; margin-top: 10px;">
            </siteLarge>
        </large>

    </div>

Upvotes: 0

Views: 383

Answers (1)

JJJ
JJJ

Reputation: 33153

If you're looking for a fade transition from one background image to another, you can't do it with .animate(). See transition background-image/css change on hover? for a fade transition solution.

Also, you might need a proper CSS declaration of the new url: backgroundImage: "url(images/site1large.png)". But .animate() will probably just switch to the new image instantly if it works at all.

Upvotes: 1

Related Questions