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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
(function(){
// Toutes mes fonctions
var myFonctions = {
// Ajout d'écouteur
add_event : function(evenement, element, fonction){
if(element.addEventListener){
element.addEventListener(evenement, fonction, false);
}
else{
element.attachEvent('on'+evenement, fonction);
}
},
// Selection d'un élément par l'id
id : function(elem){
return document.getElementById(elem);
},
// Transfert obj source, obj des
transfer : function(source, dest){
var prop, transfered={};
for ( prop in dest ) { transfered[prop] = dest[prop]; }
for ( prop in source ) { transfered[prop] = source[prop]; }
return transfered;
},
// Taille de la page
pageDim : function(elem){
if(elem){
var d = dE = dB = elem,
w, h;
}
else{
var d = document,
dE = d.documentElement,
dB = d.body,
w, h;
}
h = dE.scrollHeight;
w = dE.scrollWidth;
if ( dE.clientHeight > h ) { h = dE.clientHeight; }
if ( dE.clientWidth > w ) { w = dE.clientWidth; }
if ( dB.scrollHeight > h ) { h = dB.scrollHeight; }
if ( dB.scrollWidth > w ) { w = dB.scrollWidth; }
return {'w':w, 'h':h} ;
},
// Taille de la fenetre
winDim : function(){
var W,H,
i = window,
d = document,
de = d.documentElement,
db = d.body;
if ( i.innerWidth ) {
W = i.innerWidth;
H = i.innerHeight;
} else if ( de.clientWidth ) {
W = de.clientWidth;
H = de.clientHeight;
}
else {
W = db.clientWidth;
H = db.clientHeight;
}
return {w:W, h:H} ;
},
// Position h et v du defilement
scrolled : function(){
var x,y,
w = window,
d = document,
de = d.documentElement,
db = d.body;
if ( w.pageXOffset!==undefined) {
x = w.pageXOffset;
y = w.pageYOffset;
}
else {
x = de.scrollLeft ? de.scrollLeft : (db.scrollLeft?db.scrollLeft:0) ;
y = de.scrollTop ? de.scrollTop : (db.scrollTop?db.scrollTop:0) ;
}
return {'x':x, 'y':y};
},
// position de elem
getPos : function(elem,choix){
var pos={'r':0,'l':0,'t':0,'b':0},
tmp=elem, choix = choix || null;
do {
pos.l += tmp.offsetLeft;
tmp = tmp.offsetParent;
} while( tmp !== choix );
pos.r = pos.l + elem.offsetWidth;
tmp=elem;
do {
pos.t += tmp.offsetTop;
tmp = tmp.offsetParent;
} while( tmp !== choix );
pos.b = pos.t + elem.offsetHeight;
return pos;
},
//Calcul de la durre
duree : function(y,x,tp){
y = Math.sqrt( Math.pow( y, 2 ) + Math.pow( x, 2 ) );
var tmp = 0, i = 0 ;
while ( tmp < y ) {
i++ ;
tmp = tmp + i*i ;
}
return i * tp ;
},
// Annule l'action par default
preventDefault : function(e){
e = e||event;
if (e.preventDefault) { e.preventDefault(); }
else { e.returnValue = false; }
},
//Effets easing => Le premier marche, les autres que du haut vers le vas
// actualStep: current time, startValue: beginning value, endValue: change in value, totalSteps: duration
easeInOut : function( startValue, endValue, totalSteps, actualStep, powr, round ){
var stepp, delta = endValue - startValue;
stepp = startValue+(Math.pow(((1 / totalSteps) * actualStep), powr) * delta);
return round ? Math.round(stepp) : stepp;
},
easeInOutQuad : function(startValue, endValue, totalSteps, actualStep, powr){
if ((actualStep/=totalSteps/2) < 1) return endValue/2*actualStep*actualStep + startValue;
return -endValue/2 * ((--actualStep)*(actualStep-2) - 1) + startValue;
},
easeInOutElastic: function (startValue, endValue, totalSteps, actualStep) {
var s = 1.70158, p = 0, a = endValue;
//alert(startValue+' '+endValue+' '+totalSteps+' '+actualStep);
if (actualStep === 0) {
return startValue;
}
if ((actualStep /= totalSteps / 2) === 2) {
return startValue + endValue;
}
if (!p) {
p = totalSteps * (0.3 * 1.5);
}
if (a < Math.abs(endValue)) {
a = endValue;
s = p / 4;
} else {
s = p / (2 * Math.PI) * Math.asin(endValue / a);
}
if (actualStep < 1) {
return -0.5 * (a * Math.pow(2, 10 * (actualStep -= 1)) * Math.sin((actualStep * totalSteps - s) * (2 * Math.PI) / p)) + startValue;
}
return a * Math.pow(2, -10 * (actualStep -= 1)) * Math.sin((actualStep * totalSteps - s) * (2 * Math.PI) / p) * 0.5 + endValue + startValue;
},
easeInElastic: function (startValue, endValue, totalSteps, actualStep) {
var s = 1.70158, p = 0, a = endValue;
if (actualStep === 0) {
return startValue;
}
if ((actualStep /= totalSteps) === 1) {
return startValue + endValue;
}
if (!p) {
p = totalSteps * 0.3;
}
if (a < Math.abs(endValue)) {
a = endValue;
s = p / 4;
} else {
s = p / (2 * Math.PI) * Math.asin(endValue / a);
}
return -(a * Math.pow(2, 10 * (actualStep -= 1)) * Math.sin((actualStep * totalSteps - s) * (2 * Math.PI) / p)) + startValue;
}
}
var scroll = function(options){
this.options = {
ease : 0.3,
effet: 'easeInOut',
duree : 50,
move : 'window', //Par default
finish : false
};
this.options = myFonctions.transfer(options,this.options);
this.init = function(elem){
this.startTime = new Date().getTime();
var t_page = myFonctions.pageDim(),
t_win = myFonctions.winDim(),
element = myFonctions.getPos(elem),
init_pos = myFonctions.scrolled(),
self = this;
// En cas de destination trop proche du bord
if(element.l + t_win.w > t_page.w){
element.l = t_page.w - t_win.w;
}
if(element.t + t_win.h > t_page.h){
element.t = t_page.h - t_win.h;
}
//Calcul de la duree
var duree = myFonctions.duree(init_pos.y - element.t, init_pos.x - element.l,this.options.duree);
this.next(t_page,t_win,element,init_pos,self,duree);
};
this.next = function(t_page,t_win,element,init_pos,self,duree){
var timer = setTimeout(function(){
self.frame(t_page,t_win,element,init_pos,self,duree);
},25);
}
this.frame = function(t_page,t_win,element,init_pos,self,duree){
var time = new Date().getTime(),
actuel_t = time - this.startTime,
calculTime = Math.min(actuel_t,duree),
options = this.options,
newX = myFonctions[options.effet](init_pos.x, element.l,duree,calculTime,options.ease);
newY = myFonctions[options.effet](init_pos.y, element.t,duree,calculTime,options.ease);
window.scrollTo(newX,newY);
if(actuel_t >= duree){
window.scrollTo(element.l,element.t);
if(options.finish){options.finish();}
}
else{
this.next(t_page,t_win,element,init_pos,self,duree);
}
}
}; |
Partager