欢迎光临
我们一直在努力

如何利用html+css设置菜单/导航栏缓慢下拉效果

如何利用html+css设置菜单栏缓慢下拉效果?解决方法和相关代码分享如下:

方法一:使用transition过度

对forum_box开启绝对定位(absolute),让里面的li从其父元素中脱离出去,不然会把之后的内容往右挤,并且设置overflow:hidden,设置高度为0,鼠标移入后再设置相应的高度即可:

.code .forum_box{
  /* 开启绝对定位 */
  position: absolute;
  overflow: hidden;
  height: 0;
  transition-duration: 0.5s;
}

 

HTML代码:

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>菜单栏缓慢下拉-新起点博客</title>
</head>
<body>
  <ul>
    <li><a href="https://www.henenseo.com/">新起点博客</a></li>
    <li><a href="https://www.henenseo.com/">新起点博客</a>
      <ul>
        <li><a href="https://www.henenseo.com/">新起点博客</a></li>
        <li><a href="https://www.henenseo.com/">新起点博客</a></li>
        <li><a href="https://www.henenseo.com/">新起点博客</a></li>
      </ul>
    </li>
    <li><a href="https://www.henenseo.com/">新起点博客</a></li>
  </ul>
</body>
</html>

 

css样式代码如下:

a{
  display: block;
  text-decoration: none;
  color: #333;
}
.code{
  width: 390px;
  height: 50px;
  line-height: 50px;
  background-color:#bfa;
  margin: 5px auto;
}
.code li{
  float: left;
  width: 130px;
  height: 50px;
  background-color: #bfa;
  text-align: center;
  margin: 0 auto;
  font-size: 20px;
}
.code > li:last-child{
  margin-right: 0;
}
.code > li:hover{
  background-color: #f8f192;
}
.forum{
  position: relative;
  margin: auto 90px;
}
.code .forum_box{
  /* 开启绝对定位 */
  position: absolute;
  overflow: hidden;
  height: 0;
  transition-duration: 0.5s;
}
.forum:hover .forum_box{
  /* 鼠标移入释放高度 */
  height: 150px;
}

 

最后发现transition是不支持display属性的,也就是说,不能用display:none隐藏菜单栏

方法二:animation动画

首先创建css动画:

@keyframes frames{
  from{
    height: 0px;
  }
  to{
    height: 150px;
  }
}

然后设置display:none隐藏菜单样式,把它绑定到forum-1选择器中,用animation绑定动画名字,设置持续时间

.forum_box{
  position: absolute;
  display: none;
  overflow: hidden;
  /* 绑定动画名字并且设置持续时间 */
  animation-name: frames;
  animation-duration: 0.5s;
}

当鼠标移入时,设置display属性为block即可:

.forum:hover .forum_box{
  display: block;
}

需要注意的一点是,这样写的结果会出现一个问题:当鼠标移入不久后二级菜单栏会自动收回,为了避免这种问题,我们可以在forum-1选择器内部添加一行代码即可:

.forum_box{
	animation-fill-mode: forwards;
}

 

注:方法二其余的代码与方法一雷同,自行测试!

 

赞(0) 打赏
未经允许不得转载:新起点博客 » 如何利用html+css设置菜单/导航栏缓慢下拉效果


关注公众号『新起点软件管家』

获取最新网络资源及破解软件!
带你玩转各样软件...

评论 抢沙发

评论前必须登录!

 

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏