jquery - 使用 Jquery 调整 Tumblr Post iframe 的大小

标签 jquery css iframe tumblr tumblr-themes

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

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 值设置高度。我需要它是一个相对高度。

最佳答案

我最近才自己解决这个问题,这需要一点数学知识。你需要获取他们当前的宽度(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,所以我不知道我的头顶需要哪些边距值,但高度变化就在这里,我想你可以弄清楚那里的 margin 值。如果没有,我可以帮助您多玩一些。

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

关于jquery - 使用 Jquery 调整 Tumblr Post iframe 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27177734/

相关文章:

jquery - 单击按钮恢复为内联样式 - jQuery

javascript - 使用 POST 将 Ajax 变量发送到 PHP

javascript - 这个CSS声明的结果?

CSS 底部无法正常工作

javascript - Iframe.readyState 不适用于 chrome

javascript - 动态创建的元素上的事件绑定(bind)?

javascript - 如何知道一个对象是可见的还是隐藏的?

html - iFrame 禁止一起滚动

javascript - 当 iframe src 更改时 jQuery 切换类

jquery - Google map 搜索框 APi,初始加载后搜索框不显示