jQuery - how to add mark to image

社会主义新天地 提交于 2019-12-02 05:47:07

There are several ways of doing this, for instance you can simple set your image as a background of some div (use also position: relative; on that div) and then onclick you can create/move/show other image, setting position: absolute; to it and positioning it with top and left.

Example CSS:

#container {
    background: green;
    width: 1000px;
    height: 500px;
    position: relative;
}
#container img {
    position: absolute;   
}

Example HTML:

<div id="container"></div>

Example JS (using jQuery):

$(document).ready(function() {
    $("#container").click(function(e) {
        e.preventDefault();
        var x = e.pageX - this.offsetLeft;
        var y = e.pageY - this.offsetTop;
        var img = $('<img>');
        img.css('top', y);
        img.css('left', x);
        img.attr('src', 'http://img34.imageshack.us/img34/3337/imglp.png');
        img.appendTo('#container');
    })
});

Working example http://jsfiddle.net/uKkRh/1/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!