[英]How can I sync CSS and JavaScript for a responsive layout? (Vars and @media)
I created a menu for a web application using JQuery and CSS.我使用 JQuery 和 CSS 为 web 应用程序创建了一个菜单。 This consists basically of four states: Hidden (mobile), minimzed (desktop), little (desktop) and big (mobile).这基本上包括四种状态:隐藏(移动)、最小化(桌面)、小(桌面)和大(移动)。
A cookie is initiliazed whether the user activated or deactivated the menu when reloading the web page (it's PHP).在重新加载 web 页面(它是 PHP)时,无论用户是否激活了菜单,都会初始化一个 cookie。
The problem is that if the browser is resized, CSS deletes the menu (display: none).问题是如果浏览器调整大小,CSS 会删除菜单(显示:无)。 JQuery is of course not aware of this circumstance, because I do not change the variables (cookies) during the runtime. JQuery 当然不知道这种情况,因为我不会在运行时更改变量(cookies)。
So, how can I actually sync JavaScript and CSS?那么,我如何才能真正同步 JavaScript 和 CSS? Or should I go only for one language?或者我应该只使用一种语言 go 吗? Here's some code.这是一些代码。 Thanks for some problem I'd really appreciate a good way to solve this issue!感谢一些问题,我真的很感激解决这个问题的好方法!
JavaScript JavaScript
/* ____________ PRE-LOADING USER-SETTINGS & COOKIE HANDLING ___________ */
$(document).ready(function(){
if (Cookies.get('menuToggled') == '1') {
if (isMobileDevice()) {
$('.grid-main').css('grid-template-columns', ""); // "" resets to default
Cookies.set('menuToggled', '0');
// I do this because after every initial load, the menu must be closed in mobile view.
}
else {
$('.grid-main').css('grid-template-columns', ""); // "" resets to default
}
}
else {
if (isMobileDevice()) {
$('.grid-main').toggleClass("closedMenu");
$('.main-menu').css("display", "none");
}
else {
$('.grid-main').toggleClass("smallMenu");
$('nav span').css("display", "none");
}
}
});
/* ######################################################### */
/* ##################### DOM FUNCTIONS ##################### */
/* ######################################################### */
/* Menu Toggler */
$(document).ready(function() {
$( "a.header-burger-menu" ).click(function() {
$('.grid-main').attr('class', 'grid-main'); // Resets all sub-classes
$('.grid-main').css('grid-template-columns', ""); // "" Resets to default value
$('.main-menu').css("display", "flex");
if (Cookies.get('menuToggled') == '1') {
if (isMobileDevice()) {
$('.grid-main').toggleClass("closedMenu");
$('.main-menu').css("display", "none");
}
else {
$('.grid-main').toggleClass("smallMenu");
}
$('nav span.mobileHandler').css('display', 'none');
Cookies.set('menuToggled', '0');
}
else {
if (isMobileDevice()) {
$('.grid-main').toggleClass("bigMenu");
$('.main-menu').css("display", "flex");
}
else {
$('.grid-main').attr('class', 'grid-main'); // Resets all sub-classes
$('.grid-main').css('grid-template-columns', ""); // "" resets to default value
}
$('nav span.mobileHandler').css('display', 'block');
Cookies.set('menuToggled', '1');
}
});
CSS CSS
/* ################################################# */
/* ############### MOBILE SECTION ################## */
/* ################################################# */
/* CSS - MENU TOGGLER for JQUERY */
.grid-main.smallMenu {
/* No empty space between the classes to refer "smallMenu" to "grid-main" */
grid-template-columns: 80px 1fr 1fr !important;
}
.grid-main.bigMenu {
grid-template-columns: 100% 0fr 0fr;
}
.grid-main.closedMenu {
grid-template-columns: 0px 1fr 1fr;
}
@media(max-width: 576px) {
header h1 {font-size: 2rem;}
header .header-chat {
width: 50px;
}
header .header-login {
width: 50px;
}
.grid-main {
font-size: .7rem;
grid-template-columns: 0px 1fr 1fr;
grid-template-rows: 0.1fr 1fr 0.08fr;
}
nav .main-menu {
flex-direction: row;
flex-wrap: wrap;
justify-content: left; /* Horizontal pseudo centering */
align-content: flex-start; /* Vertical pseudo centering */
justify-items: center;
margin: 40px 10px 0px 10px;
overflow-y: overlay;
}
nav .main-menu.mobileHandler {
display: none;
}
nav span.mobileHandler {
display: none;
/* To hide nav textes in closed or small view, only icons */
}
In your JQuery script, before performing whatever action it is you are trying to do, check to see if the element is hidden or whatever else it is that occurs when the page is shrunk.在您的 JQuery 脚本中,在执行您尝试执行的任何操作之前,请检查该元素是否隐藏或页面缩小时发生的其他任何情况。
if($('.element').css('display')=='none'){
//Do what you gotta do in here
}
So I solved the issue with the following code:所以我用下面的代码解决了这个问题:
function MainMenu() {
if (Cookies.get('menuToggled') == '1') {
if (isMobileDevice()) {
$('.grid-main').css('grid-template-columns', ""); // "" resets to default
Cookies.set('menuToggled', '0');
// I do this because after every initial load, the menu must be closed in mobile view.
}
else {
$('.grid-main').css('grid-template-columns', ""); // "" resets to default
}
}
else {
if (isMobileDevice()) {
$('.grid-main').toggleClass("closedMenu");
$('.main-menu').css("display", "none");
}
else {
$('.grid-main').toggleClass("smallMenu");
$('nav span').css("display", "none");
}
}
}
/* _________________ PRE-LOADING USER-SETTINGS & COOKIE HANDLING __________________ */
$(document).ready(function(){
MainMenu();
});
/* ######################################################### */
/* ##################### DOM FUNCTIONS ##################### */
/* ######################################################### */
$( window ).resize(function() {
MainMenu();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.