-
Notifications
You must be signed in to change notification settings - Fork 1
/
pluginjs.js
105 lines (88 loc) · 3.45 KB
/
pluginjs.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
/*
A simple horizontal scroller /carousel Jquery plugin
*/
/*
Author : Amit Singh Baghel
version: 1.0
*/
;
(function($) {
$.fn.horizontalScrollify = function(options) {
var settings = $.extend({
// These are the defaults.
rightButton: $("#rightBtn"),
leftButton: $("#leftBtn")
}, options);
if ($(this).length <= 0) return;
var outerContainer = $(this);
var outerContainerId = document.getElementById(outerContainer.attr('id'));
var containerTotalLeft = outerContainerId.getBoundingClientRect().right,
eleInView = 0,
totalContainer,
lastEle,
pageouterContainer = outerContainer.find('.page-container'), //should have this class on scrolling containers.
scrollWrapper = outerContainer.find('.scroll-wrppr'),
totalContainer = pageouterContainer.length;
getLastVisibleEle(true);
settings.leftButton.attr('disabled', 'disabled');
function getLastVisibleEle(countEleInView) {
lastEle = 0;
//if (!!!containerTotalLeft) //changes for FireFox because we can not get property of hidden fields.
// containerTotalLeft = outerContainerId.getBoundingClientRect().right;
var hidden = false;
pageouterContainer.each(function(index) {
var self = $(this);
//if (!!!self.width() || !!!containerTotalLeft) { // || containerTotalLeft check is patch for IE 10 as we get 0 on IE for the first time.
// hidden = true;
// return false; //Patch for FireFox.//can not calculate a hidden element's width so patched for now.
//}
//code such that if element is visible 50% then it should be considered in the visible container.
if (this.getBoundingClientRect().right > containerTotalLeft) {
lastEle = parseInt(index);
return false;
}
if (!!countEleInView) eleInView++;
});
if (hidden)
return;
if (lastEle == 0) {
lastEle = totalContainer;
}
}
settings.rightButton.bind('click', function() {
//if (!!!lastEle) //in case if broswer is Firefox or IE may be.
// getLastVisibleEle(true);
var ele = lastEle + 1;
if (lastEle == totalContainer) {
$(this).attr('disabled', 'disabled');
settings.leftButton.attr('disabled', '');
return;
} else {
$(this).attr('disabled', '');
};
var totalScroll = scrollWrapper.scrollLeft() + pageouterContainer.eq(ele - 1).position().left;
scrollWrapper.scrollLeft(totalScroll);
getLastVisibleEle();
});
settings.leftButton.bind('click', function() {
//if (!!!lastEle) //in case if broswer is Firefox. //better use detection //
// getLastVisibleEle(true);
var ele = lastEle + 1;
ele = (ele - (eleInView * 2) <= 0) ? 1 : (ele - (eleInView * 2));
var totalScroll = scrollWrapper.scrollLeft() + pageouterContainer.eq(ele - 1).position().left;
scrollWrapper.scrollLeft(totalScroll);
getLastVisibleEle();
if (ele == 1) {
$(this).attr('disabled', 'disabled');
settings.rightButton.attr('disabled', '');
return;
} else {
$(this).attr('disabled', '');
}
});
};
})(jQuery);
$(document).ready(function() {
//Calling the function.
$('#outerContainer').horizontalScrollify();
});