lundi 11 janvier 2016

How do I turn this jQuery function into an 'if' statement

Right now I'm trying to write a jquery function which lets me expand a div when you click it, then return it to its initial size when you click it again.

I tried adding a button to make it retract, but because when I click it the script thinks I'm also clicking the parent div and expands again. Ideally, I'd like to do it without having a button.

The code I've got is here -

(jsfiddle): http://ift.tt/1PnHdFg

<div class="grid">
        <div class="block" id="block1">1

            <button class="retractor" id="ret1">back</button>
        </div>
        <div class="block" id="block2">2</div>
        <div class="block" id="block3">3</div>
        <div class="block" id="block4">4</div>
  <div class="block" id="block5">5</div>
        <div class="block" id="block6">6</div>
</div>

CSS

*{
    margin: 0;
    padding: 0;
}

html, body{
    width: 100%;
    height: 100%;
}

.grid{
    width: 100%;
    height: 100%;
    /*background-color: #114411;*/
}

.block{
    width: 33.3%;
  height: 50%;
    float: left;
    color: #FFFFFF;
    position: relative;
}

.retractor{
    width: 50px;
    height: 50px;
    padding: 10px 0;
    text-align: center;
    background-color: #FF0000;
    color: #FFFFFF;
    font-family: 'Open Sans', sans-serif;

    position: absolute;
    bottom: 30px;
    right: 30px;
}

#block1{
    background-color: #222222;
}           

#block2{
    background-color: #999999;
}           

#block3{
    background-color: #5555555;
}           

#block4{
    background-color: #999999;
}   

#block5{
    background-color: #333333;
}       

#block6{
    background-color: #CCCCCC;

Script

$('#block1').click(function(){
        $('#block1').animate({
    width: '100%',
    height: '100%'});
});

$('#block1').click(function(){
    $('#block1').animate({
        width: '33.3%',
    height: '50%'});
});

Aucun commentaire:

Enregistrer un commentaire