简体   繁体   English

如何使用vh html,css,js重新计算div内标记的位置

[英]How to recalculate position of marker inside div with vh html,css,js

I trying to create a map framework for some games and i have a problem with recalc position of marker.我尝试为某些游戏创建地图框架,但在重新计算标记位置时遇到问题。 Look url to test, with wheel you can resize div with image but the dot red not come back to own position.查看 url 进行测试,使用轮子可以使用图像调整 div 的大小,但红色点不会回到自己的位置。 Sorry but im new on this y trying to learn more about js and css.抱歉,我是新手,正在尝试了解有关 js 和 css 的更多信息。 Thanks谢谢

 $('.map-live').css('width', "928px"); $('.map-live').css('height', "928px"); $('.map-live').css('background-size', "100%"); $('.map-live').bind('mousewheel DOMMouseScroll', function(event) { var divSize = $('.map-live').css('width'); console.log(divSize); divSize = divSize.replace('px', '') divSize = parseInt(divSize); console.log("oldSize: " + divSize); var delta_px = event.originalEvent.wheelDelta > 0 ? (divSize + (divSize * 0.15)) : (divSize - (divSize * 0.15)); console.log("NewSize: " + delta_px); $(this).css('width', delta_px + "px"); $(this).css('height', delta_px + "px"); $(this).css('background-size', "100%"); UpdatePoints(); }); $(function() { $("#map-live").draggable(); }); document.getElementById('map-live').addEventListener('click', printPosition) function getPosition(e) { var rect = e.target.getBoundingClientRect(); var x = e.clientX - rect.left; var y = e.clientY - rect.top; return { x, y } } function printPosition(e) { var position = getPosition(e); console.log('X: ' + position.x + ' Y: ' + position.y); var divX = parseInt($('.map-live').css('width').replace('px', '')); var divY = parseInt($('.map-live').css('height').replace('px', '')); var vhX = (position.x / divX) * 100; var vhY = (position.y / divY) * 100; console.log('vhX: ' + vhX + ' vhY: ' + vhY); } function UpdatePoints() { $('.point').css('top', '2.477565353101834vh'); $('.point').css('left', '2.477565353101834vh'); $('.point').css('position', 'absolute'); }
 body { margin: 0; overflow: hidden; } .map-live { position: absolute; left: 10px; z-index: 9; background-image: url(https://i.ibb.co/d2y5G1y/map.jpg); width: 222px; height: 222px; transition: all 0.2s linear; } .point { position: absolute; left: 2.477565353101834vh; top: 2.477565353101834vh; width: 10px; height: 10px; background-color: red; border-radius: 50%; }
 <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <div class="map-live ui-widget-content" id="map-live"> <div class="point"></div> </div>

jsfiddle.net/f84mto52 jsfiddle.net/f84mto52

有人可以纠正我,但我相信您使用position: absolute是使<div class="point"></div>保持原位的原因。

Your UpdatePoints is setting always the same position in the div.您的 UpdatePoints 始终在 div 中设置相同的位置。 With 'vh' you are calculating and absolute position proportional to viewport, no to parent container.使用“vh”,您正在计算绝对位置与视口成比例,而不是与父容器成比例。

So, you are zooming the background image but the position (x, y) will be always be (x, y), positions are not zoomed.因此,您正在缩放背景图像,但位置 (x, y) 将始终为 (x, y),位置未缩放。 You need to recalculate which is the new position.您需要重新计算哪个是新位置。

So you need to calculate new position.所以你需要计算新的位置。

function UpdatePoints(){
  var divW = parseInt($('.map-live').css('width').replace('px',''));
  var divH = parseInt($('.map-live').css('height').replace('px',''));
  var topPosition = (2.477565353101834 / 928) * divH;
  var leftPosition = (2.477565353101834 / 928) * divW;
  $('.point').css('top', topPosition+'vh');
  $('.point').css('left', leftPosition+'vh');
  $('.point').css('position', 'absolute');
}

Also, instead using 'vh' I recommend to calculate the px position instead.另外,我建议改为使用 'vh' 来计算 px 位置。 I have added the already calculated delta_px parameter to UpdatePoints function:我已将已计算的delta_px参数添加到UpdatePoints函数中:

<style>
.point {
    position: absolute;
    left: 22.99180647678502px;
    top: 22.99180647678502px;
    width: 10px;
    height: 10px;
    background-color: red;
    border-radius: 50%;
}
</style>

<script>
function UpdatePoints(delta_px){
  var position = (delta_px/100)*2.477565353101834;
  $('.point').css('top', position+'px');
  $('.point').css('left', position+'px');
  $('.point').css('position', 'absolute');
}
</script>

Also, here we are calculating the top-left position of the .point element, not the position for the center.此外,这里我们计算的是.point元素的左上角位置,而不是中心位置。 As it is a circle, it work fine, but if you use any other shape the position translation should be calculated from its center.由于它是一个圆形,它工作正常,但如果您使用任何其他形状,则应从其中心计算位置平移。

I recommend to do some research about how to translate elements.我建议做一些关于如何翻译元素的研究。 You can start here:你可以从这里开始:

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

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