The easiest way to achieve want you want is to use only CSS, mainly the transition property.
It was already demonstrated by 5aledmaged in his answer
Here's a JS solution:
The reason it doesn't work is because this within the callback you pass into setTimeout is not the same as this within the callback you pass into .hover().
$('button').hover(function() {
var outer = this;
setTimeout(function(){
outer === this; // false
}, 500);
// ...
What you can do is save a reference to the outer this and use it in the inner callback:
var $this = $(this); // save the `this` you need
window.setTimeout(function() {
$this.css('transform', 'scale(1.3, 1.3)'); // and use it here
}, 500);
Demo:
$('button').hover(function() {
var $self = $(this); // save the `this` you need
window.setTimeout(function() {
$self.css('transform', 'scale(1.3, 1.3)'); // and use it here
}, 500);
}, function() {
$(this).css('transform', 'none');
});
button {
margin: 50px 0 0 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>
Check
</button>
Or use an arrow function which maintains its outer context:
window.setTimeout(() => {
$(this).css('transform', 'scale(1.3, 1.3)'); // `this` is the same as outer `this`
}, 500);
Demo:
$('button').hover(function() {
var $self = $(this); // save the `this` you need
window.setTimeout(() => {
$(this).css('transform', 'scale(1.3, 1.3)'); // `this` is the same as outer `this`
}, 500);
}, function() {
$(this).css('transform', 'none');
});
button {
margin: 50px 0 0 200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>
Check
</button>