-
Notifications
You must be signed in to change notification settings - Fork 0
/
responsive-sprite.js
136 lines (116 loc) · 5.55 KB
/
responsive-sprite.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
function VideoThumbnail() {
this.thumbnailClassName = 'video-thumbnail';
this.thumbnailDivList = document.getElementsByClassName(this.thumbnailClassName);
this.maxThumbnailNr = 0;
this.isMouseMoveActive = false;
this.defaultThumbNrName = 'defaultimgnr';
this.imageFitSize = 100;
}
VideoThumbnail.prototype.setThumbnailClassName = function (thumbnailClassName) {
this.thumbnailClassName = thumbnailClassName;
this.thumbnailDivList = document.getElementsByClassName(this.thumbnailClassName);
};
VideoThumbnail.prototype.setThumbnailCountOfSprite = function (thumbnailCountOfSprite) {
this.maxThumbnailNr = thumbnailCountOfSprite;
this.imageFitSize = thumbnailCountOfSprite * 100;
};
VideoThumbnail.prototype.imagePaddingBottomInPercentage = function (imagePaddingBottomInPercentage, imageFitSizeInPercentage) {
var css = document.createElement("style");
css.type = "text/css";
var cssRules = '.'+ this.thumbnailClassName +' { width: '+ 100 +'%; height: 0; padding-bottom: ' + imagePaddingBottomInPercentage + '%; background-size: ' + this.imageFitSize + '%; display:block; background-repeat: no-repeat; margin:0 auto;'
css.innerHTML = cssRules;
document.body.appendChild(css);
};
VideoThumbnail.prototype.setMouseMoveActive = function () {
this.isMouseMoveActive = true;
};
VideoThumbnail.prototype.getXPositionByDefaultThumbnailNr = function (defaultThumbnailNr) {
var xPosition = 0;
xPosition = (100 / (this.maxThumbnailNr - 1));
xPosition = xPosition * (defaultThumbnailNr - 1);
return xPosition;
};
VideoThumbnail.prototype.getXPositionByDefaultThumbnailNr2 = function (defaultThumbnailNr) {
return -1;
};
VideoThumbnail.prototype.getImageNrByMousePosition = function (thumbnailMapWidth, thumbnailWidth, currentThumbnailMapPosition) {
currentThumbnailMapPosition *= thumbnailMapWidth;
var imageNr = 0
, minDeviation = currentThumbnailMapPosition
, summedThumbnailWidth = thumbnailWidth
, maxImageNr = (thumbnailMapWidth / thumbnailWidth);
for (var currentImageNr = 0; currentImageNr <= maxImageNr; currentImageNr++) {
var now = Math.abs((currentThumbnailMapPosition - summedThumbnailWidth));
if (now < minDeviation) {
minDeviation = now;
imageNr = currentImageNr;
}
summedThumbnailWidth += thumbnailWidth;
}
imageNr += 1;
return imageNr;
};
VideoThumbnail.prototype.setDivBackgroundByPosX = function (divElem, xPosition) {
if (xPosition == 0) {
divElem.style.backgroundPosition = xPosition + 'px ' + 0 + 'px';
} else {
divElem.style.backgroundPosition = xPosition + '% ' + 0 + 'px';
}
};
VideoThumbnail.prototype.setOnMouseMoveListenerByDivElement = function (divElem, thumbnailMapWidth) {
var self = this;
divElem.onmousemove = function (event) {
var percentagePos = (event.offsetX / this.offsetWidth);
var curPx = self.getImageNrByMousePosition(thumbnailMapWidth, thumbnailMapWidth / self.maxThumbnailNr, percentagePos);
var xPosition = self.getXPositionByDefaultThumbnailNr(curPx);
self.setDivBackgroundByPosX(this, xPosition);
};
};
VideoThumbnail.prototype.setOnMouseOutListenerByDivElement = function (divElem, thumbnailMapWidth) {
var self = this;
divElem.onmouseout = function (event) {
var defaultThumbnailNr = this.dataset.hasOwnProperty(self.defaultThumbNrName) ? this.dataset.defaultimgnr : 0;
var xPosition = self.getXPositionByDefaultThumbnailNr(defaultThumbnailNr);
self.setDivBackgroundByPosX(this, xPosition);
};
};
VideoThumbnail.prototype.setDivBackgroundImage = function (divElem) {
var defaultThumbnailNr = divElem.dataset.hasOwnProperty(self.defaultThumbNrName) ? divElem.dataset.defaultimgnr : 1;
var defaultBackgroundImg = divElem.dataset.hasOwnProperty('img') ? divElem.dataset.img : 1;
divElem.style.backgroundImage = 'url(' + defaultBackgroundImg + ')';
var self = this;
var thumbnailMapWidth = 0;
this.getThumbnailMapWidthByDivElement(divElem, function(imageWidth) {
thumbnailMapWidth = imageWidth;
self.setDivBackgroundByPosX(divElem, (thumbnailMapWidth - (defaultThumbnailNr * divElem.offsetWidth)));
});
};
VideoThumbnail.prototype.displayThumbs = function () {
for (var i = 0; i < this.thumbnailDivList.length; ++i) {
var defaultThumbnailNr = this.thumbnailDivList[i].dataset.hasOwnProperty(this.defaultThumbNrName) ? this.thumbnailDivList[i].dataset.defaultimgnr : 1;
var defaultBackgroundImg = this.thumbnailDivList[i].dataset.hasOwnProperty('img') ? this.thumbnailDivList[i].dataset.img : 1;
this.thumbnailDivList[i].style.backgroundImage = 'url(' + defaultBackgroundImg + ')';
var thumbnailMapWidth = 0;
var self = this;
var divItem = this.thumbnailDivList[i];
this.getThumbnailMapWidthByDivElement(divItem, defaultThumbnailNr, function(imageWidth, divElem, defaultThumbNr) {
thumbnailMapWidth = imageWidth;
divItem = divElem;
defaultThumbnailNr = defaultThumbNr;
var xPosition = self.getXPositionByDefaultThumbnailNr(defaultThumbnailNr);
self.setDivBackgroundByPosX(divItem, xPosition);
if (self.isMouseMoveActive) {
self.setOnMouseMoveListenerByDivElement(divItem, thumbnailMapWidth);
self.setOnMouseOutListenerByDivElement(divItem, thumbnailMapWidth);
}
});
}
};
VideoThumbnail.prototype.getThumbnailMapWidthByDivElement = function (divElem, defaultThumbnailNr, callback) {
var imageSrc = divElem.style.backgroundImage.replace(/url\((['"])?(.*?)\1\)/gi, '$2').split(',')[0];
var image = new Image();
image.src = imageSrc;
image.onload = function(){
callback(image.width, divElem, defaultThumbnailNr);
};
};