如何在悬停时使图像变暗并添加文本?

时间:2016-01-22 17:22:31

标签: javascript html css

我尝试了多种方法,但似乎没有任何工作。

我最近偶然发现了这个http://codepen.io/VectorQuanity/pen/qEeJoK,但是当我尝试将其实现到我的代码中时,文本在悬停时不会弹出。

我的代码:



$(".pic").hover(function() {
    $(".info", this).css("display", "block");
}, function() {
    $(".info", this).css("display", "none");
});

$('.hover').mouseover(function() {
    $('.text').css("visibility","visible");
});

$('.hover').mouseout(function() {
    $('.text').css("visibility","hidden");
});

body {
    width: 1500px;
    text-align: center;
    font-family: arial;
    margin: 0 auto;
}

@import url(https://fonts.googleapis.com/css?family=Source+Code+Pro:300);

ul {
    display: flex;
    list-style-type: none;
    justify-content: space-around;
    align-items: center;
    border-bottom: 2px solid black;
    font-family: 'Source Code Pro';
}

.logo {
    font-family: 'Source Code Pro';
    font-size: 40px;
    padding: 10px 0;
}

nav {
    list-style-type: none;
}

.pic {
    position: relative;
    display: inline-block;
}

.pic:hover > .overlay {
    position: absolute;
    top: 0;
    width:100%;
    height:99.5%;
    left: 0;
    background-color:#000;
    opacity:0.5;
    z-index: 100;
    display: block;
}

.info {
    display: none;
    position: absolute;
    top: 100px;
    left: 0;
    text-align: center;
}

footer{
    background: black;
    color: white;
    padding: 40px 0;
    bottom: 0;
    width: 100%;
    z-index: -1;
    font-family: 'Source Code Pro';
}

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Gullible</title>
  <link rel="stylesheet" href="css/style.css" media="screen" title="no title" charset="utf-8">
  <script type="text/javascript" src="script.js"></script>
</head>
<body>

  <ul>
    <li><a href=""></a>Home</li>
    <li><a href=""></a>Shop</li>
    <li class="logo"><a href=""></a>Gullible</li>
    <li><a href=""></a>Visit</li>
    <li><a href="text">Contact</a></li>
  </ul>

<div class="pic">
  <img src="http://i.stack.imgur.com/cjj0l.jpg" class="hover">
  <div class="overlay"></div>
  <div class="info">
    <img src="http://i.stack.imgur.com/cjj0l.jpg">
    <p class="text">Wonder Women</p>
  </div>
</div>
<div class="pic">
  <img src="http://i.stack.imgur.com/fgwPb.jpg" class="hover">
  <div class="overlay"></div>
  <div class="info">
    <img src="http://i.stack.imgur.com/fgwPb.jpg">
    <p class="text">Batman</p>
  </div>
</div>
<div class="pic">
  <img src="http://i.stack.imgur.com/b8VTt.jpg" class="hover">
  <div class="overlay"></div>
  <div class="info">
    <img src="http://i.stack.imgur.com/b8VTt.jpg">
    <p class="text">Joker</p>
  </div>
</div>
<div class="pic">
  <img src="http://i.stack.imgur.com/rTZPO.jpg" class="hover">
  <div class="overlay"></div>
  <div class="info">
    <img src="http://i.stack.imgur.com/rTZPO.jpg">
    <p class="text">Bane</p>
  </div>
</div>
<div class="pic">
  <img src="http://i.stack.imgur.com/7aHn3.jpg" class="hover">
  <div class="overlay"></div>
  <div class="info">
    <img src="http://i.stack.imgur.com/7aHn3.jpg">
    <p class="text">Bane</p>
  </div>
</div>
<div class="pic">
  <img src="http://i.stack.imgur.com/JOzEN.jpg" class="hover">
  <div class="overlay"></div>
  <div class="info">
    <img src="http://i.stack.imgur.com/JOzEN.jpg">
    <p class="text">Bane</p>
  </div>
</div>


  <footer>
  <h2>Gullible</h2>
    <nav>
      <li>Home</li>
      <li>Shop</li>
      <li>Visit</li>
      <li>Contact</li>
    </nav>
  </footer>
</body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:4)

您的代码中有很多内容可以改进。例如,您根本不需要任何JavaScript来实现您正在寻找的东西。

查看下面的改进代码(解决了您的问题)!

@import url(https://fonts.googleapis.com/css?family=Source+Code+Pro:300);

body {
    width: 1500px;
    text-align: center;
    font-family: arial;
    margin: 0 auto;
}

header ul {
   display: flex;
   list-style-type: none;
   justify-content: space-around;
   align-items: center;
   border-bottom: 2px solid black;
   font-family: 'Source Code Pro';
}

.logo {
    font-family: 'Source Code Pro';
    font-size: 40px;
    padding: 10px 0;
}

nav ul {
    list-style-type: none;
}

.pic {
    position: relative;
    display: inline-block;
}

.info {
    display: none;
    color: #fff;
    left : 0;
    top : 45%;
    right : 0;
    text-align : center;
    position: absolute;
}

.pic:hover > .overlay {
    position: absolute;
    top: 0;
    width:100%;
    height:99.5%;
    left: 0;
    background-color:#000;
    opacity:0.5;
    display: block;
}

.pic:hover .info {
    display: block;
}

footer{
    background: black;
    color: white;
    padding: 40px 0;
    bottom: 0;
    width: 100%;
    z-index: -1;
    font-family: 'Source Code Pro';
}
<header>
  <nav>
    <ul>
      <li><a href=""></a>Home</li>
      <li><a href=""></a>Shop</li>
      <li class="logo"><a href=""></a>Gullible</li>
      <li><a href=""></a>Visit</li>
      <li><a href="text">Contact</a></li>
    </ul>
  </nav>
</header>
<div class="pic">
  <img src="http://i.stack.imgur.com/cjj0l.jpg">
  <div class="overlay"></div>
  <div class="info">
    <p class="text">Wonder Women</p>
  </div>
</div>
<div class="pic">
  <img src="http://i.stack.imgur.com/fgwPb.jpg">
  <div class="overlay"></div>
  <div class="info">
    <p class="text">Batman</p>
  </div>
</div>
<div class="pic">
  <img src="http://i.stack.imgur.com/b8VTt.jpg">
  <div class="overlay"></div>
  <div class="info">
    <p class="text">Joker</p>
  </div>
</div>
<div class="pic">
  <img src="http://i.stack.imgur.com/rTZPO.jpg">
  <div class="overlay"></div>
  <div class="info">
    <p class="text">Bane</p>
  </div>
</div>
<div class="pic">
  <img src="http://i.stack.imgur.com/7aHn3.jpg">
  <div class="overlay"></div>
  <div class="info">
    <p class="text">Bane</p>
  </div>
</div>
<div class="pic">
  <img src="http://i.stack.imgur.com/JOzEN.jpg">
  <div class="overlay"></div>
  <div class="info">
    <p class="text">Bane</p>
  </div>
</div>
<footer>
  <h2>Gullible</h2>
  <nav>
    <ul>
      <li>Home</li>
      <li>Shop</li>
      <li>Visit</li>
      <li>Contact</li>
    </ul>
  </nav>
</footer>