简体   繁体   English

图像高度现在通过jQuery更改

[英]Image Height now changing with jQuery

I'm trying to change the height and width of an image when clicked on a certain button. 我正在尝试单击某个按钮时更改图像的高度和宽度。 So far I got the basics, but I think I don't have them in the right order. 到目前为止,我已经掌握了基础知识,但是我认为我没有正确的基础知识。 The image and the other text are moving, but the image size isn't changing. 图像和其他文本正在移动,但是图像大小没有变化。 Right now it mostly the white space around the image changing. 目前,图像周围的空白大部分在变化。

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

  //image
  function shrinkImage() {
    $('#dog_image').height(200);
    $('#dog_image').width(307);
  }

  </script>
</head>

<div id="dog_image">
<img src="dog.jpg" alt="turle">
</div>

<div id="image_buttons">
  <button type="button" id="shrink" onclick="shrinkImage();">Shrink Image</button>
</div> 

Here's a nice way to do that : 这是一个很好的方法:

 $('.pictureclass').click(function (e) { if ($(e.target).hasClass('expanded')) { $(".pictureclass").removeClass('expanded').stop().animate({ width: 300, height: 300 }, 300); } else { $(".pictureclass").addClass('expanded').stop().animate({ width: $(e.target).attr("width"), height: $(e.target).attr("height") }, 200); } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <img id="mrbean" class="pictureclass" src="http://2.bp.blogspot.com/-C6KY8tsc8Fw/T-SVFnncxjI/AAAAAAAAANw/FMiNzA8Zecw/s640/mr.bean.jpg" width="50" height="50" /> 

This isn't you want? 这不是你想要的吗?

 img{ width: 100%; height: 100%; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script> //image function shrinkImage() { $('#nickel_image').height(200); $('#nickel_image').width(307); } function enlargePic() { $('#nickel_image').height(450); $('#nickel_image').width(580); } function restorePic() { $('#nickel_image').height(300); $('#nickel_image').width(460); } </script> </head> <div id="nickel_image"> <center><img src="https://www.w3schools.com/w3css/img_lights.jpg" alt="turle"></center> </div> <div id="image_buttons"> <p>Image:</p> <button type="button" id="shrink" onclick="shrinkImage();">Shrink Image</button> <button type="button" id="enlarge" onclick="enlargePic();">Enlarge Pic</button> <button type="button" id="restore" onclick="restorePic();">Restore Pic</button> </div> 

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

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