tags:

views:

53

answers:

1

So I am trying to add an icon to an image, once someone has 'tagged' a portion of the image - i.e. they have submitted the form.

How do I do that in jQuery?

Just to be clear...imagine you are on flickr and you see an image. You want to add an annotation, you click on the image where you want, and it shows you a form field. You enter the information you want to, then after you have done so, there is a small image left on top of the picture that shows where you have left a comment.

Any ideas?

Edit - See Code:

    <script type="text/javascript">

        $(function() {
                var tag_box = $("<div>").appendTo("body").css({
                    "width": "40px",
                    "height":"40px",
                    "border":"4px solid #000000",
                    "position":"absolute",
                    "display":"none", 
                    "padding":"15px"
                    });

            var comment_box = $("<form action='#'><input id='comment' type='text' name='comment' placeholder='Add comment'></form>").appendTo(tag_box).css({"position":"absolute"});

            var comment_icon = $("<img src='images/asterisk_yellow.png' width='16' height='16' alt='Comment'>");

            $("#image-wrapper").live("click", function(e) {
                tag_box.css({
                    "left": e.pageX - 40,
                    "top": e.pageY - 40, 
                    "display": "block"
                    })
                .after(comment_box.css({
                    "left": e.pageX - 65,
                    "top": e.pageY + 40
                    }));
                 return false;  
                });

                $("form").submit(function() {
                    var params = $(this).serialize();

                    $("form")[0].reset();

                    return false;
                });                 
            });     
</script>

<body>
<div align="center">  

    <img src="images/image1.gif" width="760" height="182" alt="image1" id="image-wrapper">

</div>
</body>

What I am trying to do is when the user clicks an area (they see a square box that shows them where they are tagging). They then enter the comment, and the comment is stored in a variable (for now), but then I would like to mark that area (of the image) with an image...in this case it is an 'asterisk'. So I imagine the solution would have to know the coordinates or something where the comment was added. I was looking into offset(), but I am relatively new to jQuery and am having a hard time putting the pieces together.

+1  A: 

A simple, transparent PNG would work just fine. Say your markup for containing the list of images looks like this:

<ul id="img_list">
    <li>
        <img src="image.jpg" alt="Image Title" />
        <img class="icon" src="annotation.png" alt="You've done XYZ to this." />
    </li>
    <!-- List continues... -->
</ul>

You could style it like this:

#img_list li {
    display: block;
    width: 50px;
    height: 50px;
    position: relative;
}

#img_list .icon {
    width: 10px;
    height: 10px;
    position: absolute;
    z-index: 2;
    top: 0;
    left: 0;
}

If you wanted to add it dynamically in jQuery, you could do something like this in your form's submit success callback:

$icon = $("<img class='icon' src='annotation.png' alt='You've done XYZ to this.' />");
$("#img_list li.active").append($icon); // assuming you set class active on the item that's being edited

Edit: The exact functionality/styling/markup depends on what your site looks like. Since the user is browsing through images, I'd imagine you'd want to use AJAX to submit the form so they don't lose their place. If you've got some code I could look at I could be more precise.

Also, if there's a lot of different icons, you should use CSS sprites and add a 2nd class to the icon for determining which sprite to use.


Edit 2: This has more information than I can give. You'd just need to modify some of the code here an' there to get your desired effect, but you can add the transparent asterisk very easily as part of the SubmitTag function, you'll just need to pull the target position (I think he uses the variables targetX and targetY for that) and set left and right on the tag icon.

Zack
This is awesome Zack. I have added some code snippets in the original question to provide some more context.Look forward to seeing what you come up with :)
marcamillion
I've added a link that can give you more information on how to do this. They said it better than I could.
Zack
PERFECT. I could KISS you for this...but I won't :)
marcamillion
Don't get my hopes up! ;) Also, don't forget to accept it if it works
Zack
In all the excitement I forgot to accept.This more than works. Believe me...this is EPIC!!!
marcamillion
Heh, credit should really go to that blogger, all I did was find it--either way, glad I could help :)
Zack
I know. I am adding a comment on his blog as we speak.
marcamillion