html - Creating an external Javascript file and linking in div -
i trying script working external javascript file, can give me hand? needs placed in div tag within webpage
var imgarray = new array("home page/images/slideshow/bus-resize.jpg", "home page/images/slideshow/.jpg", "home page/images/slideshow/.jpg"); var imgcount = 0; function starttime() { if(imgcount == imgarray.length) { imgcount = 0; } document.getelementbyid("img2").src = imgarray[imgcount]; imgcount++; settimeout("starttime()", 5000); }
in order make code work need line in html:
<imd id=img2></img><script>starttime();</script> but please don't use solution since make repeated requests, images loaded, changing src attribute.
instead use makes same thing instead of changing src attribute uses 3 elements , hides 2 of them.
js
var each = function(array, callback){ for(var = 0; < array.length; ++i){ callback.apply(array, [array[i], i, array]); } }; var hideall = function(elements) { each(elements, function(img){ img.classname = 'hide'; }); }; var flipimages = document.queryselectorall('.images-flip img'); hideall(flipimages); var counter = function(init, cb, t){ this.start = function(){ init.call(this); this.loop(); }; this.callback = cb; this.loop = function(){ cleartimeout(this.timeout); this.timeout = settimeout(this.loop.bind(this), t); this.callback.call(this); }; this.stop = function(){ cleartimeout(this.timeout); }; return this; }; var fliparray = document.queryselectorall('.images-flip'); each(fliparray, function(flip){ flip.counter = new counter(function(){ this.count = 0; }, /*loop*/ function(){ hideall(flip.children); flip.children[this.count].classname = ''; this.count = (this.count+1) % flip.children.length; }, flip.dataset.time); flip.counter.start(); }); html
<div class="images-flip" data-time="1000"> <img src="https://developers.google.com/apps-script/images/script-128.png"> <img src="https://developers.google.com/google-apps/images/manage.png"> <img src="https://developers.google.com/google-apps/images/apidownload128.gif"> </div> <div class="images-flip" data-time="1500"> <img src="https://developers.google.com/_static/images/gplus-32.png"> <img src="https://developers.google.com/_static/images/android-32.png"> <img src="https://developers.google.com/_static/images/chrome-32.png"> css
.images-flip { position: relative; width: 128px; height: 128px; float: left; } .images-flip img { position: absolute; } .images-flip img.hide { opacity: 0; }
Comments
Post a Comment