简体   繁体   English

高度等于宽度,响应于调整javascript

[英]height equal width responsive on resize javascript

In this script I want to put my height as big as my width. 在此脚本中,我想将高度设为宽度。 This works if you shrink your browser and then refresh your browser. 如果缩小浏览器然后刷新浏览器,则此方法有效。 But now I want him to do it when I resize the browser without having to refresh. 但是现在我希望他在调整浏览器大小时无需刷新即可。 Have tried to resize window but it didn't worked, perhaps i used it wrong. 试图调整窗口大小,但没有成功,也许我用错了。

<script type="text/javascript">
    var height = $('.circle img').width();
    $('.circle img').css({ 'height': height + 'px' });
</script>

Attach a resize event handler with jQuery resize() 使用jQuery resize()附加一个调整大小事件处理程序

$(window).resize(function(){
   var height = $('.circle img').width();
   $('.circle img').css({'height':height+'px'});
});

Have you tried using vw ( viewport width ) instead of px to determine your width instead? 您是否尝试使用vw视口宽度 )而不是px来确定宽度? If it's possible for the design of your site, you can use pure CSS instead of Javascript to keep the aspect ratio consistent. 如果可以进行网站设计,则可以使用纯CSS而不是Javascript来保持宽高比一致。

It goes a little something like this: 它有点像这样:

.circle img {
   width: 10vw;
   height: 10vw;
}

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

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