【问题标题】:Show Link on image at hovering in jquery在 jquery 中悬停时在图像上显示链接
【发布时间】:2015-12-21 16:11:12
【问题描述】:

我正在使用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 样式。如何在悬停时在图像中间显示链接?非常需要这个帮助!谢谢。

【问题讨论】:

  • 你想在哪里显示链接?
  • 你只能用css来做,还是想用jQuery来做?
  • 任何东西都适合我。但我建议使用 jquery,因为我的设计是响应式的,并且图像位置会根据设备屏幕尺寸而变化。

标签: javascript jquery html css twitter-bootstrap


【解决方案1】:

希望,如果你想在没有 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>

【讨论】:

  • 这个差不多了。它只是没有响应。链接位置根据设备大小而变化。你有没有机会让它响应所有设备都支持?
  • 您需要调整 parent-div 的宽度,使其具有响应性,您甚至可以使用媒体查询对其进行管理。
【解决方案2】:

你应该试试这个。这正是你想要的。

$(function () {
        $(".img-profile").hover(
            function () {
                $(this).fadeTo(200, 0.45);
                $(this).closest("li").find("a").show();
            },
            function () {
                $(this).fadeTo(200, 1);
              $(this).closest("li").find("a").hide();
            });
})
.img-profile {
    margin-top: 10px;
    width: 200px;
    height: 250px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<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="https://i.stack.imgur.com/TwJmm.gif?s=328&g=1" alt="profile_pic" /><a href="https://www.google.com" style="display:none">Link that you want</a>
            </li>
        </ul>
    </div>
</div>

【讨论】:

【解决方案3】:

您可以单独使用 css 实现所有这些。

html

<ul class="nav" id="side-menu">
    <li>
        <img class="img-responsive img-rounded img-profile" src="myimage.png" alt="profile_pic" />
        <p class="text">change profile picture</p>
    </li>
</ul>

css

.img-profile {
    margin-top: 10px;
    width: 200px;
    height: 250px;
    opacity: 1.0;
    filter:alpha(opacity=100);
}
.text {
    display: none;
}
img-profile:hover {
    opacity: 0.45;
    filter:alpha(opacity=45);
}
img-profile:hover + .text {
    display: block;
}

检查这个fiddle

【讨论】:

    【解决方案4】:

    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="http://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Colossoma_macropomum_01.jpg/800px-Colossoma_macropomum_01.jpg" alt="profile_pic" />
                    <a href="" id="update">Update Image</a>
                </li>
            </ul>
        </div>
    </div>
    

    CSS

    .img-profile {
        margin-top: 10px;
        width: 200px;
        height: 250px;
    }
    #side-menu li a{
      display:none;
    }
    #side-menu li:hover a#update{
      display:block;
    }
    

    JS

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-28
      • 1970-01-01
      • 2015-05-11
      • 2013-08-27
      • 1970-01-01
      • 2012-04-14
      • 2012-06-01
      • 1970-01-01
      相关资源
      最近更新 更多