在JS / Jquery中飞行图像大小的变化

时间:2015-08-28 22:39:17

标签: javascript jquery html css image

所以我正在创建一个图像显示脚本,它的作用就像一个叠加层。我基本上想要将图像大小输入到animate方法中,以便我可以保持正确的图像比例。我正在使用href,使用preventDefault,并将URL提供给我在HTML中的img src。我是否应该每次动态创建一个新的img,然后使用动态创建的图像来读取图像的大小,然后在加载方法内动画?不知道如何继续。代码如下:

public class RssParseHandler extends DefaultHandler {
//Parsed items
private List<RssItem> rssItems;
private RssItem currentItem;
private boolean parsingTitle;
private boolean parsingLink;
private boolean parsing_id;
private boolean parsingDescription;

public RssParseHandler() {
    rssItems = new ArrayList<RssItem>();
}

public List<RssItem> getItems() {
    return rssItems;
}

//Creates empty RssItem object during the process of an item start tag
//Indicators are set to true when particular tag is being processed
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

    if ("item".equals(qName)) {
        currentItem = new RssItem();

    } else if ("title".equals(qName)) {
        parsingTitle = true;


    } else if ("link".equals(qName)) {
        parsingLink = true;


    } else if ("_id".equals(qName)) {
        parsing_id = true;


    } else if ("description".equals(qName)) {
        parsingDescription = true;

    }
}

//Current RssItem is added to the list following process of end tag
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {

    if ("item".equals(qName)) {
        rssItems.add(currentItem);
        currentItem = null;

    } else if ("title".equals(qName)) {
        parsingTitle = false;

    } else if ("link".equals(qName)) {
        parsingLink = false;

    } else if ("_id".equals(qName)) {
        parsing_id = false;

    } else if ("description".equals(qName)) {
        parsingDescription = false;
    }
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {

    if (parsingTitle) {
        if (currentItem != null)
            currentItem.setTitle(new String(ch, start, length));

    } else if (parsingLink) {
        if (currentItem != null) {
            currentItem.setLink(new String(ch, start, length));
            parsingLink = false;
        }

    } else if (parsing_id) {
        if (currentItem != null) {
            currentItem.set_id(new String(ch, start, length));
            parsing_id = false;
        }

    } else if (parsingDescription) {
        if (currentItem != null) {
            currentItem.setDescription(new String(ch, start, length));
            parsingDescription = false;
        }

    }
}}//rssHandlerClass

1 个答案:

答案 0 :(得分:0)

我解决了这个问题:

我动态添加了图像,然后必须检查它的尺寸 -

var imag = $('<img style="max-width: 90%;">');

                    $(imag).attr('src',link);
                    $(imag).load(function() {
                        wdth = this.width;
                        hght = this.height;
                    });

我之前尝试这个问题的主要问题是$(this)和$(imag)不会在load函数中返回正确的尺寸。我不得不使用非jquery这个关键字。奇怪的行为。