简体   繁体   中英

Text change color when hovered over background image

I am trying to have the "Chasing Color" text on top of each image to change to black whenever that image is being hovered over by using javascript.

<div class="container">
   <img src="http://images2.webydo.com/31/313624/3958/21b785db-14ea-42f7-af0d-7e7a8d8019d9.jpg" />

   <img src="http://images2.webydo.com/31/313624/3958/9657ddfd-81e8-4154-bc61-bbe30e4a8740.jpg" />
   <img src="http://images2.webydo.com/31/313624/3958/909af36d-b941-4a20-9441-20505c035da3.jpg"/>

   <img src="http://images2.webydo.com/31/313624/3958/b338fd3a-c3c4-4dbd-af54-fc49f951ea3f.jpg_400" />
   <img src="http://images2.webydo.com/31/313624/3958/6d1e573d-3eb9-42b1-ad34-c099bde75607.jpg_400" />
   <img src="http://images2.webydo.com/31/313624/3958/c2bafb57-4a2a-4f48-a75d-abe4be51f252.jpg_400"/>

   <img src="http://images2.webydo.com/31/313624/3958/421c9f82-67ef-4545-ab6b-976a11f5d406.jpg_400" />
   <img src="http://images2.webydo.com/31/313624/3958/43e1d8ae-4e94-4355-a5ce-e329123daf41.jpg_400" />
   <img src="http://images2.webydo.com/31/313624/3958/c8d99a50-a3d1-41c6-9af7-02ed02d3e820.jpg_400"/>
  <div class='titles'>
   <div id="chase1">Chasing Color</div>
   <div id="chase2">Chasing Color</div>
   <div id="chase3">Chasing Color</div>
</div>
</div>

$(document).ready(function () {
    $('.container img').hover(function () {
        $(this).fadeTo('fast', 0.5);
    }, function () {
        $(this).fadeTo('fast', 1);
    });

});

Assuming you have 9 images and 9 titles, you can change the colour of the text at the same 'position' (index) as the image:

$('.container img').hover(
    function(){
        $('#chase'+($(this).index+1)).css('color','black');
    }, 
    function(){
        $('#chase'+($(this).index+1)).css('color','white');
    }
);

is that what you're after?

I use ".chase" as sample class for your image. try this Jquery

$(document).ready(function () {
    $('.container img').hover(function () {
        $(this).fadeTo('fast', 0.5);
    }, function () {
        $(this).fadeTo('fast', 1);
    });
$(".chase").mouseover(function(){
    $("#chase1").css("color","black");
  });$(".chase").mouseout(function(){
    $("#chase1").css("color","white");
  });
});

with this CSS

.chase /*sample image class */
{    
    position:relative;
    left:0;
    top:0;
}
#chase1
{
    z-index:100;
    position:absolute;    
    font-size:24px;
    font-weight:bold;
    left:750px;
    top:50px;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM