javascript - How to iterate through children with jQuery, store values from CSS properties of each child's background image and use these values on another element -


i building slider using jquery. want build dynamically works on different number of slides depending on user input.

the simple example of code output if user created 5 slides through admin dashboard in wordpress:

<div class="slides">     <div class="slide">slide content background image</div>     <div class="slide">slide content background image</div>     <div class="slide">slide content background image</div>     <div class="slide">slide content background image</div>     <div class="slide">slide content background image</div> </div> <div class="controls">     <a class="thumbnail_slider_control"></a>     <a class="thumbnail_slider_control"></a>     <a class="thumbnail_slider_control"></a>     <a class="thumbnail_slider_control"></a>     <a class="thumbnail_slider_control"></a> </div> 

background images applied through trough css background-image property. wanted iterate through children of .slides , save background image values inside array (that first thought).

afterward, loop through .controls children , apply same urls background images of children.

my goal here create thumbnail slider navigation every control has associated background image applied slide background image eliminate need users assigning same url on 2 places manually.

i searched similar problem here seems there no such question.

fairly easy match control slide index use css() both getter , setter

var $slides = $('.slide');    $('.thumbnail_slider_control').each(function(i){    $(this).css('backgroundimage', $slides.eq(i).css('backgroundimage') )  })
.slide, .thumbnail_slider_control{    display:block;    background-repeat: no-repeat;    height:50px  }  #slide-1{    background-image: url(http://via.placeholder.com/150x50)  }  #slide-2{    background-image: url(http://via.placeholder.com/150x50/000000/ffffff)  }    #slide-3{    background-image: url(http://via.placeholder.com/150x50/990000/ffffff)  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div class="slides" style="float:left; width:50%">      slides      <div id="slide-1" class="slide"></div>      <div id="slide-2" class="slide"></div>      <div id="slide-3"  class="slide"></div>  </div>  <div class="controls"  style="float:right; width:50%">      controls      <a class="thumbnail_slider_control"></a>      <a class="thumbnail_slider_control"></a>      <a class="thumbnail_slider_control"></a>         </div>


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -