按类别过滤元素后 IMG 幻灯片/图库循环

发布于 2024-11-09 22:41:31 字数 767 浏览 3 评论 0原文

我仍然是一个 JQuery 新手,我有这个简单的幻灯片/画廊代码,效果很好,我在一些 Web 项目中使用过它:

function f_slideShow(){
    var v_fadeTime = 600;
    var v_active = $("#divPFpics IMG.active");
    var v_next =  v_active.next().length ? v_active.next() : $("#divPFpics IMG:first");

    if (v_active.length == 0) 
        {
            v_active = $("#divPFpics IMG:last");
            v_active.addClass("last-active");
        }

    v_next.css({opacity: 0.0})
        .addClass("active")
        .animate({opacity:1.0}, v_fadeTime, function() {
            v_active.removeClass("active last-active");
    });
}

我当前的项目使用一长串按 CSS 类分类的图像。现在,这张幻灯片循环播放我的所有图像。

我的问题: 如何以及在哪里放置 .filter() 方法,以便此代码仅循环包含所选 CSS 类的图像?我已经把画廊里的其他东西都搞定了,这让我很烦恼,所以我最终转向了 Stackoverflow。谢谢大家!

I'm still a JQuery novice and I have this simple slideshow/gallery code that works great, that I've used in a few web projects:

function f_slideShow(){
    var v_fadeTime = 600;
    var v_active = $("#divPFpics IMG.active");
    var v_next =  v_active.next().length ? v_active.next() : $("#divPFpics IMG:first");

    if (v_active.length == 0) 
        {
            v_active = $("#divPFpics IMG:last");
            v_active.addClass("last-active");
        }

    v_next.css({opacity: 0.0})
        .addClass("active")
        .animate({opacity:1.0}, v_fadeTime, function() {
            v_active.removeClass("active last-active");
    });
}

My current project uses a long list of images that are categorized by CSS classes. As it is now, this slideshow cycles through ALL my images.

My question:
How and where do I put a .filter() method so that this code cycles through ONLY the images containing the chosen CSS class? I've gotten everything else working in the gallery and this is killing me so I'm finally turning to Stackoverflow. Thanks all!

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

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

发布评论

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

评论(1

厌倦 2024-11-16 22:41:31
var v_active = $("#divPFpics IMG.active");
var v_next =  v_active.next().length ? v_active.next() : $("#divPFpics IMG:first");

这些行很关键,它们构建了您的幻灯片将循环播放的列表

虽然没有显示,但我想你的函数每 x 秒调用一次。

v_active 是当前显示的图像。

v_next 是 DOM 中紧随其后的图像

你可以这样做:

var v_active = $("#divPFpics IMG.active");
var v_next = v_active.next().length ? v_active.next() : $("#divPFpics IMG:first");
while (!v_next.hasClass('yourClassName')) {
  v_next = v_active.next().length ? v_active.next() : $("#divPFpics IMG:first");
} 

但是,我建议你不要使用这样的解决方案:如果你的 img 中没有一个具有所需的类,你将无限循环。

如果您确实想使用 .filter,则必须重写这些行。这是由于选择下一张图像的方式造成的。您必须找到一种方法来保存当前活动图像的位置,选择您的图像集 ($('divPFpics').filter(...)) 并选择下一个。

var v_active = $("#divPFpics IMG.active");
var v_next =  v_active.next().length ? v_active.next() : $("#divPFpics IMG:first");

Those line are critical, they build the list that your slideshow will cycle through.

Although it's not shown, I suppose your function is called every x seconds.

v_active is the image currently being shown.

v_next is the image following it in the DOM.

You could do it this way :

var v_active = $("#divPFpics IMG.active");
var v_next = v_active.next().length ? v_active.next() : $("#divPFpics IMG:first");
while (!v_next.hasClass('yourClassName')) {
  v_next = v_active.next().length ? v_active.next() : $("#divPFpics IMG:first");
} 

However, I'd advise you to not use this solution like that : if none of your img have the required class, you'll loop endlessly.

If you really want to use .filter, you'll have to rewrite those line. This is due to the way the next image is selected. You'll have to find a way to save the position of the currently active image, select your image set ($('divPFpics').filter(...)) and pick the next one.

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