Fading multiple elements simultaneously - jquery

混江龙づ霸主 提交于 2019-12-04 03:05:21

You can use the add method to get the elements in the same jQuery object:

newPanel.add(prevBtn).add(nextBtn).add(infoPanel).fadeIn(200);

Assuming that newPanel and so on are variables created like that:

newPanel = $("#newPanel");

just write:

newPanel.fadeIn(200);
prevBtn.fadeIn(200);
nextBtn.fadeIn(200);
infoPanel.fadeIn(200);

Either do it using a $.each call or I would recommend that you group them by a class or id and fade using that.

You could for example group all related elements in classes and then do a $.each call on those classes instead of every single element.

gion_13
$.each([newPanel, prevBtn, nextBtn, infoPanel], function(i, el) {
    $(el).fadeIn(200);
});  

update:
And if you want to call the function only once, you could do something like @Guffa did by adding the elements to the same jQuery collection and only then apply the animation (once):

[newPanel, prevBtn, nextBtn, infoPanel]
    .reduce(function(el, next) {
        return el.add(next);
    })
    .fadeIn(200);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!