为了账号安全,请及时绑定邮箱和手机立即绑定

光标应位于 .click() 上图像的中心

光标应位于 .click() 上图像的中心

慕丝7291255 2023-08-18 17:03:05
我正在举这个小例子,我试图将图像置于光标的中心,基本上,当您单击时,光标应该位于图像的中心。由于我是初学者,我真的不知道如何才能实现这一目标。也许是通过使用类似的东西$(".theImg").height,但我认为我做错了,因为它不起作用。也许我必须使用transform...$(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;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div class = "paysage"> </div>
查看完整描述

1 回答

?
慕慕森

TA贡献1856条经验 获得超17个赞

使用transformcss 属性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>


查看完整回答
反对 回复 2023-08-18
?
潇湘沐

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>


查看完整回答
反对 回复 2023-08-18
  • 1 回答
  • 0 关注
  • 70 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信