/    Sign up×
Community /Pin to ProfileBookmark

Align popup menu right

The popup now works almost as wanted. But when run out of space, I want to align right in order to view the menu content. How do I do this?

https://jsfiddle.net/ksn0b7wc/

to post a comment
CSSJavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMar 08.2022 — You can check if the menu exceeds the right edge of the body by creating a bounding box for menu and body. Then compare the property `right</C> of both:
<CODE>
`<i>
</i> function popup(menuCell, id) {
menuCell.appendChild(menu);
// remove class "stick-right":
menu.classList.remove('stick-right');
const
// get bounding box of menu and body:
boxMenu = menu.getBoundingClientRect(),
boxBody = document.querySelector('body').getBoundingClientRect();
console.log(boxMenu.left, boxMenu.right, boxBody.left, boxBody.right);
// does the menu exceed the right edge of the body?
if (boxMenu.right &gt; boxBody.right) {
// add class "stick-right" in order to make the menu
// stick to the right edge of the body
menu.classList.add('stick-right');
}
// make menu visible:
menu.classList.remove('hide');
}<i>
</i>
``
I added some modifications to the CSS in order to make this work.

Note that li elements cannot be children of a div, it has to be an ul element.

https://jsfiddle.net/azb519wn/
×

Success!

Help @sibert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...