【问题标题】:Resizing Tumblr Post iframes with Jquery使用 Jquery 调整 Tumblr Post iframe 的大小
【发布时间】:2015-01-26 11:14:11
【问题描述】:

我正在尝试使用 jquery 修复 tumblr 自定义主题/布局。现在,它正在调用 700px 宽的 iframe。理想情况下,我希望 iframe 宽 540px,高度可变。 (高度取决于网格中垂直图像的数量,但图像宽度保持不变。

window.onload = function() {    
$('iframe.photoset').contents().find('.photoset_row').attr("style", "max-width:540px; margin-bottom: -4px; margin-left: 4px; overflow:visible; margin-top:4px ");
$('iframe.photoset').contents().find('.photoset_row').find('img').attr("style", "max-width:540px; height:auto; overflow:visible; margin-left: -6px;     ");
$('iframe.photoset').contents().find('.photoset_row_2').find('img').attr("style", "max-width:268px; height: auto; margin-right: 0px; max-height: auto; overflow:visible; margin-left: -6px; ");
$('iframe.photoset').contents().find('.photoset_row_3').find('img').attr("style", "max-width:177px; overflow:visible;margin-right: 0px; margin-left: -6px; ");    

基本上这会适当地调整宽度,但不会按比例缩小高度。

因为 iframe 的高度会根据 iframe 中的图像数量而有所不同,所以我无法像使用宽度那样使用绝对 px 值设置高度。我需要它是一个相对高度。

【问题讨论】:

    标签: jquery css iframe tumblr tumblr-themes


    【解决方案1】:

    我自己最近才弄清楚这一点,而这需要一点数学知识。您需要获取它们当前的宽度(tumblr 提供了它们所有的 iframe css 宽度)并将其与您希望它的宽度进行比较以找到比率,然后使用该比率来更改高度。像这样的

    ratio = new width / old width
    
    width = old width * ratio
    height = old height * ratio
    

    所以试试这个:

    window.onload = function() {
    
        //define function for resizing to minimize repeated code
        var resize(element, newwidth) = function () {
            //find elements width and height
            var width = parseFloat(element.css("width"));
            var height = parseFloat(element.css("height"));
    
            //find ratio between length
            var ratio = 540 / width;
    
            //find new length            
            var width = width * ratio;
            var height = height * ratio;
    
            //change lengths                
            element.css("max-width", width + "px");
            element.css("max-height", height + "px");
        };
    
        //photoset
        $('iframe.photoset').contents().find('.photoset_row').each(function() {
            resize($(this), 540);
        }
    
        //images in row 1
        $('iframe.photoset').contents().find('.photoset_row').find('img').each(function() {
            resize($(this), 540);
        }
    
        //images in row 2
        $('iframe.photoset').contents().find('.photoset_row_2').find('img').each(function() {
            resize($(this), 268);
        }
    
        //images in row 3
        $('iframe.photoset').contents().find('.photoset_row_3').find('img').each(function() {
            resize($(this), 177);
        }
    

    我对在脑海中阅读 CSS 有点害羞,所以我不知道你需要哪些边距值在我的头顶,但高度变化就在这里,我想你可以弄清楚从那里的边距值。如果没有,我可以帮你多玩一点。

    我还要感谢您 :) 您在调整照片集大小方面的知识对我非常有用,这正是我在发现您的问题时所寻找的!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-04
      • 2012-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多