简体   繁体   中英

How can I fade in text with toggle when clicking on an image?

I would like to fade in and out text when clicking on an image. I'm pretty new to JS, so thank you for answering my question!

Here's my jsfiddle: http://jsfiddle.net/gSVfV/

Here's what I have for JS:

$(function() {
    $('.block img.info').click(function(e) {
        e.preventDefault();
        $('.caption-background').toggleClass('hidden');
    });
});

For HTML:

<div class="block">
<img class="info"/>
<div class="caption-background">
    Hello
</div>

And for css:

 .info {
    width: 200px;
    height: 100px;
    background-image: 
url("http://ocean.nationalgeographic.com/u/TvyamNb-
BivtNwpvn7Sct0VFDulyAfA9wBcU0gVHVnqC5ghqXjggeitqtJ-
1ZIZ1rmCgor42TXOteIQU/");
}
.caption-background {
    height: 200px;
}
.hidden {
    display: none;
}

You can use the jQuery fadeToggle method.

Update your javascript with the following:

$(function() {
    $('.rsABlock img.info').click(function(e) {
        e.preventDefault();
        $('.caption-background').fadeToggle();
    });
});

Check out http://api.jquery.com/fadetoggle/

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