用javascript将图像居中

Centring an image in javascript

本文关键字:图像 javascript      更新时间:2023-09-26

我是JavaScript新手。正如你在下面看到的,一个gif文件在重定向之前显示,但问题是loading.gif显示在上传框div之外。我如何将它放在div内并居中(自动边距(。

我尝试了很多方法,但都没能成功!

这里是JS函数:

//Disabling autoDiscover
Dropzone.autoDiscover = false;
$(function() {
//Dropzone class
var myDropzone = new Dropzone(".dropzone");
myDropzone.on("queuecomplete", function() {
    // Display "Please Wait" gif
    var myImage = new Image();
    var ImgSrc = "./loading.gif";
    myImage.setAttribute("src", ImgSrc);
    myImage.style.margin = "0 auto";
    document.body.appendChild(myImage);
    // Wait 3 seconds and then redirect to the link
    window.setTimeout(function() {
    location.href = 'http://www.i-0s.ir/site';
    }, 3000);
});
});

这是HTML部分:

<div class="upload-box">
    <p>Please upload your files</p>
    <div class="image_upload_div">
        <form action="upload.php" class="dropzone"></form>
    </div>
</dive>

试试这个:

$(function() {
    //Dropzone class
    var myDropzone = new Dropzone(".dropzone");
    myDropzone.on("queuecomplete", function() {
        // Display "Please Wait" gif
        $('.upload-box').append('<div style="text-align:center;"><img src="./loading.gif"/></div>')
        // Wait 3 seconds and then redirect to the link
        window.setTimeout(function() {
            location.href = 'http://www.i-0s.ir/site';
        }, 3000);
    });
});