如何在菜单下拉列表中创建平滑过渡?

时间:2017-05-22 19:02:45

标签: javascript html css transitions

我正试图弄清楚如何在菜单转换中进行平滑过渡。我已经为时间写了一个函数来显示和消失onclick,但我不确定如何设置转换的样式。

这是我到目前为止所拥有的:

HTML:

<nav id="menu">
  <h2>Menu</h2>
    <ul>
      <li id="toggleTitle">Projects <i class="fa fa-angle-down" aria-hidden="true"></i></li>
      <li class="projects projectsClass"><a href="#">Project 1</a></li>
      <li class="projects projectsClass"><a href="#">Project 2</a></li>
      <li class="projects projectsClass"><a href="#">Project 3</a></li>
    </ul>
 </nav>

JS:

//toggleProjects
var toggleProjects = function(){

//function
var projectsList=document.getElementsByClassName('projects');
// if(projectsList[0].style.display=='none'){
if(!projectsList[0].classList.contains('projectsClass')){
//reappear
for(var i=0; i<projectsList.length; i++){
  // projectsList[i].style.display='';
  projectsList[i].classList.add('projectsClass');
}

} else {
//disappear
for(var i=0; i<projectsList.length; i++){
  // projectsList[i].style.display='none';
  projectsList[i].classList.remove('projectsClass');
}
}
};

//event listener for click
document.getElementById('toggleTitle').addEventListener('click', 
toggleProjects);

CSS:

.projectsClass {
    display: none;
}

.projects {
    margin-left: 30px;
}

#menu > .toggleTitle {
    padding: 1em 0;
    line-height: 1.5;
    border: 0;
    color: white;
}

jsfiddlelink:https://jsfiddle.net/j2fygbzb/

1 个答案:

答案 0 :(得分:0)

我让它做了类似于你期望(或者我认为)使用你的代码的东西,只有一些非常轻微的css修改。我向data: {'triple': DictClass(22, 'subClassOf', 44)} repr(data): {'triple': DictClass(22, 'subClassOf', 44)} data == eval(repr(data), {'DictClass':DictClass})?: True File saved! triple File loaded! 添加了一个过渡css属性,这是一个小提琴:

https://jsfiddle.net/j2fygbzb/9/

这是我改变的CSS:

.projects