Created
November 5, 2017 19:30
-
-
Save PauliusKrutkis/891513ec32e7dac2b500e6189fa9c36d to your computer and use it in GitHub Desktop.
Javascript - change div position to hovered div center
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var $tooltip = $('.tooltip'); | |
$('.box').mouseenter(function () { | |
var $this = $(this); | |
var offset = $this.offset(); | |
var width = $this.width(); | |
var height = $this.height(); | |
var centerX = offset.left + width / 2; | |
var centerY = offset.top + height / 2; | |
$tooltip.css({ | |
'transform': 'translate(' + (centerX - ($tooltip.width() / 2)) + 'px, ' + (centerY - ($tooltip.height() / 2)) + 'px)' | |
}); | |
setTimeout(function () { | |
$tooltip.addClass('active'); | |
}, 1) | |
}); | |
$('.box-container').mouseleave(function () { | |
$tooltip.removeClass('active'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment