【问题标题】:Reset display property with @mediaquery after change it with jQuery使用 jQuery 更改后使用 @mediaquery 重置显示属性
【发布时间】:2014-12-26 07:54:44
【问题描述】:

我有一个按钮(.sidebar-button),它隐藏/显示侧边栏。 @mediaqueries 在屏幕尺寸 800px 时自动显示侧边栏,所以一切正常。

问题是当我点击按钮(.sidebar-button)时。

按钮的逻辑是正确的,但是当我点击按钮,然后增加屏幕的大小时,即使我输入了这个@,主 div (.sidebar/.event) 之一仍然不可见媒体查询

@media (min-width: 800px)  {
    .sidebar{
        display: block;
    }
    .event{
        display: block;
    }
}

javascript

$(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");
  }*/
}

css

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%;
  }
}

html

<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>

JSFIDDLE

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    原因是在您的单击处理程序中,您将元素上的 css 设置为 display:none,它被添加到元素内联样式中。即&lt;div class="sidebar" style="display: none;"&gt;。然后当屏幕大小变为 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;
    }
    }
    

    【讨论】:

    • 感谢您的回复。我创建了两个类 .show { display: block; } .hide { 显示:无; },但不起作用,当我按下按钮时,div 没有隐藏
    • 类添加成功,但是查看谷歌浏览器的控制台发现属性 display: none/block is not accepted
    猜你喜欢
    • 2015-12-14
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 2019-08-01
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    相关资源
    最近更新 更多