I am trying to follow Jquery code for a div slideshow. Basically, I want to show div1
while hiding div2
(and vice-versa) with all the child elements of the visible div
to be visible as well.
The code below only show the first child elements of the visible div and hides all child elements and then show the child elements while hiding the former.
Can someone please help figure out how to show a div
and its child elements, while hiding the other sibling div
s? Like a slideshow.
Moreover, it would be great to show only those container div
s in the slideshow where at least one image element has a src URL set (i.e. not set to null or "").
Thanks dkj
HTML
<div id="slideshow" class="site-content" role="main">
<div id="div1_wrapper" class="slides">
<div id="div1_bg">
<img src="<?php echo get_option('div1_bg'); ?>" />
</div>
<div id="div1_productimg">
<img src="<?php echo get_option('div1_productimg'); ?>" />
</div>
</div>
<div id="div2_wrapper" class="slides">
<div id="div2_bg">
<img src="<?php echo get_option('div2_bg'); ?>" />
</div>
<div id="div2_productimg">
<img src="<?php echo get_option('div2_productimg'); ?>" />
</div>
</div>
</div>
JQUERY
<script>
$(function () {
$('.slideshow div').hide(); // hide all slides
$('.slideshow div:first-child').show(); // show first slide
setInterval(function () {
$('.slideshow div:first-child').fadeOut(500)
.next('div').fadeIn(1000)
.end().appendTo('.slideshow');
},
3000); // slide duration
});
</script>
After reading your comments to my previous answer, here's another version that should be closer to your requirement:
New Code
HTML Code (unaltered structure, 2 divs and some
src
tags with empty content added):JS Code:
And another fiddle to show the changed functionality.
I think this might help you see, i have some different hacks to solve, you have your own.
Have a look, if it helps you.
DEMO
I updated the Fiddle to better match your question: first remove all slider elements that contain images with no big or product images; then check if at least one element remains. If at least one element has been found, hide all sliders, show the first, initialise the slide show and start cycling the slides.
JS code:
HTML Code:
4. updated demo
This will remove all DIVs with empty images, hide all wrappers and unhide those with at least an image with proper source. It will then cycle the images and hide/unhide the current image slide.
Remark: I wouldn't use too many IDs if they are not needed, rather set IDs on elements that require it for identification and using classes for all elements with same function or type (i.e.
<div class="div_big">
rather than<div id="div1_bg">
)