“排队”非动画属性:attr

发布于 2024-09-26 02:42:25 字数 992 浏览 4 评论 0原文

更新:好的,所以我看到我可以将每个 attr 替换包含在计时器的函数中。这意味着我会这样做五次;每个图形一次。但是,有没有更干净的方法来做到这一点?谢谢

,好的,所以我有五个不同的图像想要替换 src。但是,我希望每个都是连续的......替换第一个,然后替换第二个等等。

由于我正在更改属性源,动画队列将无济于事。

我尝试了简单的计时器插件并将其放入一个函数中,然后在每次 attr 替换后调用该函数,但所有 attr 都在函数调用之前发生。

然后,我将计时器函数、attr 替换以及对计时器函数的调用全部放在一个函数中,加载文档并调用该函数,但 attr 仍然一次性发生,然后函数调用。

所以,这是我在上面解释的最后一件事:

$(document).ready(function() {
 function holdIt(){
  myTimer = $.timer(2000,function(){
   alert("yes");
  });
 };
function doMe() {
$('#visit').attr('src', 'images/visitGrow.jpg');
holdIt();
$('#real').attr('src', 'images/realGrow.jpg');
holdIt();
$('#life').attr('src', 'images/lifeGrow.jpg');
holdIt();
$('#retirement').attr('src', 'images/retirementGrow.jpg');
holdIt();
$('#center').attr('src', 'images/centerGrow.jpg');
};

doMe();
 });
 </script>

我知道我错过了一些东西,因为它必须很容易完成,但我还没有。有什么指点吗? PS...我正在使用简单的计时器插件。

另外,我应该注意,我只是在每次 attr 替换后寻找暂停,以便警报('yes');是的,我想我只会返回 false 或 null;或其他什么。

UPDATE: alright so I see that I can just include each attr replacement within the function of the timer. That means I do that five times; once for each graphic. But, is there a cleaner way to do this? Thanks

ok, so I have five different images I want to replace the src for. However, I want each one sequential.. replace the first, then the second etc.

Since I'm changing the attribute source, the animation que won't help.

I tried the simple timer plugin and put that in a function, then called the function after each attr replace but all attr's happen before the function calls.

Then, I put the timer function, the attr replacements with the calls to the timer function all inside a function, loaded the document and called that but still, attr happens all-at-once and then function calls.

So, here is the last thing I tried explained just above:

$(document).ready(function() {
 function holdIt(){
  myTimer = $.timer(2000,function(){
   alert("yes");
  });
 };
function doMe() {
$('#visit').attr('src', 'images/visitGrow.jpg');
holdIt();
$('#real').attr('src', 'images/realGrow.jpg');
holdIt();
$('#life').attr('src', 'images/lifeGrow.jpg');
holdIt();
$('#retirement').attr('src', 'images/retirementGrow.jpg');
holdIt();
$('#center').attr('src', 'images/centerGrow.jpg');
};

doMe();
 });
 </script>

I know I'm missing something as it has to be easily accomplished but I don't have it yet. Any pointers? PS... I'm using simple plug-in for the timer.

Also, I should note that I'm just looking for a pause after each attr replace so where the alert('yes'); is, I thought I'd just return false or null; or whatever.

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

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

发布评论

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

评论(2

染柒℉ 2024-10-03 02:42:25
(function () {
    var items = $("#visit, #real, #life, #retirement, #center");
    var len = items.size();
    var urls = ["images/visitGrow.jpg", "images/realGrow.jpg", .........];
    var i = 0;

    function step() {
        items.eq(i).attr("src", urls[i]);
        i += 1;
        if (i >= len) {
            return;
        }
        setTimeout(step, 2000);
    }

    step();
})();

顺便说一句,自执行包装函数在这里创建一个本地范围,这样你就不会污染全局命名空间......

(function () {
    var items = $("#visit, #real, #life, #retirement, #center");
    var len = items.size();
    var urls = ["images/visitGrow.jpg", "images/realGrow.jpg", .........];
    var i = 0;

    function step() {
        items.eq(i).attr("src", urls[i]);
        i += 1;
        if (i >= len) {
            return;
        }
        setTimeout(step, 2000);
    }

    step();
})();

btw the self-executing wrapper function is here to create a local scope so that you don't polute the global namespace...

倥絔 2024-10-03 02:42:25

您可以使用一个简单的间隔,如下所示:

$(function() {
  var imgs = ["visit", "real", "life", "retirement", "center"], i=0,
  timer =  setInterval(function() {
    $("#" + imgs[i]).attr("src", "images/"+imgs[i]+"Grow.jpg");
    if(i++ == imgs.length) clearInterval(timer);
  }, 2000);
});

这利用了图像 URL 的约定,因此我们只是每 2 秒遍历一次数组,然后到达末尾,间隔就不会再次触发。

You could use a simple Interval, like this:

$(function() {
  var imgs = ["visit", "real", "life", "retirement", "center"], i=0,
  timer =  setInterval(function() {
    $("#" + imgs[i]).attr("src", "images/"+imgs[i]+"Grow.jpg");
    if(i++ == imgs.length) clearInterval(timer);
  }, 2000);
});

This takes advantage of your image URLs having a convention, so we're just stepping through the array every 2 seconds, then the end is reached the interval just doesn't fire again.

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