Leave dot onclick on image

I would like to leave a dot on my grid image based on where I click. I have a general concept, but unfortunately my point stays a little higher than I hit. How can I customize this?

https://jsfiddle.net/dr0emvkr/

<!DOCTYPE html>
<html>

<head>
    <meta charset=utf-8 />
    <style>
        #imageholder:hover {
            cursor: crosshair;
        }
    </style>

    <style>
        article,
        aside,
        figure,
        footer,
        header,
        hgroup,
        menu,
        nav,
        section {
            display: block;
        }

        #imageholder div {
            display: none;
            background-color: black;
            position: absolute;
        }

        #imageholder {
            position: relative;
            display: inline-block;
            overflow: hidden;
        }

        #vertical {
            width: 2.5px;
            height: 100%;
        }

        #horizontal {
            width: 100%;
            height: 2.5px;
        }
    </style>
</head>

<body>

    <h1>Some Text</h1>
    <h2>Some other text</h2>
    <div id="imageholder">
        <div id="horizontal"></div>
        <div id="vertical"></div>
        <img src="http://i.imgur.com/dRUn4ip.png">
    </div>

    <script class="jsbin" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>


    </div>
    <script type="text/javascript">
        $('#imageholder img').on('click', null, [$('#horizontal'), $('#vertical')], function(e) {
            e.data[1].css('left', e.offsetX == undefined ? e.originalEvent.layerX : e.offsetX);
            e.data[0].css('top', e.offsetY == undefined ? e.originalEvent.layerY : e.offsetY);

            $('#imageholder').click(function(event) {
                var hor = event.offsetX + 4.15,
                    ver = event.offsetY + 4;

                $(".marker").remove();
                $("body").append(
                    $('<div class="marker" style="border-radius: 25px;"></div>').css({
                        position: 'absolute',
                        top: ver + 'px',
                        left: hor + 'px',
                        width: '10px',
                        height: '10px',
                        background: '#5b5e5f'

                    })
                );
            });

            e.data[0].show();
            e.data[1].show();

            $(document).ready(function() {
                $('#imageholder').mouseover(function() {
                    $(".marker").css("box-shadow", "0 0 0 3px rgba(0, 0, 0, 0.5)");
                });
                $('#imageholder').mouseout(function() {
                    $(".marker").css("box-shadow", "none");
                });
            });

        });
    </script>
</body>

</html>

      

+3


source to share


2 answers


Instead, .marker

you can add .marker

to #imageholder

. And use transform: translate()

to place vertical / horizontal / .marker lines at dead center where you clicked.



$('#imageholder img').on('click', null, [$('#horizontal'), $('#vertical')], function(e) {
  e.data[1].css('left', e.offsetX == undefined ? e.originalEvent.layerX : e.offsetX);
  e.data[0].css('top', e.offsetY == undefined ? e.originalEvent.layerY : e.offsetY);

  $('#imageholder').click(function(event) {
    var hor = event.offsetX,
      ver = event.offsetY;

    $(".marker").remove();
    $("#imageholder").append(
      $('<div class="marker" style="border-radius: 25px;"></div>').css({
        position: 'absolute',
        top: ver + 'px',
        left: hor + 'px',
        width: '10px',
        height: '10px',
        background: '#5b5e5f',
        display: 'block',
        transform: 'translate(-50%,-50%)'

      })
    );
  });

  e.data[0].show();
  e.data[1].show();

  $(document).ready(function() {
    $('#imageholder').mouseover(function() {
      $(".marker").css("box-shadow", "0 0 0 3px rgba(0, 0, 0, 0.5)");
    });
    $('#imageholder').mouseout(function() {
      $(".marker").css("box-shadow", "none");
    });
  });

});
      

#imageholder:hover {
  cursor: crosshair;
}

article,
aside,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
  display: block;
}

#imageholder div {
  display: none;
  background-color: black;
  position: absolute;
}

#imageholder {
  position: relative;
  display: inline-block;
  overflow: hidden;
}

#vertical {
  width: 2.5px;
  height: 100%;
  transform: translateX(-50%)
}

#horizontal {
  width: 100%;
  height: 2.5px;
  transform: translateY(-50%)
}

* {
box-sizing:border-box;
}
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>

  <h1>Some Text</h1>
  <h2>Some other text</h2>
  <div id="imageholder">
    <div id="horizontal"></div>
    <div id="vertical"></div>
    <img src="http://i.imgur.com/dRUn4ip.png">
  </div>

  <script class="jsbin" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</body>
      

Run code


+2


source


The problem is you are using the x and y coordinates from the grid, but then positioning relative to the page.

The fix is ​​to position relative to the grid by adding a marker to the element imagegrid

.

I gave #horizontal

and the #vertical

general class, so that I can make style rules #imagegrid div

more specific.

Then I changed $('body').append

to $('#imagegrid').append

and I ended up subtracting 8 pixels in both dimensions to center the point.



Updated JavaScript:

$("#imageholder").append(
  $('<div class="marker" style="border-radius: 25px;"></div>').css({
    position: 'absolute',
    top: ver - 8 + 'px',
    left: hor - 8 + 'px',
    width: '10px',
    height: '10px',
    background: '#5b5e5f'

  })
);

      

Full Fiddle (including the previously mentioned HTML / CSS changes): https://jsfiddle.net/dr0emvkr/2/ .

+4


source







All Articles