Icontem

File: demo/Gallery.js

Recommend this page to a friend!
  Classes of Mark Rolich  >  JS Image Magnifier  >  demo/Gallery.js  >  Download  
File: demo/Gallery.js
Role: Auxiliary script
Content type: text/plain
Description: Demo gallery
Class: JS Image Magnifier
Show a zoomed version of an image under the mouse
Author: By
Last change: Added data attributes support, attachment to mutliple images via class, use thumbnail image if large is not set
Date: 5 years ago
Size: 1,853 bytes
 

Contents

Class file image Download
var Gallery = function (evt, m, options) {
    "use strict";

    var len = options.images.length,
        cur = -1,
        curImg = null,
        walk = function (dir) {
            if (cur === -1 && dir === -1) {
                cur = 0;
            }

            cur += dir;

            if (cur === -1 || cur === len) {
                cur -= len * dir;
            }

            curImg = options.gallery.childNodes[cur].childNodes[0].childNodes[0];
            m.setThumb(curImg);

            options.gallery.parentNode.scrollTop = (curImg.offsetHeight) * cur;
            options.previewText.childNodes[0].nodeValue = options.images[cur].title;
        };

    this.render = function () {
        var i = 0,
            frag = document.createDocumentFragment(),
            li = document.createElement('li'),
            a = document.createElement('a'),
            img = document.createElement('img');

        for (i; i < len; i += 1) {
            li = li.cloneNode(false);
            a = a.cloneNode(false);
            img = img.cloneNode(false);

            a.href = options.images[i].url;
            a.title = options.images[i].title;
            img.src = options.images[i].thumb;
            img.setAttribute('data-large-img-url', options.images[i].large);
            img.className = 'img';
            img.id = 'img-' + i;

            a.appendChild(img);
            li.appendChild(a);

            frag.appendChild(li);
        }

        options.gallery.appendChild(frag);

        walk(1);

        m.attach({
            thumb: '.img',
            zoomable: true
        });
    };

    this.next = function () {
        walk(1);
    };

    this.prev = function () {
        walk(-1);
    };

    evt.attach('mousedown', options.prev, this.prev);
    evt.attach('mousedown', options.next, this.next);

    this.render();
};