简体   繁体   English

几秒钟后,Javascript调整图像大小

[英]Javascript resize image after a few seconds

How would I resize an html image with Javascript? 如何使用Javascript调整HTML图片的大小? I would like the "Pack" image to become larger after 7 seconds. 我希望“打包”图像在7秒后变大。 Here is my code: 这是我的代码:

<html>
    <head>
        <link rel="stylesheet" href="style.css" />
    </head>

    <body>
        <!-- Pack -->
        <img id="pack" src="http://cloud.attackofthefanboy.com/wp-content/uploads/2013/07/fifa14pack.png">

        <!-- Sparkles-->
    <div id="sparkles">

        <img id="sparkle1" src="http://www.clker.com/cliparts/w/L/F/v/7/8/sparkle-md.png">

        <img id="sparkle2" src="http://www.clker.com/cliparts/w/L/F/v/7/8/sparkle-md.png">

        <img id="sparkle3" src="http://www.clker.com/cliparts/w/L/F/v/7/8/sparkle-md.png">

        <img id="sparkle4" src="http://www.clker.com/cliparts/w/L/F/v/7/8/sparkle-md.png">

        <img id="sparkle5" src="http://www.clker.com/cliparts/w/L/F/v/7/8/sparkle-md.png">

        <img id="sparkle6" src="http://www.clker.com/cliparts/w/L/F/v/7/8/sparkle-md.png">

        <img id="sparkle7" src="http://www.clker.com/cliparts/w/L/F/v/7/8/sparkle-md.png">

        <img id="sparkle8" src="http://www.clker.com/cliparts/w/L/F/v/7/8/sparkle-md.png">
        </div>

You can register a timeout function to change the elements styles after 7 seconds with the following: 您可以注册超时功能,以在7秒后使用以下命令更改元素样式:

setTimeout(function(){
    var pack = document.getElementById("pack");
    pack.style.width = "100%;";
}, 7000);

You will probably want to customize this to your use case. 您可能需要针对您的用例进行自定义。

Try: 尝试:

setTimeout(function(){document.getElementById("pack").style.width="soandsopx";document.getElementById("pack").style.height="soandsopx";},7000);

Where "soandsopx" is the number of pixels you want it to have, plus "px". 其中“ soandsopx”是您想要的像素数加上“ px”。

This will do the trick: 这将达到目的:

window.setTimeout(function () {
    var pack_image = document.getElementById('pack');
    if(pack_image && pack_image.style) {
        pack_image.style.height = '700px';
        pack_image.style.width = '700px';
    }
},7000);

jsFiddle of this code jsFiddle的这段代码

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

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