user1990992
user1990992

Reputation: 97

display text over image using div tag

i want display text over image using div tag. i am getting the values from database and store into scripting array succesfully. but the values are not displaying. but i am using div tag insted of image text will be displayed. following is my code

 <script >
        var i=0;
        var st1=new Array();
        var st2=new Array();
        var st3=new Array();
       // var obj={"A","B","C","D"};
   $(document).ready(function(){
  $("#myimg").hover(function(){


  <c:forEach var="d"  items="${data}" >
        <c:set var="st1" value="${fn:substringBefore(d,'-')}" />            
        <c:set var="st2" value="${fn:substringAfter(d,'-')}" />



         st1.push("${st1}");             
         st2.push("${st2}");

    </c:forEach>
    <c:forEach var="d1"  items="${data1}" >
        <c:set var="st22" value="${d1}" />       
            st3.push("${st22}");

  </c:forEach>

  for(var i=0;i<6;i++)
      {

  var X=st1[i];      
  var Y=st2[i];
  var txt=st3[i];

        var test = $("<span class='test'></span>");

  test.html(txt);

  $("#myimg").append(test.offset({left:X,top:Y}));

   }

  }, function(){
    $('.test').remove();
  }
 );

 });

    </script>

    <div class="test">div-test </div>
    <img id="myimg" src="mirchi2.jpg" width="500" height="500">
    </body>
    </html>

Upvotes: 0

Views: 4233

Answers (2)

What have you tried
What have you tried

Reputation: 11148

Instead of appending the DIV - do something like this:

Edit Fully working example: http://jsfiddle.net/

<div class="image-container"> <!-- MAKE SURE THIS IS POSITION RELATIVE! -->
    <img id="image1" src="https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcTKWtn1KeBcz5Ydr3Hv6aYTCjiqbqFg2DfB422smPV21TS7kjVh" alt="some alt" />
    <div id="text-pop">This is some text</div>
</div>

CSS:

.image-container{
    position: relative;
}
#text-pop{
    display: none;
    position: absolute;
}

jQuery:

$(document).ready(function(){
    $("#image1").hover(function(){
        $("#text-pop").fadeIn(800);
        $("#text-pop").css({
            "position": "absolute",
            "top": "25%",
            "left": "20%",
            "background": "yellow",
            "padding": "30px"
        })
    }, function(){
        $("#text-pop").hide();
    }
    );
});

Upvotes: 1

user1467267
user1467267

Reputation:

Use <div> with display: inline-block; and background-image: url(image.png);.

Then within that <div> you can add new children, thus text. Like ahren said, <img /> (like <br /> and <hr />) is a singular tag and can't have children.

<div id="myimg" style="background-image: url(mirchi2.jpg); width: 500px; height: 500px;">
    <div>More content</div>
</div>

OR (cleaner)

CSS

#myimg {
    background-image: url(mirchi2.jpg);
    width: 500px;
    height: 500px;
}

HTML

<div id="myimg">
    <div>More content</div>
</div>

Upvotes: 0

Related Questions