I have a drop down menu that is positioned off the page (negative margin) upon a button click I want the page to be pushed down and the menu to appear. I'm using CSS3 to achieve this and have this working fine.
My next step for the menu is to make it more dynamic, I want to use JQuery to set the margin values so that regardless of how many items are in the list (of my menu) the margins will update accordingly.
I've very close to getting this working, I've got all the math but i'm having a problem when toggling the css class to change the height. In order to get the class to overwrite my current styles I need to use the !important rule (frowned upon and I hate it!), firstly I'm having trouble using the !important rule with JQuery (I've read duplicate posts on this) but ideally I want a way to achieve the same without having to use !important, anyone any ideas?
The class i'm toggling is .body-change
Code Below:
<div id="menu">
<ul>
<li><a href="#home" class="contentLink">Guide</a></li>
<li><a href="#home" class="contentLink">Contents</a></li>
<li><a href="#home" class="contentLink">Calculator</a></li>
</ul>
</div>
.body-change{margin-top:141px !important}
.ui-page-active{
-webkit-transition: margin-top 0.35s linear;
-moz-transition: margin-top 0.35s linear;
-o-transition: margin-top 0.35s linear;
-ms-transition: margin-top 0.35s linear;
transition: margin-top 0.35s linear;
}
$(function() {
var numItems = $("#menu ul").children().length,
height = $('#menu ul li').outerHeight(),
margin = numItems * height;
console.info('Margin is ' + margin + 'px');
// Toggle menu
$('#menu').css('margin-top', -margin)
$('.body-change').css('margin-top', margin)
$("#sidemenu").on('click', function() {
$(".ui-page-active").toggleClass("body-change");
});
});
As show in the JQuery, I set the margin of the menu to the correct negative value on load. I then toggle the class body-change, what I need to do is set the css of the body change in the JQuery but I struggle to do this because of the !important rule needing to be used..
Any ideas?
!importantis meant to prevent other styles from overriding (including inline styles). You will probably have to find an alternative around that. Why is the!importantneeded anyway? And which element has the classui-page-active? It would be best if you can post a fiddle.