简体   繁体   中英

Display diffrent images when a div is hidden or visible

I have a little problem with jquery and I can't solve it on my own. I have an image and when I click on that image a div toggle. I want that when the div is hidden the image to be a down arrow, and when the div is visible the image to be an up arrow. My HTML code:

<div class="small-3 columns log_message">
   <span id="mesaj_salutare">Welcome!</span><br>
   <span id="account">MY ACCOUNT</span>
   <img id="image_log" src="images/down.png"/>
</div>
<div class="small-4 columns" id="log_form">
     <?php
    //display diffrent code if user is logged or not
    if (isset($_SESSION['id_utilizator']) and isset($_SESSION['email_login'])) {
        require_once 'page_sections/logout.php';
    } else {
        require_once 'page_sections/login_form.php';
    }

     ?>
</div>

My CSS for log_form:

#log_form{
    margin-top:18px;
    display: none;
    min-height: 200px;
    border: 1px solid #999999;
    position: absolute;
    top: 60px;
    right: 0;
    background: #788dbc;
    z-index: 10;
    -webkit-border-bottom-right-radius: 10px;
    -webkit-border-bottom-left-radius: 10px;
    -moz-border-radius-bottomright: 10px;
    -moz-border-radius-bottomleft: 10px;
    border-bottom-right-radius: 10px;
    border-bottom-left-radius: 10px;
    font-size: small;
    min-height: 150px;
}

My JQuery code:

$(document).ready(function (){
    $("#image_log").click(function(){
        $("#log_form").toggle(500);
    });
});

So let's recap: when the page starts #log_form is hidden and #image_log src is= "images/down.png" , after click on #image_log , #log_form appears and I need that #img_log src to be= "images/up.png"

$(document).ready(function (){
    $("#image_log").click(function(){
        $("#log_form").toggle(500,function(){
            if($(this).is(":visible")){
             $("#img_log").attr('src',"images/up.png");
            }
            else{
             $("#img_log").attr('src',"images/down.png");
            }
        });
    });
});

You just set the img src to the new file:

$("#image_log[src='down.png']").click(function () {
   $(this).attr("src","up.png")
})

Try this way

$(document).ready(function (){
    $("#image_log").click(function(){
        $("#log_form").toggle(500);
        $(this).attr('src', 
                     $("#log_form").is(":visible") 
                         ? 'images/up.png' 
                         : 'images/down.png');
    });
});

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