簡體   English   中英

如何在JavaScript中為推入式菜單添加過渡效果?

[英]how can i add a transition effect in javascript for a push-side menu?

如何為推入式菜單添加過渡效果? 我嘗試使用document.getElementById('content')。style.transition =“ left 1.5s”; 但它不起作用。 我希望得到一些幫助。.我是JS的初學者。

 //Javascript for showing the hidden menu document.getElementById("showMenu").addEventListener("click", menuShow); function menuShow () { document.getElementById('content').style.left = "20%"; document.getElementById('content').style.opacity = "0.6"; document.getElementById("content").style.transition = "opacity 2s"; }; //Javascript for hiding menu document.getElementById("hideMenu").addEventListener("click", menuHide); function menuHide () { document.getElementById('content').style.left = "0"; document.getElementById('content').style.opacity = "1"; document.getElementById("content").style.transition = "none"; }; 
 body { position: relative; margin: 0; } /*Style for the fixed header and show menu button*/ #header { width: 100%; height: 60px; background-color: grey; position: fixed; top: 0; z-index: 1; } #showMenu { height: 50px; width: 50px; text-align: center; background-color: transparent; border-radius: 5px; position: absolute; left: 40px; top: 5px; line-height: 0.2px; font-size: 50px; border: none; } #showMenu:hover { cursor: pointer; } /*Style for the content div*/ #content { height: 768px; width: 100%; background-color: #0B243B; position: absolute; top: 0px; left: 0%; -webkit-box-shadow: -7px 0px 15px 0px rgba(0,0,0,0.75); -moz-box-shadow: -7px 0px 15px 0px rgba(0,0,0,0.75); box-shadow: -7px 0px 15px 0px rgba(0,0,0,0.75); text-align: center; color: white; } p { width: 50%; margin: 0 auto; position: relative; top: 100px; text-align: justify; font-size: 20px; font-family: calibri; text-indent: 50px; } /*Style for the hidden menu div and hide menu button*/ #hiddenMenu { height: 765px; width: 20%; background-color: grey; position: fixed; } #hideMenu { height: 50px; width: 50px; background-color: white; border-radius: 3px; position: absolute; right: 30px; top: -25px; font-family: calibri; border: none; background-color: transparent; font-size: 80px; } #hideMenu:hover { cursor: pointer; color: white; } /*Style for the hidden menu links*/ #hiddenMenu ul li { list-style-type: none; font-family: calibri; font-size: 20px; position: relative; top: 100px; margin-top: 10px; margin-left: 20px; border-top: 1px solid black; width: 100px; padding-left: 10px; } #hiddenMenu ul li:hover { color: white; cursor: pointer; } 
 <div id=hiddenMenu> <button id=hideMenu> &#8249</button> <ul> <li><a>About </a></li> <li><a> Home </a></li> <li><a> Gallery </a></li> <li><a> Contact </a></li> </ul> </div> <div id=content> <div id=header> <button id=showMenu> &#9776 </button> </div> <p> Where does it come from? Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32. </p> <p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham. </p> </div> 

在CSS中將事務屬性用於隱藏的div

-webkit-transition: 2s; /* for Safari browser */
transition: 2s;

它將顯示所有變化(包括顏色變化)的平滑運動

使用animate.css

https://daneden.github.io/animate.css/下載animate.css

將其添加到您的項目

<button  class="fadeOutFade"> &#8249</button>
  <ul>
    <li><a>About </a></li>
    <li><a> Home </a></li>
    <li><a> Gallery </a></li>
    <li><a> Contact </a></li>
  </ul>
</div>
<div id=content>
  <div id=header>

從您的方法(您要向內容中添加20%的left CSS屬性),我建議您想從左側滑動菜單並將頁面移至右側...

所以您需要做的是:

#content {
  position: relative;
  transition: all 2s;
}
#content.menuShown {
  left: 20%;
}

然后使用javascript點擊處理程序

document.getElementById("showMenu").addEventListener("click", function(){
  document.getElementById("content").classList.toggle("menuShown");
});

document.getElementById("hideMenu").addEventListener("click", function(){
  document.getElementById("content").classList.toggle("menuShown");
});

這樣,每當您單擊菜單按鈕時,就可以切換.menuShown類的#content元素。 當content元素具有.menuShown.menuShown它會隨着過渡向右移20%。 這是jsFiddle

PS:顯示菜單時有一個水平滾動條,您可以使用body { overflow-x: hidden; }隱藏該滾動條body { overflow-x: hidden; } body { overflow-x: hidden; } 或者,您可以從中創建一個類,然后僅當菜單可見時才將其分配給主體,方法與分配.menuShown類相同。

我認為這個鏈接將幫助您JS FIDDLE

  document.getElementById("content").style.transition = "opacity 2s, left .2s"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM