jQuery - how to add mark to image

后端 未结 1 2001
遥遥无期
遥遥无期 2021-01-25 07:59

I am thinking how to add some marks to an image (I mean something like in google maps - marks for places). I have an image and if the user will clicked to this image, co I want

相关标签:
1条回答
  • 2021-01-25 08:53

    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/

    0 讨论(0)
提交回复
热议问题