Hover image - display div over it

前端 未结 5 538
误落风尘
误落风尘 2021-02-03 13:53

On hover I want a link apear at the top-right of the image. Just like on your profile picture on Facebook, where it says \"Change Picture\".

I have tried to to get it wo

相关标签:
5条回答
  • 2021-02-03 13:55

    This is the way I done it: CSS:

    .imgHover {
        display: inline;
        position: relative;
    }
    .imgHover .hover {
        display: none;
        position: absolute;
        right:0;
        z-index: 2;
    }
    

    HTML:

    <div class="imgHover">
    <div class="hover"><a href="htpp://google.com">Edit</a></div>
    <img src="http://img14.imageshack.us/img14/9698/29588166.jpg" alt="">
    </div>
    

    JQuery:

    $(function() {
        $(".imgHover").hover(
            function() {
                $(this).children("img").fadeTo(200, 0.85).end().children(".hover").show();
            },
            function() {
                $(this).children("img").fadeTo(200, 1).end().children(".hover").hide();
            });
    });
    

    Test this on jsfiddle.

    0 讨论(0)
  • 2021-02-03 13:59

    If the imgHover is made to match its contents, then it should just have position:relative and the hover class have position:absolute and top:0;right:0

    Example at http://www.jsfiddle.net/FvBqA/

    0 讨论(0)
  • 2021-02-03 14:06

    Try

    <!doctype html>
    <html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Test</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
        <style>
        .imgHover {
            display: inline;
            position: relative;
        }
        .imgHover .hover {
            display: none;
            position: absolute;
            right: 5px;
            top: 5px;
            z-index: 2;
        }
        </style>
    </head>

    <body> <script> $(function() { $(".imgHover").hover( function() { $(this).children("img").fadeTo(200, 0.25).end().children(".hover").show(); }, function() { $(this).children("img").fadeTo(200, 1).end().children(".hover").hide(); }); }); </script>

    <div class="imgHover"><div class="hover"><a href="htpp://google.com">Edit</a></div><img src="http://img14.imageshack.us/img14/9698/29588166.jpg" alt=""></div>

    </body> </html>

    0 讨论(0)
  • 2021-02-03 14:08

    make your image a background for div with relative positioning and add inner div to it with the overlay content style it with display:none

    then imgdiv:hover innerdiv{display:block} will do the trick

    0 讨论(0)
  • 2021-02-03 14:10

    You need to absolute position the link in a container that has position different than the normal flow. In this example I use relative for that on the container. Try this:

    HTML

    <div class="imgHover">
      <div class="imgContainer">
        <a href="http://google.com">Edit</a>
        <img src="http://img14.imageshack.us/img14/9698/29588166.jpg" alt="" />
      </div>
    </div>
    

    CSS

    .imgHover { float: left; }
    .imgContainer { position: relative; }
    .imgContainer a { display: block; position: absolute; top: 0; right: 0; background: Green;}
    
    0 讨论(0)
提交回复
热议问题