Bootstrap 在jQuery中悬停时在图像上显示链接

5uzkadbs  于 2023-08-01  发布在  Bootstrap
关注(0)|答案(4)|浏览(122)

我正在使用Bootstrap 3和jQuery 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样式。如何在悬停时在图像中间显示链接?

xyhw6mcr

xyhw6mcr1#

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

个字符

vc6uscn9

vc6uscn92#

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

$(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>
l7mqbcuq

l7mqbcuq3#

你可以单独使用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

mrzz3bfm

mrzz3bfm4#

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);
            });
});

相关问题