Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
84
rated 0 times [  90] [ 6]  / answers: 1 / hits: 18385  / 7 Years ago, thu, september 28, 2017, 12:00:00

I am using popper.js for displaying more post categories if trigger clicked. But after i click it, the initial position is wrong (more right). When popper opened and i scroll the position of popper is updated - this state need to be initial.



I found that CSS position transform of popper is calculated wrong for my usage. But don't understand, why it is recalculated to right number after scroll or any window resize. Check the Codepen below.



My JS:



$(function () {
var element = document.getElementById('more-cats');
var dropdown = document.getElementById('tooltip');

var catsTooltip = new Popper ( element, dropdown, {
placement: 'bottom-end'
});

$(element).click(function(e) {
$(dropdown).toggle();
e.preventDefault();
});

});


Codepen



Thank you, if you have some ideas, what part of code i wrote wrong.


More From » tooltip

 Answers
12

That's because Popper.js needs the popper element to be rendered in the DOM (aka, have a position in the document) to be able to properly compute its position.



You are initializing Popper.js when the element is hidden, and then you toggle its visibility to show it, but Popper.js doesn't know that something changed.



When you scroll the page, or resize it, Popper.js updates the position of your popper because it listens by default to these events.



You should manually run catsTooltip.scheduleUpdate() after .toggle() to have it properly positioned.



https://codepen.io/FezVrasta/pen/PJjWWZ


[#56360] Monday, September 25, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mckaylab

Total Points: 311
Total Questions: 120
Total Answers: 93

Location: Montenegro
Member since Thu, Jun 16, 2022
2 Years ago
;