50
rated 0 times
[
57]
[
7]
/ answers: 1 / hits: 24665
/ 8 Years ago, tue, april 19, 2016, 12:00:00
The error is that _this.offset
is not a function. I logged this to the console and it was the <li>
element I clicked on, so I am confused why this would not work.
$('.item').click(function(e) {
var _this = this;
var topx = _this.offset().top;
var leftx = _this.offset().left;
var moveArea = $('#replace').offset().top;
var moveLeft = $('#replace').offset().left;
var moveUp = topx - moveArea - 50;
_this.css('position', 'absolute').css('top', moveUp).css('zIndex', 50).css('left', leftx);
_this.animate({
top: -50,
left: moveLeft
}, 300)
});
#replace {
height: 50px;
width: 100px;
background-color: green;
}
#list {
height: 200px;
overflow-y: scroll;
}
.item {
height: 50px;
width: 100px;
background-color: blue;
}
<script src=https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js></script>
<div id=replace>
Replace this
</div>
<ul id=list>
<li class=item>TEST</li>
<li class=item>TEST</li>
<li class=item>TEST</li>
</ul>
I also want to create an animation such that the item I click in the list moves up to replace the green Replace this box, if someone could help with that too.
I created a jsfiddle to show the error: https://jsfiddle.net/v5fjjwmj/2/
More From » jquery