javascript - JS Image Transition Not Working -
i unable css opacity transition work adding using javascript. please let me know wrong code. http://jsfiddle.net/copperspeed/bvwbb
(function () { var myimgs = document.getelementbyid('vz0'); var = 0; function cycle() { if (i <= 3) { var myarray = [ 'http://jsrun.it/assets/t/r/u/o/truot.jpg', 'http://jsrun.it/assets/6/c/y/s/6cysh.jpg', 'http://jsrun.it/assets/w/m/r/i/wmriq.jpg', 'http://jsrun.it/assets/5/q/8/f/5q8fw.jpg' ]; console.log(myarray[0]); myimgs.setattribute("src", myarray[i]); if (myimgs.style.opacity === '0') { console.log('trans'); myimgs.style.transitionproperty = 'opacity'; myimgs.style.transitionduration = "1500ms"; } if (myimgs.style.opacity === '1') { console.log('opacity-0'); myimgs.style.opacity = '0'; } i++; settimeout(function () { cycle(); }, 3000);
there couple of issues script.
- the opacity style doesn't exist on element on initialization. need account in logic
- on second pass through, opacity style exist , may 0, condition needs accounted for
- your second if statement reverses did in first conditional. statement should in else-if
- you cycling 1 image element in/out transition 1 image won't work expected. either need change 2 elements or change transitioning strategy accommodate single element.
code changed 1-3 above:
(function () { var myimgs = document.getelementbyid('vz0'); var = 0; function cycle() { if (i <= 3) { var myarray = ['http://jsrun.it/assets/t/r/u/o/truot.jpg', 'http://jsrun.it/assets/6/c/y/s/6cysh.jpg', 'http://jsrun.it/assets/w/m/r/i/wmriq.jpg', 'http://jsrun.it/assets/5/q/8/f/5q8fw.jpg']; myimgs.setattribute("src", myarray[i]); if (myimgs.style.opacity === '' || myimgs.style.opacity == 0) { console.log(myimgs.style.opacity + '0'); myimgs.style.transitionproperty = 'opacity'; myimgs.style.transitionduration = "1500ms"; myimgs.style.opacity = 1; } else if (myimgs.style.opacity == 1) { console.log(myimgs.style.opacity + '2'); myimgs.style.opacity = 0; } i++; settimeout(function () { cycle(); }, 3000); if (i === 4) { = 0; } } } cycle(); }());
for item #4 above - here refactored version uses 2 img elements manage transition in , out:
html:
<div class="imgwrapper"> <img src="http://jsrun.it/assets/t/r/u/o/truot.jpg" id="vz0" class="vzimage" alt="first" height="300" width="300" /> <img src="http://jsrun.it/assets/t/r/u/o/truot.jpg" id="vz1" class="vzimage" alt="first" height="300" width="300" /> </div>
css:
.imgwrapper { position: relative; height: 300px; width: 300px; } .vzimage { opacity:0; position: absolute; top: 0; left: 0; bottom: 0; right: 0; }
script:
(function () { var myimgs = document.getelementsbyclassname('vzimage'); var myarray = ['http://jsrun.it/assets/t/r/u/o/truot.jpg', 'http://jsrun.it/assets/6/c/y/s/6cysh.jpg', 'http://jsrun.it/assets/w/m/r/i/wmriq.jpg', 'http://jsrun.it/assets/5/q/8/f/5q8fw.jpg']; // consider moving .vsimage in stylesheet for(var j = 0; j < myimgs.length; ++j) { myimgs[j].style.transitionproperty = 'opacity'; myimgs[j].style.transitionduration = "1500ms"; } function cycle(i) { var myarrayidx = % myarray.length; var imgidx = % myimgs.length; var previmgidx = (i-1) % myimgs.length; myimgs[imgidx].setattribute("src", myarray[myarrayidx]); myimgs[imgidx].style.opacity = 1; if(myimgs[previmgidx]) { myimgs[previmgidx].style.opacity = 0; } settimeout(function () { cycle(i+1); }, 3000); } cycle(0); }());
Comments
Post a Comment