Javascript - 全局和全局之间的混淆局部变数

发布于 2024-12-13 23:59:56 字数 3059 浏览 2 评论 0原文

EDIT2:这是控制台日志。图像正在转换为画布(HTML5),我需要以某种方式将其“返回”到全局变量。建议?

<img id="mainIllustration" alt="main illustration" src="Img/Model01.png">
Illust...o_03.js (line 24)
<canvas id="mainIllustration" class="" width="278" height="659" style="" title="" tabindex="-1">
Illust...o_03.js (line 32)
<img id="mainIllustration" alt="main illustration" src="Img/Model01.png">
Illust...o_03.js (line 24)
<canvas id="mainIllustration" class="" width="278" height="659" style="" title="" tabindex="-1">
Illust...o_03.js (line 32)
<img id="mainIllustration" alt="main illustration" src="Img/Model01.png">
Illust...o_03.js (line 24)
<canvas id="mainIllustration" class="" width="278" height="659" style="" title="" tabindex="-1">

编辑1:我开始怀疑问题是否出在 Pixastic 上,事实上它正在操作图像,我需要将其返回到全局图像变量(eMainIllustration)。我将如何实现这一目标?

原始信息:

所有,

我正在使用 pixastic 库,并且对一个看似非常简单的事情(全局/局部变量)感到非常困惑。

我有下面两组代码,一组是全局定义变量的(最终是我想要的),一组是全局定义的变量。另一个变量是本地定义的。

下面的第一个代码工作正常,变量(eMainIllustration)是本地定义的,并且函数工作良好。

    var eDisplayTableSlideState = false;
    function displayTableSlideIn()
    {
// eMainIllustration is locally defined
        var eMainIllustration = document.getElementById("mainIllustration");
        if (eDisplayTableSlideState === false)
        {
            $("#displayTable").animate ({top: "-=33px", left: "+=66px", width: "-=198px", height: "-=48px"}, 750, "swing",function()
            {
                Pixastic.process (eMainIllustration,"blurfast", {amount: 0.1});
            return (eDisplayTableSlideState = true);
        } else
        {
            $("#displayTable").animate ({top: "+=33px", left: "-=66px", width: "+=198px", height: "+=48px"}, 500, "swing",function()
            {
                Pixastic.revert(eMainIllustration);
            });
            return (eDisplayTableSlideState = false);
        }
    }

但是,当我全局设置变量 (eMainIllustration) 时,函数的第一个 if 语句起作用,但函数“else”部分中的 pixastic.revert 代码不起作用。为什么?

// eMainIllustration is locally defined
var eMainIllustration = document.getElementById("mainIllustration");
    var eDisplayTableSlideState = false;
    function displayTableSlideIn()
    {
        if (eDisplayTableSlideState === false)
        {
            $("#displayTable").animate ({top: "-=33px", left: "+=66px", width: "-=198px", height: "-=48px"}, 750, "swing",function()
            {
                Pixastic.process (eMainIllustration,"blurfast", {amount: 0.1});
            return (eDisplayTableSlideState = true);
        } else
        {
            $("#displayTable").animate ({top: "+=33px", left: "-=66px", width: "+=198px", height: "+=48px"}, 500, "swing",function()
            {
                Pixastic.revert(eMainIllustration);
            });
            return (eDisplayTableSlideState = false);
        }
    }

我想知道这是否是因为我没有通过函数前半部分 (if) 的 return 返回更改后的状态 (eMainIllustration)。这是正确的吗?我将如何实现这一目标?

最好的,

EDIT2: Here is the console log. The image is being converted to canvas (HTML5), I need to 'return' this to the global variable somehow. Suggestions?

<img id="mainIllustration" alt="main illustration" src="Img/Model01.png">
Illust...o_03.js (line 24)
<canvas id="mainIllustration" class="" width="278" height="659" style="" title="" tabindex="-1">
Illust...o_03.js (line 32)
<img id="mainIllustration" alt="main illustration" src="Img/Model01.png">
Illust...o_03.js (line 24)
<canvas id="mainIllustration" class="" width="278" height="659" style="" title="" tabindex="-1">
Illust...o_03.js (line 32)
<img id="mainIllustration" alt="main illustration" src="Img/Model01.png">
Illust...o_03.js (line 24)
<canvas id="mainIllustration" class="" width="278" height="659" style="" title="" tabindex="-1">

EDIT 1: I am starting to wonder whether the issue is with Pixastic, the fact that its manipulating the image, which I need to return back to the global image variable (eMainIllustration). How would I achieve this?

ORIGINAL MESSAGE :

All,

Im using pixastic library and am getting very confused by a seemingly very simple thing, global/ local variable.

I have below two sets of code, one in which the variable is globally defined (ultimately what I want) & another in which the variable is locally defined.

The first code below works fine, the variable (eMainIllustration) is locally defined and the function works well.

    var eDisplayTableSlideState = false;
    function displayTableSlideIn()
    {
// eMainIllustration is locally defined
        var eMainIllustration = document.getElementById("mainIllustration");
        if (eDisplayTableSlideState === false)
        {
            $("#displayTable").animate ({top: "-=33px", left: "+=66px", width: "-=198px", height: "-=48px"}, 750, "swing",function()
            {
                Pixastic.process (eMainIllustration,"blurfast", {amount: 0.1});
            return (eDisplayTableSlideState = true);
        } else
        {
            $("#displayTable").animate ({top: "+=33px", left: "-=66px", width: "+=198px", height: "+=48px"}, 500, "swing",function()
            {
                Pixastic.revert(eMainIllustration);
            });
            return (eDisplayTableSlideState = false);
        }
    }

However, when I set the variable (eMainIllustration) globally, the first if statement of the function works, however the pixastic.revert code in the 'else' part of the function does not. Why?

// eMainIllustration is locally defined
var eMainIllustration = document.getElementById("mainIllustration");
    var eDisplayTableSlideState = false;
    function displayTableSlideIn()
    {
        if (eDisplayTableSlideState === false)
        {
            $("#displayTable").animate ({top: "-=33px", left: "+=66px", width: "-=198px", height: "-=48px"}, 750, "swing",function()
            {
                Pixastic.process (eMainIllustration,"blurfast", {amount: 0.1});
            return (eDisplayTableSlideState = true);
        } else
        {
            $("#displayTable").animate ({top: "+=33px", left: "-=66px", width: "+=198px", height: "+=48px"}, 500, "swing",function()
            {
                Pixastic.revert(eMainIllustration);
            });
            return (eDisplayTableSlideState = false);
        }
    }

I wonder if this is because I am not returning the state of the changed (eMainIllustration) via return in the first half (if) of the function. Is that correct? And how would I achieve this?

Best,

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

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

发布评论

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

评论(3

卸妝后依然美 2024-12-20 23:59:56

如果您在函数内使用 Alert 或 console.log 输出变量,会发生什么情况?我在使用函数定义全局变量时遇到问题。

我想说的是 DOM 可能还没有准备好。尝试使用:

if(window.addEventListener) {
    window.addEventListener('DOMContentLoaded', function() {
        //DOM is now ready. Set your global variables value here.
    });
} else {
    //If internet explorer, use attachEvent method.
}

What happens if you alert or console.log the variable out inside the function? Had my issues with defining global variables using functions.

Where i am going with this is that the DOM might not the ready. Try the use of:

if(window.addEventListener) {
    window.addEventListener('DOMContentLoaded', function() {
        //DOM is now ready. Set your global variables value here.
    });
} else {
    //If internet explorer, use attachEvent method.
}
£噩梦荏苒 2024-12-20 23:59:56

这可能是由于 Javascript 在页面上的位置造成的。

如果您将它放在页面顶部,那么在加载 javascript 之前,mainIllustration dom 元素可能尚未加载。

如果将其移至 body 标记上方,则该变量将被填充,因为 dom 将在 javascript 加载时完全加载。

因此,它在函数内部起作用,因为 dom 在调用函数之前加载,但如果在函数外部则不起作用。

var eMainIllustration = document.getElementById("mainIllustration");

编辑

<script type="text/javascript">
    $(document).ready(function() {
        load("path_to_javasript");
     });
</script>

This is probably due to the location of your Javascript on the page.

If you have it at the top of your page then the mainIllustration dom element may not have loaded before the javascript is loaded.

If you move it to just above the body tag then the variable will be populated as the dom will be fully loaded by the time the javascript loads.

Thus it works when inside the function as the dom is loaded before you call the function, but does not work if it is outside the function.

var eMainIllustration = document.getElementById("mainIllustration");

Edit

<script type="text/javascript">
    $(document).ready(function() {
        load("path_to_javasript");
     });
</script>
季末如歌 2024-12-20 23:59:56

我明白了这一点。

如果有人对 Pixastic 的工作原理有任何困惑,这里是我的理解,以及我的问题的解决方案。

pixastic Blufast(&blur)插件“恢复功能”只是将元素恢复到其原始状态。换句话说,您不是添加另一个效果,而是告诉 pixastic 取消所有更改和更改。将元素替换为其原始状态。

请记住,添加像素效果后,图像不再是图像,而是 HTML5 画布元素,在这种情况下,该元素将被取消,原始“图像”将放回原处。

全局变量 &局部变量问题可以通过在函数末尾使用“return”来解决。实际上,这部分不是实际的问题,问题在于我(错误地)试图实现的恢复实现。

希望这对某人有帮助。

I figured this out.

In case anyone has any confusion of how Pixastic works, here is what my understanding, along with a solution to my problem.

The pixastic blurfast (& blur) plugin 'revert function' merely reverts the element back to its original state. In other words, you are not adding another effect but telling pixastic to cancel all the changes & replace the element with its original state.

Keep in mind that when a pixastic effect is added, the image is no longer an image but a HTML5 canvas element, which in this case, is being canceled and the original 'image' being put back in place.

The global variable & local variable problem with resolved by using the 'return' at the end of the function. Actually, this part was not the actual problem, the problem was with the revert implementation I (wrongly) was trying to implement.

Hope this helps someone.

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