鼠标悬停对图像的影响

时间:2016-05-11 10:17:57

标签: jquery html css twitter-bootstrap

before mouse hover on the image

after mouse hover on the image

最初图像仅可见,按钮隐藏在图像后面。在图像鼠标悬停时,图像会向上移动,按钮将位于图像的底部。 但如何在图像中隐藏按钮?

鼠标悬停之前,只有在鼠标悬停在图像上并且可见按钮后才能看到图像。我试过这样但没有得到,这里的片段作为参考(http://www.olleep.com/)我试图做同样的事情



$(document).ready(function() {
    $('#textbox').mouseenter(function() {
        $('#textbox').animate({
        'marginTop' : "-=30px" //moves up
        });
    });
    $('#textbox').mouseleave(function() {
        $('#textbox').animate({
        'marginTop' : "+=30px" //moves up
        });
    });
});

.container{

       margin:50px;
        }
    #textbox{
        position:relative;
            width:300px;
           
    }
    #text{
      position:relative;
        overflow:hidden;
         z-index:1;
        background-color:#282727;
        text-align:center;
        padding:10px 10px 10px 10px;
    }

<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div id="textbox">
<img src="http://i.imgur.com/9hr2mlL.png" width="300px">
<div id="text">
<button class="btn btn-danger btn-md">MORE INFO</button>
<button class="btn btn-danger btn-md">BOOK NOW</button></div>
</div>
</div>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:4)

您可以单独使用CSS,还可以添加悬停效果,然后使用transform:translateY在屏幕上切换这些效果:

div {
  display: inline-block;
}
.wrapper {
  overflow: hidden;
  position:relative;
}
.buttons{
  position:absolute;
  bottom:0;
  width:100%;background:dimgray;
  font-size:0;height:40px;
    transition:all 0.4s;
    transform:translateY(100%);
  }
.buttons button{
  width:40%;margin:5%;font-size:15px;

  }
.wrapper img{  transition:all 0.4s;}

.wrapper:hover .buttons{
  transform:translateY(0);
  }
.wrapper:hover img{
    transform:translateY(-35px);
  }
<div class="wrapper">
  <img src="http://i.imgur.com/9hr2mlL.png" />
  <div class="buttons">
    <button>Button 1</button>
    <button>Button 2</button>
  </div>
</div>

答案 1 :(得分:0)

你可以使用CSS来做到这一点。试试:hover,请查看以下示例。

&#13;
&#13;
.container {
  margin: 50px;
}
#textbox {
  position: relative;
  width: 300px;
}
#text {
  position: relative;
  overflow: hidden;
  z-index: 1;
  background-color: #282727;
  text-align: center;
  padding: 10px 10px 10px 10px;
  display: none;
}
#textbox:hover #text {
  display: block;
}
&#13;
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
  <div id="textbox">
    <img src="http://i.imgur.com/9hr2mlL.png" width="300px">
    <div id="text">
      <button class="btn btn-danger btn-md">MORE INFO</button>
      <button class="btn btn-danger btn-md">BOOK NOW</button>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

添加$('#text').show();$('#text').hide();

&#13;
&#13;
$(document).ready(function() {
    $('#textbox').mouseenter(function() {
        $('#text').show();
        $('#textbox').animate({
        'marginTop' : "-=30px" //moves up
        });
    });
    $('#textbox').mouseleave(function() {
        $('#text').hide();
        $('#textbox').animate({
        'marginTop' : "+=30px" //moves up
        });
    });
});
&#13;
.container{

       margin:50px;
        }
    #textbox{
        position:relative;
            width:300px;
           
    }
    #text{
      position:relative;
        overflow:hidden;
         z-index:1;
        background-color:#282727;
        text-align:center;
        padding:10px 10px 10px 10px;
    }
&#13;
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div id="textbox">
<img src="http://i.imgur.com/9hr2mlL.png" width="300px">
<div id="text">
<button class="btn btn-danger btn-md">MORE INFO</button>
<button class="btn btn-danger btn-md">BOOK NOW</button></div>
</div>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您可以使用简单的.hide().show()

来完成此操作

检查一下 http://codepen.io/vigneshvdm/pen/PNLJKL