如何显示前景图像然后隐藏在悬停?

时间:2016-05-12 23:05:10

标签: html css ruby-on-rails ruby image

如何在前景中显示图像,然后当用户将鼠标悬停在该图像上时,图像将会消失,用户可以看到它背后的内容。

我尝试了display: nonevisibility: none的组合,但之后图片不会悬停或完全显示

查看

<div class="social-stuff">
  <div class="like-button">
    <%= link_to like_challenge_path(:id => @challenge.id), class: "btn", method: :post do %>
      <span class='glyphicon glyphicon-thumbs-up'></span> Like
    <% end %>
  </div>

  <div class="text-background">
    <%= render "comments/comments" %>
    <%= render "comments/form" %>
  </div>
</div>

CSS

.social-stuff {
  background: url('goal-setting.jpeg');
  &:hover {
   ??
  }
}

1 个答案:

答案 0 :(得分:3)

我已经为您创建了CodePen。我已经用两种方式完成了它。使用CSS在悬停时更改图像的不透明度,并使用jQuery为图像的不透明度设置动画。下面是代码。它目前使用jQuery设置工作,但取消对css悬停部分的评论,并评论jQuery,CSS版本将起作用。希望这会有所帮助。

HTML

<div class="image-container">
  <img id="image-foreground" src="https://cdn2.vox-cdn.com/thumbor/CA1U4gMJ2xG2s-tSfPTKJdiVFxw=/0x0:1687x1125/1280x854/cdn0.vox-cdn.com/uploads/chorus_image/image/48681987/titan-aerospace-drone-google.0.0.jpeg" alt="" />
  <div class="text-wrapper">
    <p id="text-body">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Magnam delectus, optio qui, magni suscipit error aperiam nemo veritatis quod sunt at perferendis accusantium quaerat. Repudiandae quaerat inventore voluptatum, sit eos.</p>
  </div>
</div>

CSS

.image-container {
  background: black;
  position: relative;
  height: auto;
  width: 30vw;

  #image-foreground {
    position: absolute;
    height: inherit;
    width: inherit;
    z-index: 2;
    opacity: 1;

    // &:hover {
    //   opacity: 0;
    // }
  }
  .text-wrapper {
    position: absolute;
    width: 20vw;
    color: white;
    top: 15vh;
    left: 5vw;
    z-index: 1;
    background: black;
  }
}

的jQuery

$(document).ready(function(){
  $('#image-foreground').hover(function(){
    $(this).animate({
      opacity: 0
    },500, function(){
      $(this).animate({
        opacity: 1
      },500)
    });
  });
})