简体   繁体   English

如何将符号或文本放入单击的图像中 position

[英]How to put symbol or text into an image's clicked position

I am trying to implement something like this, a predefined symbol or text should be putted on a image where clicked.我正在尝试实现这样的东西,预定义的符号或文本应该放在点击的图像上。 Suppose there is an image and I had clicked on the top right corner of that image.假设有一张图片,我点击了该图片的右上角。 Now I want to show something on that clicked position.现在我想在点击的 position 上显示一些东西。 I can get the coordinate of the clicked position.我可以得到点击的 position 的坐标。 But I am not getting how I can put something on that place using this coordinate.但是我不知道如何使用这个坐标在那个地方放东西。 This is what I had tried.这是我尝试过的。

HTML code: HTML 代码:

<img src="result.png" alt="" id="image">

JS code: JS代码:

$('#image').click(function (e) { 
var posX = $(this).position().left,
    posY = $(this).position().top,
    positionX = e.pageX - posX,
    positionY = e.pageY - posY;
$("#image").html('<p id="clicked">Clicked here</p>');
});

You are trying to set the html of an image element that is clicked.您正在尝试设置单击的图像元素的 html。 You do nothing with the x and y position of the element.您对元素的 x 和 y position 什么都不做。

You need to append an element and set the position.您需要 append 一个元素并设置 position。

 $('#myDiv').on("click", "img", function (e) { var wrapper = $(this).parent(), position = wrapper.offset(), posX = position.left, posY = position.top, positionX = Math.floor(e.pageX - posX), positionY = Math.floor(e.pageY - posY), marker = $('<p class="clicked">Clicked here</p>'); marker.css({top: positionY + "px", left: positionX + "px"}) wrapper.append(marker); });
 #myDiv { position: relative; border: 1px solid green; } #myDiv p { position: absolute; color: rgb(0, 255, 255); mix-blend-mode: difference; margin: 0; padding: 0; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1>Example</h1> <div id="myDiv"> <img src="http://placekitten.com/g/400/300" alt=""> </div>

Consider an alternative solution.考虑一个替代解决方案。

 $(function() { $.fn.setMarker = function(e, o) { if (o == undefined) { o = $("<p>", { class: "clicked" }).html("Clicked Here"); } var self = $(this); var marker = o.appendTo(self.parent()).css({ top: (e.pageY - self.offset().top) + "px", left: (e.pageX - self.offset().left) + "px" }); } $('#myDiv-1').on("click", "img", function(e) { $(this).setMarker(e); }); });
 #myDiv-1 { position: relative; border: 1px solid green; } #myDiv-1 p { position: absolute; color: rgb(0, 255, 255); mix-blend-mode: difference; margin: 0; padding: 0; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1>Example</h1> <div id="myDiv-1"> <img src="http://placekitten.com/g/400/300" alt=""> </div>

This adds the function as a jQuery extension basically.这基本上添加了 function 作为 jQuery 扩展。 If you like, you can modify the function to pass in an jQuery Object or Element to add as the marker.如果您愿意,可以修改 function 以传入 jQuery Object 或 Element 以添加为标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM