2016-08-31 3 views
0

Как я могу добавить эффект перехода в меню с кнопками? Я попытался использовать document.getElementById ('content'). Style.transition = "left 1.5s"; но он не работает. Я приветствую какую-то помощь. Я новичок в JS.Как добавить эффект перехода в javascript для push-side menu?

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

ответ

0

С вашего подхода (который вы добавляете 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 элемента. А когда элемент контента имеет класс .menuShown, он смещается вправо на 20% с переходом. jsFiddle

PS: при отображении меню отображается горизонтальная полоса прокрутки, вы можете скрыть это с помощью body { overflow-x: hidden; }. Или вы можете создать класс из него и назначить его телу только тогда, когда меню видно так же, как присвоено класс .menuShown.

3

Использование animate.css

загрузить animate.css из https://daneden.github.io/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> 
0

Я думаю, что эта ссылка поможет вам JS FIDDLE

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

Использовать свойство транзакции в css для скрытого div

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

он будет показывать плавное движение для всех изменений (включая изменение цвета)

Смежные вопросы