javascript - Fade in original element without displaying secondary one? -
setinterval(function () { $(".story1").fadeout(2000,function(){ $(".story2").fadein(2000, function(){ $(".story2").fadeout(2000) }); }) });
that code have. story1
displayed on page when loaded, , trying fade out story1
story2
, fade out story2
fading in story1
(so there's endless loop of story1
fading out story2
, fading out story1
again etc. bit carousal, goes 1
2
3
1
again)
however, when story1
fades out , story2
fades in, keeps fading out , fading in story2
on , on again. i'm not sure how correct this?
it's future-proof things this, example if wanted add third story later, you'd able code:
var r = 0, stories = $(".stories").children(); setinterval(function () { r = (r+1) % stories.length; stories.fadeout(2000); stories.eq(r).stop().fadein(2000); }, 5000);
.stories { position: relative; } .story { position: absolute; display: none; } .story:first-child { display: block; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="stories"> <div class="story story1">lorem ipsum</div> <div class="story story2">dolor sit amet</div> </div>
not code shorter way, work no matter how many stories there (except zero, that's error anyway). feel free test 3 stories, or fifty!
Comments
Post a Comment