0

I have a sidebar on my website which I hide on a click with.

$('#sidebar').toggle('slide', 'left', 300)

I also have a CSS style which hides this sidebar mobiles.

#sidebar {
    display: none;
}

/* if screen size gets wider than 768 */
@media screen and (min-width:768px) {
    #sidebar {
        display: block;
    }
}

However these 2 don't work together very well because .toggle() uses inline CSS. Is there any way to make .toggle() use an inline style? Or would there be a better solution to this.

Thanks! Thomas

6
  • !important in your css overrules inline css Commented Jul 27, 2016 at 7:00
  • Yes, but this always shows the sidebar even when I use .toggle() to hide it. Unless I'm missing something? Commented Jul 27, 2016 at 7:10
  • When do you want it to show, and when do you want it hidden? Commented Jul 27, 2016 at 7:44
  • Default it's shown (so the #sidebar has a display: block). When you click on a link it's hidden, when you click on it again it's shown etc. Commented Jul 27, 2016 at 7:48
  • 1
    There's no real problem there (though you might want to change it to 767px for max-width), some purists would tell you that it breaks the 'mobile first' principle, but unless you fancy rewriting jQuery.toggle() you do what you gotta. Commented Jul 27, 2016 at 8:05

1 Answer 1

1

Do it with classes and toggle that class. Now it will work together because it doesnt matter if the sidebar is open.

CSS:

#sidebar {
    display: none;
    -webkit-transform: translate(-300px, 0px);
    -ms-transform: translate(-300px, 0px);
    transform: translate(-300px, 0px);
    -webkit-transition: all 1s; 
    -moz-transition: all 1s; 
    transition: all 1s;
}
#sidebar.open {
    -webkit-transform: translate(0px, 0px);
    -ms-transform: translate(0px, 0px);
    transform: translate(0px, 0px);
}

/* if screen size gets wider than 768 */
@media screen and (min-width:768px) {
    #sidebar {
        display: block;
    }
}

Also when using animations to open something. Use transform for browser optimization.

jQuery:

$('#sidebar').toggleClass('open')
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks! This seems to have nearly gotten it working but the sidebar stays hidden when I put display: none on it. When this isn't on there it works but the div is not removed from the DOM.
Can you try display: block !important; ?
It works, but then the div isn't removed from the DOM (which I need). I realise this is a harder matter it seems.
If you mean removed, like delete the html. You cant do that with css. thats only possible with javascript/jquery. $('#sidebar').remove();
You can detect it the screensize by jquery and then delete it if that is what you want
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.