jQuery 的 height() 和 width() 方法是否保持图像的比例?
Google 结果和 jQuery 文档表明 jQuery 的 height() 和 width() 方法没有努力保持元素的比例。
然而,当我运行代码时,似乎确实保持了比例性。
任何人都可以澄清事实吗?
这是我的代码:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js" type="text/javascript"></script>
<script src="jquery.gallery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('img').gallery();
});
</script>
</head>
<body>
<img src="img1.jpg" alt="" />
<img src="img2.jpg" alt="" />
<img src="img3.jpg" alt="" />
</body>
</html>
jquery.gallery.js
(function( $ ){
$.fn.gallery = function() {
return this.each(function() {
var e = $(this);
e.height(128);
});
};
})( jQuery );
Google results and the jQuery documentation indicate that jQuery's height() and width() methods make no effort to maintain an element's proportionality.
Yet when I run my code, it appears as if proportionality is indeed maintained.
Can anyone set the record straight?
Here's my code:
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.js" type="text/javascript"></script>
<script src="jquery.gallery.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('img').gallery();
});
</script>
</head>
<body>
<img src="img1.jpg" alt="" />
<img src="img2.jpg" alt="" />
<img src="img3.jpg" alt="" />
</body>
</html>
jquery.gallery.js
(function( $ ){
$.fn.gallery = function() {
return this.each(function() {
var e = $(this);
e.height(128);
});
};
})( jQuery );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jQuery 确实没有做任何事情来维持图像的比例。如果您仅设置图像的宽度或高度,它将自动保持其比例。
jQuery does indeed do nothing to maintain the proportions of the image. If you set only the width or only the height of an image, it will automatically maintain it's proportions.
如果仅设置图像的高度,则宽度会按比例调整,反之亦然。如果同时设置它们,则不会保持比例性。
If you set only the height of an image, the width is adjusted in proportion and vice versa. Proportionality is not maintained if you set both of them.