一直使用jQuery的hover()函数来写鼠标经过事件,这几天发现其他同行直接使用CSS就搞定了。自己在线下也试了试:

当然也并不是没有前提条件,需要配合一些定位属性来使用(position/top/left)。代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
   <link rel="stylesheet" href="css/bootstrap.css" />
   <style>
   .outer {
      position: relative;
      width: 150px;
   }
   .link {
      padding: 0 18px;
      height: 50px;
      display: block;
      z-index: 2;
      background: #373d41;
      border-right: 1px solid #2a2f32;
      color: #fff;
      font-size: 14px;
      line-height: 50px;
      text-decoration: none;
   }
   .link:hover {
      background: #373d41
   }
   .appear-hide {
      position: absolute;
      top: 50px;
      left: 0;
      background: #ccc;
      color: #000;
      opacity: 0;
      visibility: hidden;
    }
   .outer:hover .appear-hide {
       opacity: 1;
       visibility: visible;
    }
    .test {margin-top: 20px}
   </style>
</head>
<body>
   <div class="outer">
      <a class="link" href="javascript:;">点击出现或隐藏</a>
      <div class="appear-hide">
         <p>CSS、CSS3</p>
         <p>HTML、HTML5</p>
         <p>JavaScript、ES6</p>
         <p>HTTP协议</p>
         <p>性能优化</p>
         <p>一门后端语言</p>
         <p>更好的方式写代码</p>
         <p>线下开发和代码发布</p>
      </div>
   </div>
   <div class="test">
      <p>CSS、CSS3</p>
      <p>HTML、HTML5</p>
      <p>JavaScript、ES6</p>
      <p>HTTP协议</p>
      <p>性能优化</p>
      <p>一门后端语言</p>
      <p>更好的方式写代码</p>
      <p>线下开发和代码发布</p>
   </div>
</body>
</html>
View Code

相关文章:

  • 2021-12-06
  • 2021-11-29
  • 2022-12-23
  • 2021-12-25
猜你喜欢
  • 2022-03-01
  • 2021-11-08
  • 2021-04-28
  • 2022-12-23
  • 2022-02-22
  • 2021-11-29
  • 2021-07-03
相关资源
相似解决方案