I have a piece of code where I have two buttons, and when I click one button, I want the div to flip the y axis, and when I click another button, I want the same div to switch along the x axis, this works fine when I use the button for the y axis, even if the previous flip was on the x axis. But when I want to switch from the y axis to the x axis, I need to press the x button twice, because for the first time nothing happens.
Desired behavior:
Press the y button, flip the div along the y axis.
Press the x button, flip the div along the x axis.
Current behavior:
Press the y button to collapse on the y axis.
press the x button, nothing happens press the x button again to expand on the x axis.
I have no idea what the problem is, this is the best I have received so far. All help is appreciated.
var ax = 'x'; $(document).ready(function() { $('#card').flip({ trigger: 'manual', axis: 'x' }); $('#left').click(function() { if (ax != 'y') { $("#card").flip({ axis: 'y' }); $("#card").on('flip:change', function() { $('#card').flip('toggle'); }); ax = 'y'; } else { $("#card").flip('toggle'); } }); $('#right').click(function() { if (ax != 'x') { $("#card").flip({ axis: 'x' }); $("#card").on('flip:change', function() { $('#card').flip('toggle'); }); ax = 'x'; } else { $("#card").flip('toggle'); } }); });
#card { position: fixed; left: 50px; top: 50px; width: 200px; height: 200px; } .front { width: 100%; height: 100%; background-color: red; } .back { width: 100%; height: 100%; background-color: yellow; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://cdn.rawgit.com/nnattawat/flip/master/dist/jquery.flip.min.js"></script> <button id="left">y</button> <button id="right">x</button> <div id="card"> <div class="front">Front content</div> <div class="back">Back content</div> </div>
source share