javascript - Keep absolute element (dropdown-menu) in viewport or window width -
<li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">dropdown <span class="caret"></span></a> <ul class="dropdown-menu menu-multicolumn"> <li class="menu-column"> <ul> <li class="menu-column-title">multi page</li> <li><a href="#">action</a></li> <li><a href="#">another action</a></li> <li><a href="#">something here</a></li> <li><a href="#">separated link</a></li> <li><a href="#">separated link</a></li> </ul> </li> <li class="menu-column"> <ul> <li class="menu-column-title"></li> <li><a href="#">action</a></li> <li><a href="#">another action</a></li> <li><a href="#">something here</a></li> <li><a href="#">separated link</a></li> <li><a href="#">separated link</a></li> </ul> </li> <li class="menu-column"> <ul> <li class="menu-column-title">one page</li> <li><a href="#">action</a></li> <li><a href="#">another action</a></li> <li><a href="#">something here</a></li> <li><a href="#">separated link</a></li> <li><a href="#">separated link</a></li> </ul> </li> <li class="menu-column"> <ul> <li class="menu-column-title"></li> <li><a href="#">action</a></li> <li><a href="#">another action</a></li> <li><a href="#">something here</a></li> <li><a href="#">separated link</a></li> <li><a href="#">separated link</a></li> </ul> </li> </ul> </li><!-- end of dropdown -->
css
.dropdown-menu { background-clip: padding-box; background-color: #fff; border: 1px solid rgba(0, 0, 0, 0.15); border-radius: 4px; box-shadow: 0 6px 12px rgba(0, 0, 0, 0.176); display: none; float: left; font-size: 14px; list-style: outside none none; margin: 2px 0 0; min-width: 160px; padding: 5px 0; position: absolute; text-align: left; top: 100%; z-index: 1000;} .menu-multicolumn { white-space: nowrap; width: auto !important;} .menu-column { display: inline-block; float: none; overflow: hidden; vertical-align: top; white-space: normal; min-width: 190px;}
ok thinking javascript determine how .dropdown-menu element out of window width , set same width left: -(that width);, .dropdown-menu element have flexible left position , inside window width.
basically need determine how element out of window width , set same amount minus left absolute position of same element.
maybe thought simple, if right way, , want right side stay inside viewport, this:
.dropdown-menu { position: absolute; right: 0px; left: auto; }
instead of calculating how should push left side in negative margin can set space right side zero!
Comments
Post a Comment