简体   繁体   English

在运行时之前按比例调整我无权访问的图像大小

[英]Resizing an image proportionally that I don't have access to before runtime

I have been searching all day for a solution to this and have read many articles on image resizing and DOM node searching and Wordpress functions and any other possible way for this to be done. 我一整天都在寻找解决方案,并阅读了很多关于图像大小调整和DOM节点搜索以及Wordpress功能的文章以及任何其他可能的方法。

Heres the scenario... 继承人的情景......

I am creating a custom theme for Wordpress for a client. 我正在为客户创建Wordpress的自定义主题。 This is a newspaper site and requires the functionality that it auto-resizes the pictures proportionally to fit within a certain box on the page. 这是一个报纸网站,需要它按比例自动调整图片大小以适应页面上某个框的功能。 Normally I would just set width:SomeWidth in CSS but I don't have any way to ID the img. 通常我会设置width:SomeWidth CSS中的width:SomeWidth但我没有任何方法来识别img。

My index page is a framework and the wordpress engine loads the content. 我的索引页面是一个框架,wordpress引擎加载内容。 This means that until it is displayed to the user I don't have any control of the attributes on the image. 这意味着在向用户显示之前,我无法控制图像上的属性。 (Yes we could manually size every picture, but my client doesn't want to have to do that, and she also wants it to be the original size on the single page. (是的,我们可以手动调整每张图片的大小,但我的客户不希望这样做,并且她还希望它是单页上的原始大小。

Here is one of boxes: 这是一个盒子:

<div class="box-center">
        <h1>Music 101</h1>
        <div class="box-inner-center">
            <ul>
                <?php
                    global $post;
                    $args = array('numberposts'=>1,'category'=>get_cat_ID('Music 101'));
                    $myposts=get_posts($args);
                    foreach($myposts as $post) : setup_postdata($post); ?>
                        <li><?php the_content(); ?></li>
                    <?php endforeach; ?>
            </ul>
            <a class="more" href="<?php echo esc_url(get_category_link(get_cat_ID('Music 101'))); ?>">More Music 101 >></a>
        </div>
    </div>

After the page loads this is what comes out: 页面加载后,这是出来的:

<div class="box-center">
    <h1>Music 101</h1>
        <div class="box-inner-center">
            <ul>
                <li>
                    <p>
                        <a href="http://***?attachment_id=150" rel="attachment wp-att-150">
                            <img class="alignnone  wp-image-150" alt="Venice Symphony Orchestra" src="http://***/Venice-Symphony-Orchestra.jpg" width="960" height="640">
                        </a>
                    </p>
                    <p><span style="color: #000000;"><b><i>Some sample text.</i></b></span></p>
                    <p><span style="color: #000000;"><b>By Someone</b></span></p>
                    <p><span style="color: #000000;">Some quote and more content</span></p>
                    <p><span style="color: #000000;"><a href="http://***?p=141#more-141" class="more-link">(more…)</a></span></p>
                </li>
            </ul>
            <a class="more" href="http://***/?cat=20">More Music 101 &gt;&gt;</a>
        </div>
    </div>

I have tried using document.getElementById("box-center").childNodes and document.getElementByTagName("img") and then for loop through the results and set the properties there, but the above lines return no results somehow. 我已经尝试使用document.getElementById("box-center").childNodesdocument.getElementByTagName("img")然后for通过循环的结果和设置的属性有,但上面的纹路莫名其妙返回任何结果。

I would prefer not to use JQuery or any additional libraries if possible. 如果可能的话,我宁愿不使用JQuery或任何其他库。

Thank you for your time. 感谢您的时间。

This assumes that you want to resize the first occurrence of <img> inside element with class 'box-inner-center' 这假设您要使用类'box-inner-center'调整元素内第一次出现的<img>

var thebox = document.getElementsByClassName('box-inner-center')[0];
var theimg = thebox.getElementsByTagName('img')[0];
theimg.style.width = '100px';

I don't see any reason why you can't use css. 我没有看到任何你不能使用CSS的原因。 I would suggest something like this: 我会建议这样的事情:

.box-center .box-inner-center img {
    width:150px; /* or whatever */
    height:auto; /* overrides the set HTML height*/
}

This will override the width and height attributes on the image itself and you can even be more specific if necessary: .box-center .box-inner-center ul p > a img or whatever you want. 这将覆盖图像本身的宽度和高度属性,如有必要,您甚至可以更具体: .box-center .box-inner-center ul p > a img或您想要的任何内容。

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

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