【问题标题】:transform: scale - dealing with remaining 'margin' of initial sizetransform: scale - 处理初始大小的剩余“边距”
【发布时间】:2018-03-27 09:57:59
【问题描述】:

我使用 iframe 元素作为混合视频和图像内容的预览窗口。缩小的 iframe 效果很好,因为它允许我们的客户查看接近其在电视屏幕上显示的图像。

我想在 iframe 下方添加一些按钮,但按钮显示在 iframe 下方,符合 iframe 的未缩放大小。

处理原始边距空间的最佳方法是什么?

HTML:

<iframe id="iframe"  width="1920" height="1080" src="https://jsfiddle.net/" frameBorder="1">  </iframe>
<button id="previewSkip">Skip</button>

CSS:

#iframe {
  -webkit-transform:scale(0.295);
  -moz-transform: scale(0.295);            
  transform-origin: left top;
}

小提琴:

#iframe {
  -webkit-transform: scale(0.295);
  -moz-transform: scale(0.295);
  transform-origin: left top;
}
<iframe id="iframe" width="1920" height="1080" src="https://youtube.com/" frameBorder="1">  </iframe>
<button id="previewSkip">Skip</button>

【问题讨论】:

    标签: html css scale


    【解决方案1】:

    基于相同的因素和&lt;iframe&gt; 的尺寸,使用包装器来限制文档流中的占用空间:

    #iframe {
      transform: scale(0.295);
      transform-origin: left top;
    }
    
    scale-factor {
      width: calc(1920px * 0.295);
      height: calc(1080px * 0.295);
      overflow: hidden;
      display: block;
      border: 1px solid red; /* not needed, obviously */
    }
    <scale-factor>
      <iframe id="iframe" width="1920" height="1080" src="https://jsfiddle.net/" frameBorder="1">  </iframe>
    </scale-factor>
    <button id="previewSkip">Skip</button>

    这是一个示例,在原版中,从标记中读取属性并对 &lt;scale-factor&gt; 内的任何 &lt;iframe&gt; 应用正确的缩放 + 修剪,只要后者具有 data-scale 属性:

    /* forEach polyfill */
    if (window.NodeList && !NodeList.prototype.forEach) {
        NodeList.prototype.forEach = function (callback, thisArg) {
            thisArg = thisArg || window;
            for (var i = 0; i < this.length; i++) {
                callback.call(thisArg, this[i], i, this);
            }
        };
    }
    
    window.onload = handleScaleFactors;
    
    function handleScaleFactors() {
      const scaleFactor = document.querySelectorAll('scale-factor');
      scaleFactor.forEach(function(e,i) {
        const iframe = e.querySelector('iframe');
        if (iframe) {
          const w = iframe.getAttribute('width') ? 
            parseInt(iframe.getAttribute('width')) : 
            iframe.clientWidth,
          h = iframe.getAttribute('height') ? 
            parseInt(iframe.getAttribute('height')) : 
            iframe.clientHeight,
          s = e.getAttribute('data-scale') ? 
            parseFloat(e.getAttribute('data-scale')) : 
            1;
          iframe.style.transform = 'scale(' + s + ')';
          e.style.width = (w * s) + 'px';
          e.style.height = (h * s) + 'px';
          e.style.opacity = 1;
        }
      });
    }
    scale-factor {
      display: block;
      overflow: hidden;
      opacity: 0;
      transition: opacity .3s linear;
    }
    scale-factor iframe {
      transform-origin: 0 0;
    }
    <scale-factor data-scale="0.295">
      <iframe width="1920" height="1080" src="https://jsfiddle.net/" frameBorder="0">  </iframe>
    </scale-factor>
    <button id="previewSkip">Skip</button>
    
    <scale-factor data-scale="0.708">
      <iframe width="800" height="600" src="https://jsfiddle.net/" frameBorder="0">  </iframe>
    </scale-factor>
    <button id="previewSkip">Skip</button>

    它更简洁的 jQuery 等价物是:

    $(window).on('load', handleScaleFactors)
    
    function handleScaleFactors() {
      $('scale-factor').each(function(i, e) {
        const iframe = $('iframe', e);
        if (iframe.is('iframe')) {
          const w = iframe.attr('width') ? parseInt(iframe.attr('width')) : iframe.width(),
            h = iframe.attr('height') ? parseInt(iframe.attr('height')) : iframe.height(),
            scale = $(e).data('scale') || 0;
          iframe.css({
            transform: 'scale(' + scale + ')'
          });
          $(e).css({
            width: (w * scale) + 'px',
            height: (h * scale) + 'px',
            opacity: 1
          })
        }
      });
    }
    

    【讨论】:

    • 谢谢!这让我感到困惑,而且速度非常快。我以前从未见过使用 calc。
    • 这里不需要calc()。你可以计算它并在那里设置宽度。 calc() 当您想通过 JavaScript 根据当前宽度和高度进行操作时派上用场,当两者(和比例因子)都是动态的时,基于...也许是当前屏幕宽度?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 2017-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多