javascript - 在 jquery 中悬停时在图像上显示链接

标签 javascript jquery html css twitter-bootstrap

我正在使用 Bootstrap 3jQuery 1.9.1制作一个个人资料图片显示在 <li> 中的网站标签。当我将鼠标悬停在图像上时,我想显示“更新图像”的链接。到目前为止,我已经设法在悬停时淡化图像,但无法在悬停在图像上时显示链接或文本。这是我的代码,

JQUERY

$(function () {
    $(".img-profile").hover(
        function () {
            $(this).fadeTo(200, 0.45);
        },
        function () {
            $(this).fadeTo(200, 1);
        });
});

CSS

.img-profile {
    margin-top: 10px;
    width: 200px;
    height: 250px;
}

HTML

<div class="navbar-default sidebar" role="navigation">
    <div class="sidebar-nav navbar-collapse">
        <ul class="nav" id="side-menu">
            <li>
                <img class="img-responsive img-rounded img-profile" src="myimage.png" alt="profile_pic" />
            </li>
        </ul>
    </div>
</div>

我搜索了其他来源,但没有一个有效,因为它会影响我自己的 CSS 样式。如何在悬停时在图像中间显示链接?非常需要这个帮助!谢谢。

最佳答案

希望,如果你想在没有 Jquery 的情况下完成它 - 即使你想使用 jquery 来管理它,它也可以完成..这不是什么大问题

.img-profile {
  margin-top: 10px;
  width: 200px;
  /* if image is bigger it will adjust according to parent width */
  height: 250px;
  display: inline-block;
}
.img-wrap {
  display: inline-block;
  position: relative;
}
.img-wrap:hover .img-profile {
  opacity: 0.5;
}
.img-wrap .hover-div {
  display: none;
  position: absolute;
  text-align: center;
  top: 50%;
  left: 0;
  right: 0;
  margin-top: -10px;
  z-index: 10;
  /* width of image container */
}
.img-wrap:hover .hover-div {
  display: inline-block;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="navbar-default sidebar" role="navigation">
  <div class="sidebar-nav navbar-collapse">
    <ul class="nav" id="side-menu">
      <li class="img-wrap">
        <img class="img-responsive img-rounded img-profile center-block" src="https://www.google.co.in/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" alt="profile_pic" />
        <div class="hover-div"><a href="#">Update Image</a>
        </div>
      </li>
    </ul>
  </div>
</div>

关于javascript - 在 jquery 中悬停时在图像上显示链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34390859/

相关文章:

javascript - 在声明链中使用 , (逗号)代替正确的变量定义(如 const、var 或 let)是一种好的 JS 实践吗?

javascript - 在悬停时显示下拉菜单并替换 CSS 类

Javascript 自定义选项卡添加到代码 : hide content if tab is empty

javascript - 为什么浏览器会在空的 body 元素中插入 2 个换行符?

Javascript 淡入淡出 div 与 YouTube 视频

javascript - 更改高度适用于我的两个输入字段,但不适用于其他输入字段。为什么?

javascript - 检索 jquery-ui 对话框的 div

javascript - 选择表行并插入到表单 Javascript

java - 为什么我的 CSS 文件在我的元素中不起作用

javascript - HTML 链接到 anchor 而不更改 url