我有一个按钮( .sidebar-button ),它会隐藏/显示侧边栏。当屏幕尺寸小于时,@ mediaqueries会隐藏侧边栏。 800px,并在屏幕尺寸> 1时自动显示侧边栏。 800px,所以一切都是对的。
问题是当我按下按钮( .sidebar-button )时。
按钮的逻辑是正确的,但是当我点击按钮,然后增加屏幕的大小时,主div(.sidebar / .event)中的一个仍保持不可见,即使我输入了这个@ mediaquery
@media (min-width: 800px) {
.sidebar{
display: block;
}
.event{
display: block;
}
}
$(document).ready(function() {
var flagEvent = false;
$(".sidebar-button").bind('touchstart click', function() {
if (!flagEvent) {
flagEvent = true;
setTimeout(function() {
flagEvent = false;
}, 100);
if ($(".sidebar").css("display") == "none") {
$(".sidebar").css("display", "block");
$(".event").css("display", "none");
} else {
$(".sidebar").css("display", "none");
$(".event").css("display", "block");
}
}
return false
});
});
$(document).load($(window).bind("resize", listenResizeEvent));
function listenResizeEvent() {
/*if($(window).width() >= 800){
$(".sidebar").css("display" , "block");
$(".event").css("display" , "block");
}*/
}
html {
width: 100%;
}
body {
font-family: 'Roboto', sans-serif;
font-weight: 300;
background-color: #004050;
background: #004050;
margin: 0;
}
::selection {
background: transparent;
}
::-moz-selection {
background: transparent;
}
.forboth {
float: none;
clear: both;
height: 0;
}
.container {
width: 100%;
max-width: 1024px;
min-width: 320px;
margin: auto;
margin-top: 10px;
}
.header {
width: 100%;
height: 100%;
}
.logo {
background-image: url(../img/logo.png);
background-repeat: no-repeat;
width: 100%;
max-width: 271px;
height: 111px;
background-size: 100% auto;
margin: 10px;
}
.cat-menu {
width: 100%;
}
.cat-menu > div {
width: 25%;
font-size: 30px;
color: white;
text-align: center;
font-weight: 100;
float: left;
line-height: 60px;
height: 60px;
}
.cat-menu > div:nth-child(1) {
background-color: #2c86ee;
background: #2c86ee;
}
.cat-menu > div:nth-child(2) {
background-color: #6cb71e;
background: #6cb71e;
}
.cat-menu > div:nth-child(3) {
background-color: #d44a27;
background: #d44a27;
}
.cat-menu > div:nth-child(4) {
background-color: #af1a3f;
background: #af1a3f;
}
.cat-menu > div:nth-child(5) {
float: none;
clear: both;
height: 0;
}
.container-data {
width: 100%;
margin-top: 3px;
}
.sidebar {
width: 27%;
float: left;
background: transparent;
display: block;
}
.sidebar .sidebar-content {
position: relative;
margin-right: 3px;
background-color: blue;
}
.event {
width: 73%;
float: left;
background-color: green;
display: block;
}
.search {
width: 100%;
height: 40px;
background-color: #ff9201;
background: #ff9201;
margin-bottom: 3px;
}
.sidebar-button {
float: left;
display: none;
margin-left: 5px;
cursor: pointer;
width: 40px;
/* FIX FOR JSFIDDLE*/
height: 40px;
/* FIX FOR JSFIDDLE*/
background: red;
/* FIX FOR JSFIDDLE*/
}
/*.sidebar-button > span::before{
font-size: 40px;
line-height: 40px;
color: white;
}*/
.search-textfield {
float: right;
height: 30px;
width: 200px;
background: white;
margin-top: 5px;
}
.textfield {
height: 30px;
width: 100%;
padding: 0;
margin: 0;
outline: none;
text-decoration: none;
font-size: 15px;
border: none;
border-radius: 0;
padding-left: 5px;
}
.search-button {
float: right;
width: 40px;
height: 40px;
padding-left: 15px;
cursor: pointer;
}
.search-button > span::before {
font-size: 40px;
line-height: 40px;
color: white;
}
.sidebar-user {
position: relative;
width: 100%;
background-color: #ff7801;
background: #ff7801;
}
.sidebar-user-div1 {
width: 100%;
height: 40px;
}
.sidebar-user-div2 {
width: 100%;
height: 30px;
color: white;
font-size: 12px;
padding-left: 10px;
text-decoration: none;
}
.sidebar-user-div11 {
box-sizing: border-box;
float: left;
width: 80%;
height: 40px;
line-height: 40px;
color: white;
padding-left: 10px;
overflow: auto;
overflow-x: hidden;
}
.sidebar-user-div12 {
float: left;
width: 20%;
height: 40px;
}
.sidebar-user-div12 > span::before {
float: right;
margin-top: 5px;
padding-right: 15px;
font-size: 30px;
line-height: 30px;
color: white;
}
@media (max-width: 750px) {
.cat-menu > div {
font-size: 20px;
height: 40px;
line-height: 40px;
font-weight: 300;
}
}
@media (max-width: 500px) {
.cat-menu > div {
font-size: 15px;
height: 30px;
line-height: 30px;
}
.header .logo {
margin: auto;
margin-top: 10px;
margin-bottom: 10px;
width: 80%;
}
}
@media (max-width: 400px) {
.cat-menu > div:nth-child(1) {
width: 50%;
}
.cat-menu > div:nth-child(2) {
width: 50%;
}
.cat-menu > div:nth-child(3) {
/*float: none;*/
width: 50%;
}
.cat-menu > div:nth-child(4) {
float: left;
width: 50%;
}
}
<div class="container">
<div class="header">
<div class="logo"></div>
</div>
<div class="cat-menu">
<div>TEST1</div>
<div>TEST2</div>
<div>TEST3</div>
<div>TEST4</div>
<div class="forboth"></div>
</div>
<div class="container-data">
<div class="search">
<div class="sidebar-button">
<span class="flaticon-menu55"></span>
</div>
<div class="search-button">
<span class="flaticon-search9"></span>
</div>
<div class="search-textfield">
<input placeholder="cerca.." class="textfield" />
</div>
</div>
<div class="sidebar">
<div class="sidebar-content">
<div class="sidebar-user">
<div class="sidebar-user-div1">
<div class="sidebar-user-div11">
User name
</div>
<div class="sidebar-user-div12">
<span class="flaticon-user62"></span>
</div>
<div class="forboth"></div>
</div>
<div class="sidebar-user-div2">
Non sei tu? LOGOUT
</div>
</div>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</div>
</div>
<div class="event">
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
</div>
<div class="forboth"></div>
</div>
</div>
答案 0 :(得分:1)
原因是在您的点击处理程序中,您将元素上的css设置为display:none
,这会添加到元素内联样式中。即<div class="sidebar" style="display: none;">
。然后,当屏幕大小更改为800以上时,媒体css规则会更改元素上的样式,但内联样式元素会覆盖css规则,因此元素保持隐藏状态。不是直接设置元素的css,而是添加/删除一个类来隐藏/显示它,以便媒体css规则可以覆盖它。
if($(".sidebar").hasClass("show")){
$(".sidebar").addClass("hide").removeClass("show");
$(".event").addClass("show").removeClass("hide");
}else{
$(".sidebar").addClass("show").removeClass("hide");
$(".event").addClass("hide").removeClass("show");
}
更新
请参阅http://jsfiddle.net/df2f8qm7/2/
我在媒体中添加了.show和.hide规则:
@media (max-width: 800px) {
.sidebar{
display: none;
width: 100%;
}
.event{
width: 100%;
display: block;
}
.sidebar .sidebar-content{
margin-right: 0px;
}
.sidebar-button{
display: block;
}
.show
{
display:block;
}
.hide
{
display:none;
}
}
@media (min-width: 800px) {
.sidebar{
display: block;
}
.event{
display: block;
}
.show
{
display:block;
}
.hide
{
display:block;
}
}