I've tried to get more familiar with transitions lately so I can use css to style my animations rather than scripts.
My Objective is a modal window (fixed position) which comes up when a certain button is clicked. I would like to have a visual effect like the button spawns the modal, so the animation ought to originate from the position of the button.
I'll insert some code at the end.
The question I have is whether there is a way without js to set the starting point of the animation (the position of the div while it is invisible) to the same location as the button. I assume it ought to be possible by simply having the div in the DOM right by that button. However how do I use a transition to animate between a DOM-bound position to a fixed position?
If possible I would like to use Javascript only to switch classes. But I would be ok with reading the position/offset of the button, too. I however got stuck with my attempts so far.
Anyone able to steer me on track again? Thank you.
jQuery(document).ready(function() { jQuery('button').on('click', function() { if (jQuery('div').hasClass('orig')) { jQuery('div').offset(jQuery('button').offset()).removeClass('orig').addClass('alter'); } else jQuery('div').removeClass('alter').addClass('orig'); }); }); .orig { overflow: hidden; position: fixed !important; margin-left: 0px; margin-top: 0px; width: 0px; height: 0px; background-color: white; border: 0px solid black; border-radius: 3px; transition-property: all; transition-duration: 0.2s; } .alter { overflow: hidden; position: fixed !important; top: 50% !important; left: 50% !important; margin-left: -100px; margin-top: -50px; width: 200px; height: 100px; background-color: white; border: 1px solid black; border-radius: 3px; transition-property: all; transition-duration: 0.2s; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <body><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> <button>klick</button> <div class="orig">Lorem Ipsum ganz viel text</div> <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /> </body>
top: -21px; left: 0px.