简单的 JavaScript 函数来模糊图像

发布于 2024-12-09 06:52:00 字数 649 浏览 0 评论 0原文

我正在使用 [jQuery 插件][1] 使用以下代码创建模糊图像:

$(document).ready(function(){
    
var img = new Image();
img.onload = function() {
    Pixastic.process(img, "blurfast", {amount:1});
}
document.body.appendChild(img);
img.src = "image.jpg";

});

我如何重写它,而不是创建图像,我可以将其分配给 < img class="blurthisimage" >?

更新:

新代码仍然无法工作:

<img src="image.jpg" class="blurthisimage" />

<script type="text/javascript">
$(document).ready(function(){
var img = $(".blurthisimage");
img.onload = function() {
    Pixastic.process(img, "blurfast", {amount:1});
}
});
</script>

I'm using a [jQuery plugin][1] to create a blur an image with the following code:

$(document).ready(function(){
    
var img = new Image();
img.onload = function() {
    Pixastic.process(img, "blurfast", {amount:1});
}
document.body.appendChild(img);
img.src = "image.jpg";

});

How can I rewrite this so than rather than creating the image I can just assign it to < img class="blurthisimage" >?

UPDATE:

New code still not working:

<img src="image.jpg" class="blurthisimage" />

<script type="text/javascript">
$(document).ready(function(){
var img = $(".blurthisimage");
img.onload = function() {
    Pixastic.process(img, "blurfast", {amount:1});
}
});
</script>

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

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

发布评论

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

评论(2

清风无影 2024-12-16 06:52:00

更改

img = new Image();

img = $(".blurthisimage");

更新
请尝试检查图像是否已加载,

if (img[0].complete == true) {
    Pixastic.process(img[0], "blurfast", {amount:1});
}
else {
    img.load(function () {
        Pixastic.process(img[0], "blurfast", {amount:1});
    });
}

如果您想在多个图像上使用此功能,

img.each(function(index, element)
{
    if (img[index].complete == true) {
        Pixastic.process(img[index], "blurfast", {amount:1});
    }
    else {
        img.load(function () {
            Pixastic.process(img[index], "blurfast", {amount:1});
        });
    }
});

请使用以下命令:如果这不起作用,请在您的 onload 函数中添加一个警报以查看是否可以甚至火灾

Change

img = new Image();

to

img = $(".blurthisimage");

UPDATE
Try checking to see if the image is loaded with this

if (img[0].complete == true) {
    Pixastic.process(img[0], "blurfast", {amount:1});
}
else {
    img.load(function () {
        Pixastic.process(img[0], "blurfast", {amount:1});
    });
}

if you wanted to have this work on multiple images use the following:

img.each(function(index, element)
{
    if (img[index].complete == true) {
        Pixastic.process(img[index], "blurfast", {amount:1});
    }
    else {
        img.load(function () {
            Pixastic.process(img[index], "blurfast", {amount:1});
        });
    }
});

If that doesn't work add an alert to inside your onload function to see if it even fires

情感失落者 2024-12-16 06:52:00

img.onload 函数是否受到攻击?如果代码甚至没有进入该函数,可能是因为您在 jQuery 的 .ready() 函数中实例化了 .onload 代码,该代码会等待页面加载运行代码(请参阅 ready() 文档)。当函数被实例化时,图像已经被加载,因此该事件永远不会触发。

Is the img.onload function getting hit? If the code is not even getting into the function it might be because you instantiate the .onload code in jQuery's .ready() function which waits until the page is loaded to run the code (see the ready() documentation) . By the time the function gets instantiated the image has already been loaded so that event will never fire.

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