I’m trying to get the red div to come up and sit under the click me link when that link is clicked.
$('.clickMe').click(function() {
var pos = $(this).offset().top;
$('.myDiv').animate({
marginTop: '-'+pos+'px',
}, 1000 );
});
<a href="" class="clickMe">Click Me</a>
<br /><br /><br /><br /><br /><br /><br /><br />
<div style="position: absolute; border: 1px solid red; height: 50px; width: 50px" class="myDiv"></div>
Here’s my failed attempt:
How do I get this to work?
Try this instead, animating the
topproperty is probably a better choice, negative margins can have undesirable effects:Demo: http://jsfiddle.net/SWqmb/6/