如何在页面加载时折叠元素

时间:2017-03-13 11:03:18

标签: javascript jquery html css html5

加载HTML页面时如何折叠div(菜单)?我尝试使用以下代码,但它不起作用,因为菜单仍然可见。

  

页面加载后,我希望仅在@media screen and (max-width: 768px)上折叠菜单。当菜单被拼写时,按钮应该显示menu +。当菜单可见时,按钮应显示menu -

$('#togglelink').click(function() {
  $('ul').toggle(300);
  if ($(this).text() == "menu +")
    $(this).text("menu -")
  else
    $(this).text("menu +");
})
.wrapper {
  width: 90%;
  height: auto;
  margin: 10px auto 10px auto;
  border: 2px solid #000000;
  background-color: #0099cc;
}

@charset "utf-8";

/* CSS Document */

body {
  font-family: 'Work Sans', sans-serif;
}

.wrapper h1 {
  font-size: 2.75em;
  line-height: 100%;
  margin-left: 10px;
  margin-right: 10px;
  text-align: center;
  color: #fff;
}


/*Header menu*/

#menu {
  background: #ffffff;
  width: auto;
  margin-bottom: 5px;
}

#menu ul {
  list-style: none;
  margin: 0;
  padding: 0;
  line-height: 1;
  /*	display: block;
*/
  zoom: 1;
  /* Added the following for flexbox */
  display: flex;
  flex-wrap: wrap;
}

#menu ul:after {
  content: ' ';
  display: block;
  font-size: 0;
  height: 0;
  clear: both;
  visibility: hidden;
}

#menu ul li {
  /*	float: left;
*/
  display: block;
  padding: 0;
  /* Added the following for flexbox */
  flex-grow: 1;
}

#menu ul li a {
  color: #000;
  text-decoration: none;
  display: block;
  padding: 15px 25px;
  font-family: 'Work Sans', sans-serif;
  font-size: 1.5em;
  font-weight: 500;
  position: relative;
  -webkit-transition: color .25s;
  -moz-transition: color .25s;
  -ms-transition: color .25s;
  -o-transition: color .25s;
  transition: color .25s;
  /* Added the following for flexbox */
  /* So that text appear visually centered */
  text-align: center;
}

#menu ul li a:hover {
  color: #000;
}

#menu ul li a:hover:before {
  width: 100%;
}

#menu ul li a:before {
  content: '';
  display: block;
  position: absolute;
  left: 0;
  bottom: 0;
  height: 3px;
  width: 0;
  background: #000;
  -webkit-transition: width .25s;
  -moz-transition: width .25s;
  -ms-transition: width .25s;
  -o-transition: width .25s;
  transition: width .25s;
}

#menu ul li.last > a:after,
#cssmenu ul li:last-child > a:after {
  display: none;
}

#menu ul li.active a {
  color: #000;
}

#menu ul li.active a:before {
  width: 100%;
}

.menu_toggle {
    display: none;
}

@media screen and (max-width: 768px) {


  #menu ul li {
    float: none;
    width: 100%;
  }
  #menu ul li a {
    width: 100%;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }
  #menu ul li a:after {
    display: none;
  }
  #menu ul li a:before {
    height: 1px;
    background: #000;
    width: 100%;
    opacity: .2;
  }
  #menu ul li.last > a:before,
  #cssmenu ul li:last-child > a:before {
    display: none;
  }
  .menu_toggle {
    visibility: visible;
    background-color: white;
    border: 3px solid black;
    color: black;
    text-align: center;
    text-decoration: none;
    display: block;
    font-size: 16px;
    font-size: 1.5em;
    padding: 0.5em 2em;
    font-weight: 500;
    margin: 0 auto;
    margin-bottom: 10px;
    cursor: pointer;
  }
  .menu_toggle:active {
    background-color: black;
    color: white;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="wrapper">
  <div>
    <h1>Hello World</h1>
  </div>
  <div>
    <button class="menu_toggle" tabindex="0" id="togglelink">menu +</button>
  </div>
  <div id="menu">
    <ul>
      <li><a><span>January</span></a>
      </li>
      <li><a><span>February</span></a>
      </li>
      <li><a><span>March</span></a>
      </li>
      <li><a><span>April</span></a>
      </li>
      <li><a><span>May</span></a>
      </li>
      <li><a><span>June</span></a>
      </li>
      <li><a><span>July</span></a>
      </li>
      <li><a><span>August</span></a>
      </li>
      <li><a><span>September</span></a>
      </li>
      <li><a><span>October</span></a>
      </li>
      <li><a><span>November</span></a>
      </li>
      <li><a><span>December</span></a>
      </li>
    </ul>
  </div>

  <p>
    Hello World
  </p>
</div>

window.innerWidth&lt; 769个屏幕 enter image description here

window.innerWidth&gt; 769个屏幕 enter image description here

enter image description here

3 个答案:

答案 0 :(得分:3)

由于您需要display: flex,最简单的方法之一是使用JS,您只需在点击之前添加hide()函数。

这仍然会将您的菜单显示为display: flex,因为添加display: none会在打开时将菜单更改为display: block

&#13;
&#13;
$(document).ready(function() {
  if(window.innerWidth < 769) {
    $('#menu ul').hide();
  }
  $('#togglelink').click(function() {
    $('ul').toggle(300);
    if ($(this).text() == "menu +")
      $(this).text("menu -")
    else
      $(this).text("menu +");
  })
});
&#13;
.wrapper {
  width: 90%;
  height: auto;
  margin: 10px auto 10px auto;
  border: 2px solid #000000;
  background-color: #0099cc;
}

@charset "utf-8";

/* CSS Document */

body {
  font-family: 'Work Sans', sans-serif;
}


.wrapper h1 {
  font-size: 2.75em;
  line-height: 100%;
  margin-left: 10px;
  margin-right: 10px;
  text-align: center;
  color: #fff;
}


/*Header menu*/
#menu {
  background: #ffffff;
  width: auto;
  margin-bottom: 5px;
}

#menu ul {
  list-style: none;
  margin: 0;
  padding: 0;
  line-height: 1;
  /*	display: block;
*/
  zoom: 1;
  /* Added the following for flexbox */
  display: flex;
  flex-wrap: wrap;
}

#menu ul:after {
  content: ' ';
  display: block;
  font-size: 0;
  height: 0;
  clear: both;
  visibility: hidden;
}

#menu ul li {
  /*	float: left;
*/
  display: block;
  padding: 0;
  /* Added the following for flexbox */
  flex-grow: 1;
}

#menu ul li a {
  color: #000;
  text-decoration: none;
  display: block;
  padding: 15px 25px;
  font-family: 'Work Sans', sans-serif;
  font-size: 1.5em;
  font-weight: 500;
  position: relative;
  -webkit-transition: color .25s;
  -moz-transition: color .25s;
  -ms-transition: color .25s;
  -o-transition: color .25s;
  transition: color .25s;
  /* Added the following for flexbox */
  /* So that text appear visually centered */
  text-align: center;
}

#menu ul li a:hover {
  color: #000;
}

#menu ul li a:hover:before {
  width: 100%;
}

#menu ul li a:before {
  content: '';
  display: block;
  position: absolute;
  left: 0;
  bottom: 0;
  height: 3px;
  width: 0;
  background: #000;
  -webkit-transition: width .25s;
  -moz-transition: width .25s;
  -ms-transition: width .25s;
  -o-transition: width .25s;
  transition: width .25s;
}

#menu ul li.last > a:after,
#cssmenu ul li:last-child > a:after {
  display: none;
}

#menu ul li.active a {
  color: #000;
}

#menu ul li.active a:before {
  width: 100%;
}

.menu_toggle {
    display: none;
}

@media screen and (max-width: 768px) {


  #menu ul li {
    float: none;
    width: 100%;
  }
  #menu ul li a {
    width: 100%;
    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }
  #menu ul li a:after {
    display: none;
  }
  #menu ul li a:before {
    height: 1px;
    background: #000;
    width: 100%;
    opacity: .2;
  }
  #menu ul li.last > a:before,
  #cssmenu ul li:last-child > a:before {
    display: none;
  }
  .menu_toggle {
    visibility: visible;
    background-color: white;
    border: 3px solid black;
    color: black;
    text-align: center;
    text-decoration: none;
    display: block;
    font-size: 16px;
    font-size: 1.5em;
    padding: 0.5em 2em;
    font-weight: 500;
    margin: 0 auto;
    margin-bottom: 10px;
    cursor: pointer;
  }
  .menu_toggle:active {
    background-color: black;
    color: white;
  }
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="wrapper">
  <div>
    <h1>Hello World</h1>
  </div>
  <div>
    <button class="menu_toggle" tabindex="0" id="togglelink">menu +</button>
  </div>
  <div id="menu">
    <ul>
      <li><a><span>January</span></a>
      </li>
      <li><a><span>February</span></a>
      </li>
      <li><a><span>March</span></a>
      </li>
      <li><a><span>April</span></a>
      </li>
      <li><a><span>May</span></a>
      </li>
      <li><a><span>June</span></a>
      </li>
      <li><a><span>July</span></a>
      </li>
      <li><a><span>August</span></a>
      </li>
      <li><a><span>September</span></a>
      </li>
      <li><a><span>October</span></a>
      </li>
      <li><a><span>November</span></a>
      </li>
      <li><a><span>December</span></a>
      </li>
    </ul>
  </div>

  <p>
    Hello World
  </p>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

试试这个答案 https://jsfiddle.net/alfinpaul/umbtL1wq/2/

	var windowSize = $(window).width(); // Could've done $(this).width()
     if(windowSize < 768){


$('#menu ul').each(function(){
 $(this).slideUp();
 });
   } 
$('#togglelink').click(function() {
  $('ul').toggle(300);
  if ($(this).text() == "menu +")
    $(this).text("menu -")
  else
    $(this).text("menu +");
})
.wrapper {
  width: 90%;
  height: auto;
  margin: 10px auto 10px auto;
  border: 2px solid #000000;
  background-color: #0099cc;
}

@charset "utf-8";

/* CSS Document */

body {
  font-family: 'Work Sans', sans-serif;
}


/*'FACH Technical Support' title*/

.wrapper h1 {
  font-size: 2.75em;
  line-height: 100%;
  margin-left: 10px;
  margin-right: 10px;
  text-align: center;
  color: #fff;
}


/*Header menu*/

#menu {
  background: #ffffff;
  width: auto;
  margin-bottom: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="wrapper">
  <div>
    <h1>Hello World</h1>
  </div>
  <div>
    <button class="menu_toggle" tabindex="0" id="togglelink">menu +</button>
  </div>
  <div id="menu">
    <ul>
      <li><a><span>January</span></a>
      </li>
      <li><a><span>February</span></a>
      </li>
      <li><a><span>March</span></a>
      </li>
      <li><a><span>April</span></a>
      </li>
      <li><a><span>May</span></a>
      </li>
      <li><a><span>June</span></a>
      </li>
      <li><a><span>July</span></a>
      </li>
      <li><a><span>August</span></a>
      </li>
      <li><a><span>September</span></a>
      </li>
      <li><a><span>October</span></a>
      </li>
      <li><a><span>November</span></a>
      </li>
      <li><a><span>December</span></a>
      </li>
    </ul>
  </div>

  <p>
    Hello World
  </p>
</div>

答案 2 :(得分:0)

Helloo,

@media screen and (max-width: 768px)尝试添加:

#menu ul {
  display:none;
}

小提琴:https://jsfiddle.net/rq2a3b4b/1/