简体   繁体   中英

Javascript resize image after a few seconds

How would I resize an html image with Javascript? I would like the "Pack" image to become larger after 7 seconds. 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:

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".

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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