Jquery Anything 滑块导航栏回调

发布于 2024-11-15 12:49:09 字数 1145 浏览 1 评论 0原文

我为 Anything Slider 创建了一个自定义导航栏功能,它将根据单击的“按钮”切换幻灯片。我还需要编写一些内容,当按下其中一个滑块箭头时将切换“当前”类,但我找不到要查找的事件名称或在何处插入回调函数。

任何帮助将不胜感激。如果有帮助的话,我已经添加了我的导航栏功能。

$('nav ul li').click(function(){ 

    $('nav ul li').removeClass('current'); // reset current menu button

        var slideName = $(this).attr('class'); // get class name

        $(this).toggleClass('current'); // make button current

        $('section.about_us').fadeOut('slow');
        $('#about_us_container').fadeOut('slow', function(){ // hide about us page

        switch (slideName){ //change slide
            case 'navItem1':
                $('#slider').anythingSlider(1);
                    break;
            case 'navItem2':
                $('#slider').anythingSlider(2);
                break;
            case 'navItem3':
                $('#slider').anythingSlider(3);
                break;
            case 'navItem4':
                $('#slider').anythingSlider(4);
                break;
            case 'navItem5':
                $('#slider').anythingSlider(5);
            default:
                //do nothing
        }
    });
});

I created a custom nav bar function for the Anything Slider that will switch slides based on which 'button' is clicked. I need to also write something that will toggle the 'current' class when one of the slider arrows is pushed but I can't find the event name to look for or where to insert a callback function.

Any help would be appreciated. I've included my nav bar function if it helps.

$('nav ul li').click(function(){ 

    $('nav ul li').removeClass('current'); // reset current menu button

        var slideName = $(this).attr('class'); // get class name

        $(this).toggleClass('current'); // make button current

        $('section.about_us').fadeOut('slow');
        $('#about_us_container').fadeOut('slow', function(){ // hide about us page

        switch (slideName){ //change slide
            case 'navItem1':
                $('#slider').anythingSlider(1);
                    break;
            case 'navItem2':
                $('#slider').anythingSlider(2);
                break;
            case 'navItem3':
                $('#slider').anythingSlider(3);
                break;
            case 'navItem4':
                $('#slider').anythingSlider(4);
                break;
            case 'navItem5':
                $('#slider').anythingSlider(5);
            default:
                //do nothing
        }
    });
});

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

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

发布评论

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

评论(2

满意归宿 2024-11-22 12:49:09

您可以只使用 appendControlsTo 选项并让插件完成所有工作(demo )。

在AnythingSlider(版本1.7+)的最新版本中,您可以附加箭头(单独)、控制面板(导航+播放按钮)、导航或仅播放按钮到页面上的任何元素。

或者,如果您需要保持菜单完整,请尝试以下操作(演示):

$('#slider').anythingSlider({
    // If true, builds a list of anchor links to link to each panel
    buildNavigation: false,
    onSlideComplete: function(slider){
        $('nav ul li').eq(slider.currentPage-1).trigger('click');
    }
});

$('nav ul li').click(function() {
    // prevent infinite loop
    if ($(this).is('.current')) { return; }

    $('nav ul li').removeClass('current'); // reset current menu button
    var slideName = $(this).attr('class'); // get class name
    $(this).toggleClass('current'); // make button current

    $('#slider').anythingSlider(slideName.slice(-1));
});

You could just use the appendControlsTo option and have the plugin do all the work (demo).

In the latest verison of AnythingSlider (version 1.7+), you can append the arrows (separately), control panel (navigation + play button), navigation, or just the play button to any element on the page.

Or, if you need to keep your menu intact, try this (demo):

$('#slider').anythingSlider({
    // If true, builds a list of anchor links to link to each panel
    buildNavigation: false,
    onSlideComplete: function(slider){
        $('nav ul li').eq(slider.currentPage-1).trigger('click');
    }
});

$('nav ul li').click(function() {
    // prevent infinite loop
    if ($(this).is('.current')) { return; }

    $('nav ul li').removeClass('current'); // reset current menu button
    var slideName = $(this).attr('class'); // get class name
    $(this).toggleClass('current'); // make button current

    $('#slider').anythingSlider(slideName.slice(-1));
});
以往的大感动 2024-11-22 12:49:09

查看了插件源代码后,我认为单击箭头时它不会广播事件。这很容易添加,但是如果您不想修改插件,则在插件代码运行后,您可以将自己的点击处理程序绑定到箭头,然后在这些处理程序中获取当前页面并通过 AnythingSlider 的外部 API 获取的页数:

var current = $('#slider').data('AnythingSlider').currentPage
var pages = $('#slider').data('AnythingSlider').pages

请注意,var current 将提供点击之前的页码,因此您需要加或减 1 并确保你正在处理正确地结束范围。然后确定实际的当前页面后,您应该能够将 current 类应用到您所需的 LI

顺便说一句:由于您要向这些 LI 添加当前类,所以我认为当 $(this).attr('class ') 返回 'navItem2 current' 而不仅仅是 'navItem2'。对其进行一些重构可能会在以后减轻您的痛苦。

Having looked over the plugin source, I don't think it broadcasts events when the arrows are clicked. That would be very easy to add in, but if you don't want to modify the plugin, after the plugin code is run, you can just bind your own click handlers to the arrows, then in those handlers, get the current page and the numbers of pages via AnythingSlider's external API:

var current = $('#slider').data('AnythingSlider').currentPage
var pages = $('#slider').data('AnythingSlider').pages

Note that the var current will provide the page number before the click, so you'll need to add or subtract one and make sure you're handling the end-of-range properly. Then with the real current page determined, you should be able to apply the current class to your desired LI.

As an aside: since you're going to be adding a current class to these LIs, I think you'll have issues with your switch statement when $(this).attr('class') returns 'navItem2 current' instead of just 'navItem2'. Some refactoring of that will probably save you pain later.

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