遍历多个无序列表

时间:2015-06-23 15:14:48

标签: javascript jquery html-lists

考虑以下标记:

<div class="col6">
    <h2>Accueil</h2>
    <ul class="listing">
        <li><a href="#" title="bg_home">Accueil</a></li>
        <li><a href="#" title="bg_homeTakeover">Homepage Takeover</a></li>
        <li><a href="#" title="bg_homePromoOverlay">Homepage Promo Overlay</a></li>
        <li><a href="#" title="bg_homeFeatureOverlay">Homepage Feature Overlay</a></li>
        <li><a href="#" title="bg_homeFeatureScrollOverlay">Homepage Feature Scroll + Takeover</a></li>
    </ul>

    <h2>Thèmes & intégrations</h2>
    <ul class="listing">                        
        <li><a href="#" title="bg_le30">Le 30</a></li>
        <li><a href="#" title="bg_menuCarte">Menu à la carte</a></li>                           
        <li><a href="#" title="bg_intTheme">Intégration thème</a></li>
        <li><a href="#" title="bg_intVirtuel01">Intégration papier virtuel 1</a></li>
        <li><a href="#" title="bg_intVirtuel02">Intégration papier virtuel 2</a></li>
    </ul>

    <h2>Galeries</h2>
    <ul class="listing">
        <li><a href="#" title="bg_gal01">St-Valentin noir & blanc</a></li>
        <li><a href="#" title="bg_gal02">Cuisinez avec le fromage</a></li>
        <li><a href="#" title="bg_gal03">Des fraises pour dessert</a></li>
        <li><a href="#" title="bg_gal04">Nos meilleures recettes de homard</a></li>
        <li><a href="#" title="bg_gal05">Jeudi 5@7</a></li>
    </ul>
</div>

我想用#leftArrow和#rightArrow遍历uls。我有一个带叠加的页面,我用这些箭头更改背景图像。但是,当点击这些箭头时,我无法从第一个ul传递到第二个,依此类推。

这里是Jquery

$(document).ready(function(){

    var current_li;
    //OPEN OVERLAY
    $('.innerWrap a').click(function(){

        var src = $(this).attr("title");

        current_li = $(this).parent();

        var text = $(current_li).text();
        $('#frame h2').text(text) 

        $('#projectOverlay').css({'background-image': 'url(img/' + src + '.jpg)' ,});


        $('#frame').fadeIn(400);
        $('#projectOverlay').fadeIn(400);

    })

    //RIGHT
    $("#rightArrow").click(function(){

        if(current_li.is(":last-child")) {                  
            var next_li = current_li.parents('ul').siblings().find('li').first();                                                   
        } else {
            var next_li = current_li.next();            
        }

        var next_src = next_li.children('a').attr("title");            

        current_li = next_li; 
    })

    //LEFT
    $("#leftArrow").click(function(){

        if(current_li.is(":first-child")) {
            //var prev_li = $('.innerWrap ul li').last();
            var prev_li = current_li.parents('ul').siblings().find('li').last();
        } else {
            var prev_li = current_li.prev();
        }

        var prev_src= prev_li.children('a').attr("title");         

        current_li = prev_li;

    })

});

任何帮助将不胜感激!

谢谢!

1 个答案:

答案 0 :(得分:0)

https://jsfiddle.net/rgwst1xj/4/

编辑:解决方案

$(document).ready(function(){

// I set current_li manually, so change it
var current_li = $('a').first().parent();
// Use your own method to determine which ul is the current one
var current_ul = 0;

//RIGHT
$("#rightArrow").click(function(){

    if(current_li.is(":last-child")) {
        // you may want to be more specific with $('ul').length
        current_ul = (current_ul + 1)%$('ul').length;
        var next_li = current_li.parent().parent().find('ul:eq('+current_ul+')').find('li').first();
    } else {
        var next_li = current_li.next();            
    }

    var next_src = next_li.children('a').attr("title");

    current_li = next_li;

    console.log(current_li.text())
    var text = $(current_li).text();
    $('#frame h2').text(text);  
})

//LEFT
$("#leftArrow").click(function(){

    if(current_li.is(":first-child")) {
        //var prev_li = $('.innerWrap ul li').last();
        current_ul = (current_ul - 1)%$('ul').length;
        var prev_li = current_li.parent().parent().find('ul:eq('+current_ul+')').find('li').last();
    } else {
        var prev_li = current_li.prev();
    }

    var prev_src= prev_li.children('a').attr("title");

    $('#projectOverlay').animate({opacity: 0}, 200, function() {
        $(this)
            .css({'background-image': 'url(img/' + prev_src + '.jpg)' ,})
            .animate({opacity: 1});

            bgSize($('#projectOverlay'), function(width, height){
            $("#frame").css("height",height)                
            $("#projectOverlay").css("height",height);
        });     
     });         

    current_li = prev_li;
    console.log(current_li.text())
    var text = $(current_li).text();
    $('#frame h2').text(text); 

})
});