jquery - Add next / prev image caption to slider navigation buttons -


i'm trying make next , previous buttons in slider show caption next , previous images, respectively. should when hover on next button caption next image in slider appears. ditto previous button.

i've got slider created gallery field in advanced custom fields. have multiple sliders on same page, using same markup. difference background images, , captions of each image. markup simplified below, imagine 3-4 iterations of code below on 1 page (again, thing different between each iteration of slider background images , captions)

<div class='theslider'> <div class="controls">      <button class="prev"></button>      <button class="next"></button> </div> <div class="the-slides">    <ul>      <li><div class='slide' style="background-image...">          <span>item caption 1</span>          </li>      <li><div class='slide' style="background-image...">          <span>item caption 2</span>         </li>      <li><div class='slide' style="background-image...">          <span>item caption 3</span>      </li>   </ul> </div> 

i trying use jquery .clone , .html functions move captions slide li elements slider buttons.

the way slider works, current image first li element. therefore, "next" button should have caption "li:nth-child(2)" , "previous" button should have caption "li:last-child".

i've been able progress following:

var $thecaption = jquery('li:nth-child(2) span'); var $button = jquery($thecaption).closest('.theslider').find('button.next'); var $nextcaption = jquery($thecaption).clone(); jquery($button).html($nextcaption); 

but end result next button captions every slider on page.

<button class="next">     <span>caption 2</span>     <span>caption 2</span>     <span>caption 2</span> </button> 

how specify caption shares same parent element ".slider" target next button gets moved button?

any appreciated. in advance.

i figured out. using wrong element use common ancestor ensure $button , $thecaption part of same slider.

*note: slider button wrapped in span class "control_next".

<span class="control_next">    <button class="next"></button> </span> 

using button "control_next" element trigger able to...

a: move along it's ancestors find closest parent ".theslider", there find caption child of ".theslider" parent, '.slides-list li:nth-child(2) span'.

var $thecaption = jquery(this).closest('.theslider').find('.slides-list li:nth-child(2) span'); 

b: find span caption placed, using same origin (the trigger)

var $button = jquery(this).find('.btn-caption.right'); 

c: clone caption, , set variable placed.

var $nextcaption = jquery($thecaption).clone(); 

d: execute on each slide. (the slider shifts li elements, appending last li slide top , on slides cycled through: slider code based on this)

here's working code below:

jquery(document).ready(function(){ jquery('.control_next').mouseenter(function() { var $thecaption = jquery(this).closest('.theslider').find('.slides-list li:nth-child(2) span'); var $button = jquery(this).find('.btn-caption.right'); var $nextcaption = jquery($thecaption).clone(); jquery($targetbutton).html($nextcaption); }); }); 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -