1 回答

TA贡献1856条经验 获得超17个赞
使用transform
css 属性img
transform: translate(-50%, -50%)
$(document.body).click(function(event) {
x = event.pageX;
y = event.pageY;
console.log(x + ", " + y);
console.log(('theImg').height);
$(".paysage").css({
'position': 'absolute',
'top': y,
'left': x
})
.prepend($('<img>', {
class: 'theImg',
src: 'https://www.30millionsdamis.fr/uploads/pics/conseils-erreurs-chat-1171.jpg'
}))
$(".theImg").prev().remove();
});
body {
width: 100vw;
height: 100vh
}
.theImg {
width: 20em;
height: auto;
transform: translate(-50%, -50%)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="paysage"></div>

TA贡献1816条经验 获得超6个赞
将此带有规则的选择器添加到您的 css 中:
.paysage {
transform: translate(-50%, -50%);
}
有必要吗?
$(document.body).click(function(event) {
x = event.pageX;
y = event.pageY;
console.log(x +", "+ y);
console.log(('theImg').height);
$(".paysage") .css({'position' : 'absolute', 'top': y, 'left' : x})
.prepend($('<img>',{ class:'theImg',
src:'https://www.30millionsdamis.fr/uploads/pics/conseils-erreurs-chat-1171.jpg'}))
$(".theImg").prev().remove();
});
body{
width: 100vw;
height: 100vh
}
.theImg{
width :20em;
height: auto;
}
.paysage {
transform: translate(-50%, -50%);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class = "paysage"> </div>
添加回答
举报