Yala Yala Herzlin
Yala Yala Herzlin

Reputation: 622

Next prev navigation between divs

I have tried to make navigation experience between divs that share the same class: (.art_details).This div is nested inside the li element.

It is hidden by default and fills the screen completely when the li item is clicked.

Then I need to create navigation between the .art_details divs, next and previous.

This is the mockup, next should navigate between the 1st, 2nd, 3rd and 4th and so on.

[https://i.sstatic.net/Md8mD.png][1]

I've found a very useful script, but the problem I'm having is that the navigation stops after the 1st item, only navigates the first next one and stops there.

The setup is a bit more complex that the fiddle, which does not work but has the Js code.

  <ul id="group">

  <li class="artwork_grid_item">          


  <div class="item_front"> 

  <img class="img1" width="200px" height="auto" src="http://dummyimage.com/600x400/000/fff">
  <h4>Title</h4>
  </div>  

  <div class="art_details" style="display:none;">
  <div class="closex">X</div>
  <h1> Artwork title </h1>
  <img class="img2" src="http://dummyimage.com/600x400/000/fff">
  <div id="next">next</div>
  <div style="display:none" id="prev">prev</div>
  </div>
  </li>

  <li class="artwork_grid_item">          
        <div class="item_front"> 
      <img class="img1" width="200px" height="auto" src="http://dummyimage.com/600x400/000/fff">
  <h4>Title</h4>
  </div>      
  <div class="art_details" style="display:none;">
  <div class="closex">X</div>
  <h1> Artwork title </h1>
  <img class="img2" src="http://dummyimage.com/600x400/000/fff">
  <div id="next">next</div>
  <div style="display:none" id="prev">prev</div>
  </div>
  </li>

  </ul>

  <div class="navigation_art">
  <div id="next">next</div>
  <div style="display:none" id="prev">prev</div>
 </div>

The jS

// navigation 

function updateItems(delta)
{
    var $items = jQuery('.art_details');
    var $current = $items.filter('.expanded');
    var index = $current.index();
    var newIndex = index+delta;
    // Range check the new index
    newIndex = (newIndex < 0) ? 0 : ((newIndex > $items.length) ? $items.length : newIndex); 
    if (newIndex != index){
        $current.fadeOut('fast').removeClass('expanded');
        $current = $items.eq(newIndex).fadeIn('fast').addClass('expanded');
        // Hide/show the next/prev
        jQuery("#prev").toggle(!$current.is($items.first()));    
        jQuery("#next").toggle(!$current.is($items.last()));    
    }
}
jQuery("#next").click(function () {
    updateItems(1);
});
jQuery("#prev").click(function () {
    updateItems(-1);
});

The .expanded class is added when the items are clicked with jquery:

// open artwork 

jQuery(function(jQuery) {
 jQuery('.item_front').click(function() {
   jQuery('body').addClass("noscroll");
   jQuery(this).parent().addClass("active");
   jQuery(".art_details").not(jQuery(this).next(".art_details")).fadeOut('fast');
   jQuery(this).next('.art_details').fadeIn('200, swing');
   jQuery(this).next('.art_details').addClass('expanded');
    });
}); 

// close all of them

jQuery('.closex').click(function() {
  jQuery('body').removeClass("noscroll");
 jQuery('.art_details').fadeOut('1900, swing');
 jQuery('.artwork_grid_item').removeClass("active");
 jQuery('.art_details').removeClass('expanded');

});

Upvotes: 1

Views: 386

Answers (1)

Sayed Rafeeq
Sayed Rafeeq

Reputation: 1219

Why not you adopt Bootstrap Framework for your requirement.

Tabs sections with next and previous buttons.

<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
  <li role="presentation" class="active"><a href="#tab1" role="tab" data-toggle="tab">Tab Number 1</a></li>
  <li role="presentation"><a href="#tab2" role="tab" data-toggle="tab">Tab Number 2</a></li>
  <li role="presentation"><a href="#tab3" role="tab" data-toggle="tab">Tab Number 3</a></li>
  <li role="presentation"><a href="#tab4" role="tab" data-toggle="tab">Tab Number 4</a></li>
  <li role="presentation"><a href="#tab5" role="tab" data-toggle="tab">Tab Number 5</a></li>
  <li role="presentation"><a href="#tab6" role="tab" data-toggle="tab">Tab Number 6</a></li>
  <li role="presentation"><a href="#tab7" role="tab" data-toggle="tab">Tab Number 7</a></li>
  <li role="presentation"><a href="#tab8" role="tab" data-toggle="tab">Tab Number 8</a></li>
  <li role="presentation"><a href="#tab9" role="tab" data-toggle="tab">Tab Number 9</a></li>
  <li role="presentation"><a href="#tab10" role="tab" data-toggle="tab">Tab Number 10</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
  <div role="tabpanel" class="tab-pane active" id="tab1">Tab 1 content...</div>
  <div role="tabpanel" class="tab-pane" id="tab2">Tab 2 content...</div>
  <div role="tabpanel" class="tab-pane" id="tab3">Tab 3 content...</div>
  <div role="tabpanel" class="tab-pane" id="tab4">Tab 4 content...</div>
  <div role="tabpanel" class="tab-pane" id="tab5">Tab 5 content...</div>
  <div role="tabpanel" class="tab-pane" id="tab6">Tab 6 content...</div>
  <div role="tabpanel" class="tab-pane" id="tab7">Tab 7 content...</div>
  <div role="tabpanel" class="tab-pane" id="tab8">Tab 8 content...</div>
  <div role="tabpanel" class="tab-pane" id="tab9">Tab 9 content...</div>
  <div role="tabpanel" class="tab-pane" id="tab10">Tab 10 content...</div>
</div>

Check the demo link.

Bootstrap Horizontal Scrolling Tabs

Mockup

Upvotes: 1

Related Questions