【问题标题】:Cursor should be at the center of the image on .click() [duplicate]光标应位于 .click() 上的图像中心 [重复]
【发布时间】:2021-03-04 15:39:52
【问题描述】:

我正在做这个小例子,我试图将图像置于光标的中心,基本上,当您单击时,光标应该位于图像的中心。 由于我是初学者,我真的不知道如何实现这一目标。 也许是通过使用$(".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>

【问题讨论】:

    标签: javascript html jquery css


    【解决方案1】:

    img 上使用transform css 属性

    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>

    【讨论】:

    • 就是这么简单哈哈。感谢您的帮助以及重新格式化我的代码!
    【解决方案2】:

    将此带有规则的选择器添加到您的 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>

    【讨论】:

    • 嗨,谢尔盖,感谢您的帮助,非常简单!
    • 是的,有时简单的问题乍一看似乎很困难 :) 我很乐意提供帮助!
    猜你喜欢
    • 2021-12-08
    • 1970-01-01
    • 2018-07-19
    • 1970-01-01
    • 2016-10-29
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    相关资源
    最近更新 更多