Can this be achieved by only modifying the class of the
custom dropdown menu?
By just modifying the top, left, right, bottom
par
After discussing with the OP, here's what was wanted:
[This is made for bootstrap, edit the class to match your needs]
.reverse {
top:auto;
bottom:100%;
}
$(".dropdown-toggle").click(function(){
// get the scollTop (distance scrolled from top)
var scrollTop = $(window).scrollTop();
// get the top offset of the dropdown (distance from top of the page)
var topOffset = $(".dropdown").offset().top;
// calculate the dropdown offset relative to window position
var relativeOffset = topOffset-scrollTop;
// get the window height
var windowHeight = $(window).height();
// if the relative offset is greater than half the window height,
// reverse the dropdown.
if(relativeOffset > windowHeight/2){
$(".dropdown-menu").addClass("reverse");
}
else{
$(".dropdown-menu").removeClass("reverse");
}
});
Same goes for this, this will work on a bootstrap dropdown, so you'll need to update the selectors if you want to use another framework.
The idea is to calculate the difference between the top
offset of the element and the current scrollTop
, then add the reverse
class to the inner ul
depending on the comparison between that value and the middle of the page.
As per requested, here's how I tweaked things around a bit.
N.B. This uses Twitter Bootstrap, both for styling and markup and for javascript, but it is by no means necessary and can be recreated without too much hassle.
As asked, I added two reverse-toggle
elements to the dropdown, (one at the beginning, the other at the end).
<li role="presentation" class="reverse-toggle top-reverse hidden"><a role="menuitem" tabindex="-1" href="javascript:void(0)">Reverse towards top</a></li>
The important part here is the reverse-toggle top-reverse
classes. The rest is Bootstrap markup.
It is also important to add the javascript:void(0)
to the a
element, if this isn't added the dropdown will close when the buttons are clicked.
As stated earlier, the only important rules here are those given by the .reverse
class.
This class will be applied to the ul
containing the dropdown and is responsible for the reversed state.
.reverse {
top: auto;
bottom:100%;
}
$(window).scroll(function () {
// if the dropdown is "high enough" on the page, show the toggle commands
if(($(window).scrollTop() + $(window).height() > $(document).height() - 256)){
if($(".dropdown-menu").hasClass("reverse")){
// if the dropdown is already reversed
$(".bottom-reverse").removeClass("hidden");
$(".top-reverse").addClass("hidden");
}
else{
$(".top-reverse").removeClass("hidden");
$(".bottom-reverse").addClass("hidden");
}
}
else{
$(".top-reverse").addClass("hidden");
$(".bottom-reverse").addClass("hidden");
}
});
$(".reverse-toggle").click(function(){
$(".dropdown-menu").toggleClass("reverse").dropdown("toggle");
$(".top-reverse").toggleClass("hidden");
$(".bottom-reverse").toggleClass("hidden");
});
The JS here does the two following things:
if
) and the state of the dropdown (second if
, either reverse
or not), the correct button is shown in the dropdown.reverse-toggle
buttons is clicked, the dropdown ul
changes states.Here
My solution works the following way:
reverse
class that specifies the dropdown location (in twitter bootstrap, that class contains only top:auto;
and bottom:100%
)scrollTop()
value.It can pretty easily be ported to other frameworks assuming you find the correct class to add.
See the fiddle.