index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. var imf = function () {
  2. var lf = 0;
  3. var instances = [];
  4. function getElementsByClass (object, tag, className) {
  5. var o = object.getElementsByTagName(tag);
  6. for ( var i = 0, n = o.length, ret = []; i < n; i++)
  7. if (o[i].className == className) ret.push(o[i]);
  8. if (ret.length == 1) ret = ret[0];
  9. return ret;
  10. }
  11. function addEvent (o, e, f) {
  12. if (window.addEventListener) o.addEventListener(e, f, false);
  13. else if (window.attachEvent) r = o.attachEvent('on' + e, f);
  14. }
  15. function createReflexion (cont, img) {
  16. var flx = false;
  17. if (document.createElement("canvas").getContext) {
  18. flx = document.createElement("canvas");
  19. flx.width = img.width;
  20. flx.height = img.height;
  21. var context = flx.getContext("2d");
  22. context.translate(0, img.height);
  23. context.scale(1, -1);
  24. context.drawImage(img, 0, 0, img.width, img.height);
  25. context.globalCompositeOperation = "destination-out";
  26. var gradient = context.createLinearGradient(0, 0, 0, img.height * 2);
  27. gradient.addColorStop(1, "rgba(255, 255, 255, 0)");
  28. gradient.addColorStop(0, "rgba(255, 255, 255, 1)");
  29. context.fillStyle = gradient;
  30. context.fillRect(0, 0, img.width, img.height * 2);
  31. } else {
  32. /* ---- DXImageTransform ---- */
  33. flx = document.createElement('img');
  34. flx.src = img.src;
  35. flx.style.filter = 'flipv progid:DXImageTransform.Microsoft.Alpha(' +
  36. 'opacity=50, style=1, finishOpacity=0, startx=0, starty=0, finishx=0, finishy=' +
  37. (img.height * .25) + ')';
  38. }
  39. /* ---- insert Reflexion ---- */
  40. flx.style.position = 'absolute';
  41. flx.style.left = '-1000px';
  42. cont.appendChild(flx);
  43. return flx;
  44. }
  45. /* //////////// ==== ImageFlow Constructor ==== //////////// */
  46. function ImageFlow(oCont, size, zoom, border) {
  47. this.diapos = [];
  48. this.scr = false;
  49. this.size = size;
  50. this.zoom = zoom;
  51. this.bdw = border;
  52. this.oCont = oCont;
  53. this.oc = document.getElementById(oCont);
  54. this.scrollbar = getElementsByClass(this.oc, 'div', 'scrollbar');
  55. this.text = getElementsByClass(this.oc, 'div', 'text');
  56. this.title = getElementsByClass(this.text, 'div', 'title');
  57. this.legend = getElementsByClass(this.text, 'div', 'legend');
  58. this.bar = getElementsByClass(this.oc, 'img', 'bar');
  59. this.arL = getElementsByClass(this.oc, 'img', 'arrow-left');
  60. this.arR = getElementsByClass(this.oc, 'img', 'arrow-right');
  61. this.bw = this.bar.width;
  62. this.alw = this.arL.width - 5;
  63. this.arw = this.arR.width - 5;
  64. this.bar.parent = this.oc.parent = this;
  65. this.arL.parent = this.arR.parent = this;
  66. this.view = this.back = -1;
  67. this.resize();
  68. this.oc.onselectstart = function () { return false; }
  69. /* ---- create images ---- */
  70. var img = getElementsByClass(this.oc, 'div', 'bank').getElementsByTagName('a');
  71. this.NF = img.length;
  72. for (var i = 0, o; o = img[i]; i++) {
  73. this.diapos[i] = new Diapo(this, i,
  74. o.rel,
  75. o.title || '- ' + i + ' -',
  76. o.innerHTML || o.rel,
  77. o.href || '',
  78. o.target || '_self'
  79. );
  80. }
  81. /* ==== add mouse wheel events ==== */
  82. if (window.addEventListener)
  83. this.oc.addEventListener('DOMMouseScroll', function(e) {
  84. this.parent.scroll(-e.detail);
  85. }, false);
  86. else this.oc.onmousewheel = function () {
  87. this.parent.scroll(event.wheelDelta);
  88. }
  89. /* ==== scrollbar drag N drop ==== */
  90. this.bar.onmousedown = function (e) {
  91. if (!e) e = window.event;
  92. var scl = e.screenX - this.offsetLeft;
  93. var self = this.parent;
  94. /* ---- move bar ---- */
  95. this.parent.oc.onmousemove = function (e) {
  96. if (!e) e = window.event;
  97. self.bar.style.left = Math.round(Math.min((self.ws - self.arw - self.bw), Math.max(self.alw, e.screenX - scl))) + 'px';
  98. self.view = Math.round(((e.screenX - scl) ) / (self.ws - self.alw - self.arw - self.bw) * self.NF);
  99. if (self.view != self.back) self.calc();
  100. return false;
  101. }
  102. /* ---- release scrollbar ---- */
  103. this.parent.oc.onmouseup = function (e) {
  104. self.oc.onmousemove = null;
  105. return false;
  106. }
  107. return false;
  108. }
  109. /* ==== right arrow ==== */
  110. this.arR.onclick = this.arR.ondblclick = function () {
  111. if (this.parent.view < this.parent.NF - 1)
  112. this.parent.calc(1);
  113. }
  114. /* ==== Left arrow ==== */
  115. this.arL.onclick = this.arL.ondblclick = function () {
  116. if (this.parent.view > 0)
  117. this.parent.calc(-1);
  118. }
  119. }
  120. /* //////////// ==== ImageFlow prototype ==== //////////// */
  121. ImageFlow.prototype = {
  122. /* ==== targets ==== */
  123. calc : function (inc) {
  124. if (inc) this.view += inc;
  125. var tw = 0;
  126. var lw = 0;
  127. var o = this.diapos[this.view];
  128. if (o && o.loaded) {
  129. /* ---- reset ---- */
  130. var ob = this.diapos[this.back];
  131. if (ob && ob != o) {
  132. ob.img.className = 'diapo';
  133. ob.z1 = 1;
  134. }
  135. /* ---- update legend ---- */
  136. this.title.replaceChild(document.createTextNode(o.title), this.title.firstChild);
  137. this.legend.replaceChild(document.createTextNode(o.text), this.legend.firstChild);
  138. /* ---- update hyperlink ---- */
  139. if (o.url) {
  140. o.img.className = 'diapo link';
  141. window.status = 'hyperlink: ' + o.url;
  142. } else {
  143. o.img.className = 'diapo';
  144. window.status = '';
  145. }
  146. /* ---- calculate target sizes & positions ---- */
  147. o.w1 = Math.min(o.iw, this.wh * .5) * o.z1;
  148. var x0 = o.x1 = (this.wh * .5) - (o.w1 * .5);
  149. var x = x0 + o.w1 + this.bdw;
  150. for (var i = this.view + 1, o; o = this.diapos[i]; i++) {
  151. if (o.loaded) {
  152. o.x1 = x;
  153. o.w1 = (this.ht / o.r) * this.size;
  154. x += o.w1 + this.bdw;
  155. tw += o.w1 + this.bdw;
  156. }
  157. }
  158. x = x0 - this.bdw;
  159. for (var i = this.view - 1, o; o = this.diapos[i]; i--) {
  160. if (o.loaded) {
  161. o.w1 = (this.ht / o.r) * this.size;
  162. o.x1 = x - o.w1;
  163. x -= o.w1 + this.bdw;
  164. tw += o.w1 + this.bdw;
  165. lw += o.w1 + this.bdw;
  166. }
  167. }
  168. /* ---- move scrollbar ---- */
  169. if (!this.scr && tw) {
  170. var r = (this.ws - this.alw - this.arw - this.bw) / tw;
  171. this.bar.style.left = Math.round(this.alw + lw * r) + 'px';
  172. }
  173. /* ---- save preview view ---- */
  174. this.back = this.view;
  175. }
  176. },
  177. /* ==== mousewheel scrolling ==== */
  178. scroll : function (sc) {
  179. if (sc < 0) {
  180. if (this.view < this.NF - 1) this.calc(1);
  181. } else {
  182. if (this.view > 0) this.calc(-1);
  183. }
  184. },
  185. /* ==== resize ==== */
  186. resize : function () {
  187. this.wh = this.oc.clientWidth;
  188. this.ht = this.oc.clientHeight;
  189. this.ws = this.scrollbar.offsetWidth;
  190. this.calc();
  191. this.run(true);
  192. },
  193. /* ==== move all images ==== */
  194. run : function (res) {
  195. var i = this.NF;
  196. while (i--) this.diapos[i].move(res);
  197. }
  198. }
  199. /* //////////// ==== Diapo Constructor ==== //////////// */
  200. Diapo = function (parent, N, src, title, text, url, target) {
  201. this.parent = parent;
  202. this.loaded = false;
  203. this.title = title;
  204. this.text = text;
  205. this.url = url;
  206. this.target = target;
  207. this.N = N;
  208. this.img = document.createElement('img');
  209. this.img.src = src;
  210. this.img.parent = this;
  211. this.img.className = 'diapo';
  212. this.x0 = this.parent.oc.clientWidth;
  213. this.x1 = this.x0;
  214. this.w0 = 0;
  215. this.w1 = 0;
  216. this.z1 = 1;
  217. this.img.parent = this;
  218. this.img.onclick = function() { this.parent.click(); }
  219. this.parent.oc.appendChild(this.img);
  220. /* ---- display external link ---- */
  221. if (url) {
  222. this.img.onmouseover = function () { this.className = 'diapo link'; }
  223. this.img.onmouseout = function () { this.className = 'diapo'; }
  224. }
  225. }
  226. /* //////////// ==== Diapo prototype ==== //////////// */
  227. Diapo.prototype = {
  228. /* ==== HTML rendering ==== */
  229. move : function (res) {
  230. if (this.loaded) {
  231. var sx = this.x1 - this.x0;
  232. var sw = this.w1 - this.w0;
  233. if (Math.abs(sx) > 2 || Math.abs(sw) > 2 || res) {
  234. /* ---- paint only when moving ---- */
  235. this.x0 += sx * .1;
  236. this.w0 += sw * .1;
  237. if (this.x0 < this.parent.wh && this.x0 + this.w0 > 0) {
  238. /* ---- paint only visible images ---- */
  239. this.visible = true;
  240. var o = this.img.style;
  241. var h = this.w0 * this.r;
  242. /* ---- diapo ---- */
  243. o.left = Math.round(this.x0) + 'px';
  244. o.bottom = Math.floor(this.parent.ht * .25) + 'px';
  245. o.width = Math.round(this.w0) + 'px';
  246. o.height = Math.round(h) + 'px';
  247. /* ---- reflexion ---- */
  248. if (this.flx) {
  249. var o = this.flx.style;
  250. o.left = Math.round(this.x0) + 'px';
  251. o.top = Math.ceil(this.parent.ht * .75 + 1) + 'px';
  252. o.width = Math.round(this.w0) + 'px';
  253. o.height = Math.round(h) + 'px';
  254. }
  255. } else {
  256. /* ---- disable invisible images ---- */
  257. if (this.visible) {
  258. this.visible = false;
  259. this.img.style.width = '0px';
  260. if (this.flx) this.flx.style.width = '0px';
  261. }
  262. }
  263. }
  264. } else {
  265. /* ==== image onload ==== */
  266. if (this.img.complete && this.img.width) {
  267. /* ---- get size image ---- */
  268. this.iw = this.img.width;
  269. this.ih = this.img.height;
  270. this.r = this.ih / this.iw;
  271. this.loaded = true;
  272. /* ---- create reflexion ---- */
  273. this.flx = createReflexion(this.parent.oc, this.img);
  274. if (this.parent.view < 0) this.parent.view = this.N;
  275. this.parent.calc();
  276. }
  277. }
  278. },
  279. /* ==== diapo onclick ==== */
  280. click : function () {
  281. if (this.parent.view == this.N) {
  282. /* ---- click on zoomed diapo ---- */
  283. if (this.url) {
  284. /* ---- open hyperlink ---- */
  285. window.open(this.url, this.target);
  286. } else {
  287. /* ---- zoom in/out ---- */
  288. this.z1 = this.z1 == 1 ? this.parent.zoom : 1;
  289. this.parent.calc();
  290. }
  291. } else {
  292. /* ---- select diapo ---- */
  293. this.parent.view = this.N;
  294. this.parent.calc();
  295. }
  296. return false;
  297. }
  298. }
  299. /* //////////// ==== public methods ==== //////////// */
  300. return {
  301. /* ==== initialize script ==== */
  302. create : function (div, size, zoom, border) {
  303. /* ---- instanciate imageFlow ---- */
  304. var load = function () {
  305. var loaded = false;
  306. var i = instances.length;
  307. while (i--) if (instances[i].oCont == div) loaded = true;
  308. if (!loaded) {
  309. /* ---- push new imageFlow instance ---- */
  310. instances.push(
  311. new ImageFlow(div, size, zoom, border)
  312. );
  313. /* ---- init script (once) ---- */
  314. if (!imf.initialized) {
  315. imf.initialized = true;
  316. /* ---- window resize event ---- */
  317. addEvent(window, 'resize', function () {
  318. var i = instances.length;
  319. while (i--) instances[i].resize();
  320. });
  321. /* ---- stop drag N drop ---- */
  322. addEvent(document.getElementById(div), 'mouseout', function (e) {
  323. if (!e) e = window.event;
  324. var tg = e.relatedTarget || e.toElement;
  325. if (tg && tg.tagName == 'HTML') {
  326. var i = instances.length;
  327. while (i--) instances[i].oc.onmousemove = null;
  328. }
  329. return false;
  330. });
  331. /* ---- set interval loop ---- */
  332. setInterval(function () {
  333. var i = instances.length;
  334. while (i--) instances[i].run();
  335. }, 16);
  336. }
  337. }
  338. }
  339. /* ---- window onload event ---- */
  340. addEvent(window, 'load', function () { load(); });
  341. }
  342. }
  343. }();
  344. /* ==== create imageFlow ==== */
  345. // div ID , size, zoom, border
  346. imf.create("imageFlow", 0.15, 1.5, 10);