如何在 mootools 中链接数组?

发布于 2024-08-17 02:49:26 字数 350 浏览 7 评论 0原文

假设您在 mootools 中有一个数组:

// get the elements and set them as slide
var slideElements = $$('.mySlideElements');
slideElements.set('slide');

// and fire the event -> would slide all elements out at the same time
slideElements.slide('out');

我如何将其放入一个链中以一次滑动一个数组?

到目前为止,只有当我知道要执行的操作的确切数量时,我才能成功链接。感谢您的帮助。

Say you have an array in mootools:

// get the elements and set them as slide
var slideElements = $('.mySlideElements');
slideElements.set('slide');

// and fire the event -> would slide all elements out at the same time
slideElements.slide('out');

How could I put this into a chain to slide them one at a time?

So far I've only succeeded in chaining if I know the exact number of actions I will be performing. Thanks for your help.

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

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

发布评论

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

评论(3

过度放纵 2024-08-24 02:49:26

我最终在这个方面投入了大量的工作,链接比我乍一看要棘手得多。我以前从未以这种方式使用过它:D

您绝对可以循环遍历 slideElements 数组。将元素事件相互链接是棘手的部分。我最终得到了一个循环闭合函数,该函数循环遍历每个 slideElement 并将列表中的前一项链接到当前项:

<!-- Working example of chained sliding divs  -->
<div class="mySlideElements" style="background: red" id="slide1">Slide 1</div>
<div class="mySlideElements" style="background: yellow">Slide 2</div>
<div class="mySlideElements" style="background: green">Slide 3</div>
<div class="mySlideElements" style="background: purple">Slide 4</div>
<script type="text/javascript">
    var slideElements = $('.mySlideElements');
    slideElements.set('slide');

    for(var i = 1; i < slideElements.length; i++) (function(c1,c2){
        c1.get('slide').chain(function(){c2.slide()});
    })(slideElements[i-1],slideElements[i]);

</script>

<input type="button" onclick="$('slide1').slide()" value="slide"/>

调用 get('slide') 返回 Fx在每个元素上设置的 .Slide 对象,然后您可以将其链接到一个函数。我的 Mootools-fu 不够强大,无法弄清楚如何将其直接链接到下一个元素的 slide 属性(它应该是一个函数,但它没有' t 似乎有效),所以我最终得到了您在 chain() 调用中看到的匿名函数,这几乎同样好。循环闭包function(c1,c2)对于防止本地i循环变量在调用链式匿名函数时超出范围是必要的。

它被设置为当第一个元素的 slide 函数被触发时的连锁反应。如果您需要在单击任何任何元素时都进行级联,您可能需要设置每个元素的 onClick 事件来触发第一个元素的 slide() 而不是他们自己的。

无论如何,上面的代码是一个工作示例。我希望结果是您想要的。

I ended up putting a lot of work into this one, chaining was a lot trickier than I thought at first glance. I've never used it in quite this manner before :D

You can definitely loop through the slideElements array. Chaining the element events to each other is the tricky part. I ended up with a loop closure function that loops through each slideElement and chains the previous item in the list to the current item:

<!-- Working example of chained sliding divs  -->
<div class="mySlideElements" style="background: red" id="slide1">Slide 1</div>
<div class="mySlideElements" style="background: yellow">Slide 2</div>
<div class="mySlideElements" style="background: green">Slide 3</div>
<div class="mySlideElements" style="background: purple">Slide 4</div>
<script type="text/javascript">
    var slideElements = $('.mySlideElements');
    slideElements.set('slide');

    for(var i = 1; i < slideElements.length; i++) (function(c1,c2){
        c1.get('slide').chain(function(){c2.slide()});
    })(slideElements[i-1],slideElements[i]);

</script>

<input type="button" onclick="$('slide1').slide()" value="slide"/>

Calling get('slide') returns the Fx.Slide object that was set on each element, which you can then chain to a function. My Mootools-fu wasn't strong enough to figure out how to chain it directly to the slide property of the next element (which should be a function, but it didn't seem to work), so I ended up with the anonymous function you see inside the chain() call, which is almost as good. The loop closure function(c1,c2) was necessary to prevent the local i loop variable from being out of scope by the time the chained anonymous function got called.

It's set up as a chain reaction for when the first element's slide function gets triggered. If you needed the whole thing to cascade whenever any element is clicked, you'd probably want to set every element's onClick event to fire the first element's slide() and not their own.

Anyway, the code above is a working example. I hope the results are what you were looking for.

终弃我 2024-08-24 02:49:26

在我看来,这是一种更好/“mootoolsy”的做法。 FX 实例提供了两个可以使用的东西:link: "chain" 和 onComplete: 函数。所以像这样:

var slideEls = function(els) {
    if (!els.length)
        return;

    var lastEl = els.getLast();    
    lastEl.set("slide", {
        duration: "long",
        link: "chain",
        onComplete: function() {
            els.erase(lastEl);
            slideEls(els);
        }
    }).slide("out");
};

slideEls($('div.mySlideElements'));

这里的工作示例: http://mootools.net/shell/GmUpM/

这将适用于要滑出的无限数组长度的元素

in my opinion, this is a better / "mootoolsy" way of doing it. the FX instance provides two possible things that can be used: link: "chain" and onComplete: function. so something like this:

var slideEls = function(els) {
    if (!els.length)
        return;

    var lastEl = els.getLast();    
    lastEl.set("slide", {
        duration: "long",
        link: "chain",
        onComplete: function() {
            els.erase(lastEl);
            slideEls(els);
        }
    }).slide("out");
};

slideEls($('div.mySlideElements'));

working example here: http://mootools.net/shell/GmUpM/

this will work with an infinite array length of elements to slide out

长亭外,古道边 2024-08-24 02:49:26

试试这个:

$('.mySlideElements')
    .set('slide', {
        onComplete: function (el) 
        {
            if (el = el.getParent().getNext ('.mySlideElements'))
                el.slide ('out');
        }
    })
    [0].slide ('out');

它只要求 .mySlideElements 是直接同级

Try this:

$('.mySlideElements')
    .set('slide', {
        onComplete: function (el) 
        {
            if (el = el.getParent().getNext ('.mySlideElements'))
                el.slide ('out');
        }
    })
    [0].slide ('out');

It just requires that .mySlideElements be immediate siblings

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