将加载 gif 添加到简单脚本中

发布于 2024-08-31 03:13:11 字数 1044 浏览 5 评论 0原文

我对 Javascript 真的很陌生,但我已经有了这个加载 url 内容的脚本,一切都工作正常。我使用按钮上的 onClick 方法调用 plannerSpin 函数,但是当这一切发生时,我将如何显示动画 gif?

var xmlHttp
function plannerSpin(str) {
    xmlHttp = GetXmlHttpObject()
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request")
        return
    }
    var url = "/recipes/planner/data"
    xmlHttp.onreadystatechange = stateChanged
    xmlHttp.open("GET", url, true)
    xmlHttp.send(null)
}
function stateChanged() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        document.getElementById("recipe_planner_container").innerHTML = xmlHttp.responseText
    }
}
function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

Im really really new to Javascript but Ive got this script that loads the contents of a url and everything works fine. I call the plannerSpin function with an onClick method on a button but how would I go about displaying an animated gif whilst all this is going on?

var xmlHttp
function plannerSpin(str) {
    xmlHttp = GetXmlHttpObject()
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request")
        return
    }
    var url = "/recipes/planner/data"
    xmlHttp.onreadystatechange = stateChanged
    xmlHttp.open("GET", url, true)
    xmlHttp.send(null)
}
function stateChanged() {
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        document.getElementById("recipe_planner_container").innerHTML = xmlHttp.responseText
    }
}
function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只怪假的太真实 2024-09-07 03:13:11

有很多方法...例如:您可以隐藏图像:

<div id='loading' style='display:none'><img src='img.gif'></div>

并在启动 AJAX 请求时立即显示它:

document.getElementById('loading').style.display = 'inline';

然后,一旦请求完成,您可以再次隐藏图像:

if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"){
    document.getElementById('loading').style.display = 'none';
    document.getElementById("recipe_planner_container").innerHTML = xmlHttp.responseText;
}   

或者,您可以使用 jQuery 、Prototype、Mootools 或任何您想要的 JS 库。

再见!

There are so many ways... for example: you can have the image hidden:

<div id='loading' style='display:none'><img src='img.gif'></div>

and show it as soon as you start the AJAX request:

document.getElementById('loading').style.display = 'inline';

Then, you hide the image again once the request has been completed:

if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"){
    document.getElementById('loading').style.display = 'none';
    document.getElementById("recipe_planner_container").innerHTML = xmlHttp.responseText;
}   

Or, you can use jQuery, Prototype, Mootools, or whatever JS library you want.

Bye!

一腔孤↑勇 2024-09-07 03:13:11

您可以在 plannerSpin 的开头添加加载 gif 并在 stateChanged 函数中删除。像这样的东西:

var img;
function plannerSpin(str) {
    img=document.createElement("img");
    img.src="image/path";
    //Here you can set some style for the image like an absolute position
    document.body.appendChild(img);
    ....
}


function stateChanged() {
    ...
    document.body.removeChild(img);
}

You can add the loading gif at the beginning of plannerSpin and remove in the stateChanged function. Somethig like:

var img;
function plannerSpin(str) {
    img=document.createElement("img");
    img.src="image/path";
    //Here you can set some style for the image like an absolute position
    document.body.appendChild(img);
    ....
}


function stateChanged() {
    ...
    document.body.removeChild(img);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文