Skip to content

Instantly share code, notes, and snippets.

@Frelseren
Created April 29, 2016 08:30
Show Gist options
  • Save Frelseren/ef2fd735f6ff0de36057281368ae5c52 to your computer and use it in GitHub Desktop.
Save Frelseren/ef2fd735f6ff0de36057281368ae5c52 to your computer and use it in GitHub Desktop.
img-tagging
<html>
<head></head>
<body>
<div class="img-wrapper">
<img src="https://i.ytimg.com/vi/GMkDBh1NnP0/maxresdefault.jpg" />
</div>
</body>
</html>
$('.img-tag').hide();
$('img').click(function (e) {
const OFFSET = $(this).offset();
const NUM = $('.img-tag').length + 1;
const IMG_WIDTH = $(this).width();
const LEFT_OFFSET = e.pageX - OFFSET.left + 'px';
const TOP_OFFSET = e.pageY - OFFSET.top + 'px';
console.log(LEFT_OFFSET);
console.log(TOP_OFFSET);
$(this).after(function() {
return '<span class="img-tag" style="left:'
+ LEFT_OFFSET
+ ';top:'
+ TOP_OFFSET
+ ';">'
+ NUM
+ '</span>';
});
});
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
img
max-width: 100%
cursor: crosshair
.img-wrapper
position: relative
.img-tag
position: absolute
background: red
color: white
padding: 3px 9px
border-radius: 100px
transform: translate(-50%)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment