Skip to content

Instantly share code, notes, and snippets.

@PauliusKrutkis
Created November 5, 2017 19:30
Show Gist options
  • Save PauliusKrutkis/891513ec32e7dac2b500e6189fa9c36d to your computer and use it in GitHub Desktop.
Save PauliusKrutkis/891513ec32e7dac2b500e6189fa9c36d to your computer and use it in GitHub Desktop.
Javascript - change div position to hovered div center
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