简体   繁体   English

无法在响应式网站上将高度缩放到宽度

[英]Trouble getting height to scale to width on responsive website

Im trying to have a div re-size so that the height stays the same width as the height. 我正在尝试调整div的大小,以使高度保持与高度相同的宽度。 The divs are constantly changing size, div的大小不断变化,

The width changes size because of percentages, however the height is changed using Jquery. 宽度因百分比而改变大小,但是使用Jquery改变高度。

Html HTML

<div id="inner_container">
        <div id="Sbox1" class="select_tile" > </div>
        <div id="Sbox2" class="select_tile" > </div>
        <div id="Sbox3" class="select_tile" > </div>
        <div id="Sbox4" class="select_tile" > </div>
        <div id="Sbox5" class="select_tile" > </div>
        <div id="Sbox6" class="select_tile" > </div>
        <div id="Sbox7" class="select_tile" > </div>
        <div id="Sbox8" class="select_tile" > </div>
<div class="clr"> </div>

Css CSS

#inner_container 
{    width:98%; min-width:50%; max-width:1600px; 
     margin:auto; padding:1% 1%; text-align:center; background:;}

.select_tile 
{    width:23%; min-width:50px; min-height:50px;  
     background:green; margin:1%; float:left;}

Jquery jQuery的

   $(window).resize(function() {
      var ccW = $('.select_tile').width();
      $('.select_tile').css("height", ccW);
   });

This works fine for when the browser window is re-sized but the #inner_content div re-sizes when a button is pressed, so the height of the div.select_tile stays the same as it was before the button press but the width changes. 当重新调整浏览器窗口的大小但按按钮时#inner_content div的大小重新调整时,此方法效果很好,因此div.select_tile的高度与按下按钮之前的高度相同,但宽度发生了变化。

I was wondering if there was an event that waits for a change in the width or for something to change. 我想知道是否有一个事件在等待宽度的变化或某些变化。

basically I want the width and height at 1:1 ratio to stay the same but scale to the size of the browser window on any change on either property. 基本上,我希望宽度和高度以1:1的比例保持不变,但在任一属性上进行任何更改时,都应缩放到浏览器窗口的大小。

Any assistance on this would be much appreciated, Thanks 任何帮助,将不胜感激,谢谢

edit: added html 编辑:添加了html

您在这里有一个解决方案,可以使用setTimeOut()函数来确定如何确定一个div更改其高度或任何css属性 ,但是我会让按钮触发更改...

you could create your own event :) 您可以创建自己的事件:)

$(function() {

  // moved this out of the event so it doesn't
  // do a DOM traversal every 300ms!!! :)
  var $tiles = $('.select_tile');

  // this is your own custom function, which can be triggered
  // as you see below
  $(window).on("resizeBoxes" , function(e,$selector) {

     //set your box resize code here.
     var ccW = $selector.width();
     $selector.css("height", ccW);

  });

  // using 300 ms as it's not too short to kill a device,
  // and not too long to show noticable lag.
  // importantly though you want the resize event to
  // be succinct and easy on cpu.

  var resizeTimer = setInterval( function() {
    $(window).trigger("resizeBoxes",$tiles);
  }, 300);


  $(window).on("resize", function(e) { $(window).trigger("resizeBoxes",$tiles); });
  $("#button").on("click", function(e) { $(window).trigger("resizeBoxes",$tiles); });
  // this next one might be best for you :)
  $(window).on("mousemove", function(e) { $(window).trigger("resizeBoxes",$tiles); });

});

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

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