Reputation: 193
I'm working on a onclick event (html) with jquery. In my jquery i set the width for this event.
Though i want a different width for the mobile website. For this I should work with media queries. On desktop it is 50vw, on phone (triggered from 600px) it should be 100vw... Though how to make this work properly?
function openMenu() {
document.getElementById("menuNav").style.width = "50vw";
}
function closeMenu() {
document.getElementById("menuNav").style.width = "0vh";
}
Upvotes: 1
Views: 2100
Reputation: 73
You can use jQuery's width() method to find the width of the window (IE the browser viewport) and determine whether 50vw or 100vw should be used:
function openMenu() {
if($(window).width() > 600) {
document.getElementById("menuNav").style.width = "50vw";
} else {
document.getElementById("menuNav").style.width = "100vw";
}
}
Upvotes: 1
Reputation: 108
if($(window).width() < 601) {
// change functionality for smaller screens
} else {
// change functionality for larger screens
}e
or
if(window.matchMedia('(max-width: 600px)').matches)
{
// do functionality on screens smaller than 600px
}
Upvotes: 2
Reputation: 386
Define .open class and .closed class in CSS where you can use media queries normally. Then switch CSS classes on menuNav
with JavaScript. See documentation for classList: https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
Upvotes: 3