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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
.slideIn{
overflow:hidden;
width:500px;
height:300px;
background-color:#FFFFFF;
border:1px solid #000000;
position:relative;
}
.slideIn div{
width:458px;
height:258px;
border:1px dashed #666;
position:absolute;
display:none;
margin:20px;
}
</style>
<script type="text/javascript">
var SlideIn = function(){
//Variable globale
this.idObj = null;
this.timer = null;
this.from = null;
this.to = null;
this.direction = null;
//Tableau d'élément a déplacé
this.elementToSlide = new Array();
//Index de l'élément en cours
this.currentIndex = 0;
//Paramètre pour lancement automatique
this.timePause = 0 ; //permet de définir le temps de pause entre deux slide
this.auto = false ; //Permet d'activer ou non le slide automatique
}
/*##########################################################
#################### On ajoute les méthodes sur l'objet ###################
##########################################################*/
//Permet de récupérer un élément par id
SlideIn.prototype.$ = function(element){
return document.getElementById(element);
};
//Méthode permettant de lancer les animations si en auto :)
SlideIn.prototype.go = function(){
if(this.auto){
switch (this.direction ){
case 'left':
this.SlideToLeft();
break;
case 'right':
this.SlideToRight();
break;
case 'top':
this.SlideToTop();
break;
case 'bottom':
this.SlideToBottom();
break;
}
}
}
//Méthode permettant d'ajouter un élément
SlideIn.prototype.AddElement = function(element){
if(typeof(element) == "string"){
this.elementToSlide.push(this.$(element));
}
else if(typeof(element) == "object"){
this.elementToSlide.push(element);
}
}
//Méthode permettant d'ajouter plusieurs élément d'un coup
SlideIn.prototype.AddElements = function (elements){
for(var i = 0 , l = elements.length; i < l ;i++){
this.AddElement(elements[i]);
}
}
//Méthode permettant de déplacer les éléments vers la gauche
SlideIn.prototype.SlideToLeft = function(){
if(this.direction == null || this.direction == 'left'){
var me = this ;
//On vérifit la direction pour initialiser le positionnement
if(this.direction != 'left'){
this.direction = 'left';
if(this.timer == null){
this.Positionne();
}
}
else if(this.direction == 'left' && this.auto && this.timer == null){
this.Positionne();
}
if(this.timer != null){
clearTimeout(this.timer);
this.timer = null;
}
//Si le timer n'est pas finit on détruit l'ancienne div
if(parseInt(this.from.style.left) == Number.NaN || (parseInt(this.from.parentNode.offsetWidth) + parseInt(this.from.style.left))> 0){
this.from.style.left = parseInt(this.from.style.left) - 15 + "px";
this.to.style.left =parseInt(this.to.style.left) - 15 + "px";
//alert((parseInt(this.$(from).offsetWidth) + parseInt(this.$(from).style.left)));
this.timer = setTimeout(function(){me.SlideToLeft()},25);
}
else{
clearTimeout(this.timer);
this.timer = null;
this.currentIndex = (this.currentIndex == (this.elementToSlide.length-1)) ? 0:this.currentIndex + 1;
this.$('debug').innerHTML = this.currentIndex;
this.Positionne();
if(this.auto){
setTimeout(function(){me.SlideToLeft()},this.timePause);
}
else{
this.direction = null;
}
}
}
};
//Méthode permettant de déplacer les éléments vers la droite
SlideIn.prototype.SlideToRight = function(){
var me = this ;
if(this.direction == null || this.direction == 'right'){
if(this.direction != 'right'){
this.direction = 'right';
if(this.timer == null){
this.Positionne();
}
}
else if(this.direction == 'right' && this.auto && this.timer == null){
this.Positionne();
}
if(this.timer != null){
clearTimeout(this.timer);
this.timer = null;
}
//Si le timer n'est pas finit on détruit l'ancienne div
if(parseInt(this.from.style.left) == Number.NaN || parseInt(this.from.style.left) < parseInt(this.from.parentNode.offsetWidth)){
this.from.style.left = parseInt(this.from.style.left) + 15 + "px";
this.to.style.left =parseInt(this.to.style.left) + 15 + "px";
//alert((parseInt(this.$(from).offsetWidth) + parseInt(this.$(from).style.left)));
this.timer = setTimeout(function(){me.SlideToRight()},25);
}
else{
clearTimeout(this.timer);
this.timer = null;
this.currentIndex = (this.currentIndex == 0) ? this.elementToSlide.length-1:this.currentIndex - 1;
this.$('debug').innerHTML = this.currentIndex;
this.Positionne();
if(this.auto){
setTimeout(function(){me.SlideToRight()},this.timePause);
}
else{
this.direction = null;
}
}
}
};
//Méthode permettant de déplacer les éléments vers la gauche
SlideIn.prototype.SlideToTop = function(){
var me = this ;
if(this.direction == null || this.direction == 'top'){
//On vérifit la direction pour initialiser le positionnement
if(this.direction != 'top'){
this.direction = 'top';
if(this.timer == null){
this.Positionne();
}
}
if(this.timer != null){
clearTimeout(this.timer);
this.timer = null;
}
//Si le timer n'est pas finit on détruit l'ancienne div
if(parseInt(this.from.style.top) == Number.NaN || (parseInt(this.from.style.top) > - parseInt(this.from.parentNode.offsetHeight))){
this.from.style.top = parseInt(this.from.style.top) - 15 + "px";
this.to.style.top =parseInt(this.to.style.top) - 15 + "px";
this.timer = setTimeout(function(){me.SlideToTop()},25);
}
else{
clearTimeout(this.timer);
this.timer = null;
this.currentIndex = (this.currentIndex == 0) ? this.elementToSlide.length-1:this.currentIndex - 1;
this.$('debug').innerHTML = this.currentIndex;
this.Positionne();
if(this.auto){
setTimeout(function(){me.SlideToTop()},this.timePause);
}
else{
this.direction = null;
}
}
}
};
//Méthode permettant de déplacer les éléments vers le bas
SlideIn.prototype.SlideToBottom = function(){
var me = this
if(this.direction == null || this.direction == 'bottom'){
//On vérifit la direction pour initialiser le positionnement
if(this.direction != 'bottom'){
this.direction = 'bottom';
if(this.timer == null){
this.Positionne();
}
}
if(this.timer != null){
clearTimeout(this.timer);
this.timer = null;
}
//Si le timer n'est pas finit on détruit l'ancienne div
if(parseInt(this.from.style.top) == Number.NaN || parseInt(this.from.style.top) < parseInt(this.from.parentNode.offsetHeight)){
this.from.style.top = parseInt(this.from.style.top) + 15 + "px";
this.to.style.top =parseInt(this.to.style.top) + 15 + "px";
;
this.timer = setTimeout(function(){me.SlideToBottom()},25);
}
else{
clearTimeout(this.timer);
this.timer = null;
this.currentIndex = (this.currentIndex == this.elementToSlide.length-1) ? 0:this.currentIndex + 1;
this.$('debug').innerHTML = this.currentIndex;
this.Positionne();
if(this.auto){
setTimeout(function(){me.SlideToBottom()},this.timePause);
}
else{
this.direction = null;
}
}
}
};
//Fonction initialisant le tableau en positionnant tous les éléments :)
SlideIn.prototype.Positionne = function(){
if(this.direction == 'left'){
//On vérifit que l'on est pas a la fin sinon le premier devient le dernier
if(this.currentIndex == this.elementToSlide.length-1){
//récupération des éléments :
this.from = this.elementToSlide[this.currentIndex];
this.to = this.elementToSlide[0]; //Premier élément
}
else{
this.from = this.elementToSlide[this.currentIndex];
this.to = this.elementToSlide[this.currentIndex + 1];
}
this.from.style.display = "block" ;
this.from.style.left = 0 + "px";
this.to.style.left = this.from.parentNode.offsetWidth + "px";
this.to.style.display = "block";
//Posionement vertical
this.to.style.top = 0 + "px";
this.from.style.top = 0 + "px" ;
}
else if(this.direction == 'right'){
if(this.currentIndex == 0){
this.from = this.elementToSlide[this.currentIndex];
this.to = this.elementToSlide[this.elementToSlide.length-1]; // dernier élément
}
else{
this.from = this.elementToSlide[this.currentIndex];
this.to = this.elementToSlide[this.currentIndex-1];
}
this.from.style.display = "block" ;
this.from.style.left = 0 + "px";
this.to.style.left = - (this.from.parentNode.offsetWidth )+ "px";
this.to.style.display = "block";
//Posionement vertical
this.to.style.top = 0 + "px";
this.from.style.top = 0 + "px" ;
}
else if(this.direction == 'bottom'){
if(this.currentIndex == this.elementToSlide.length-1){
this.from = this.elementToSlide[this.currentIndex];
this.to = this.elementToSlide[0]; // dernier élément
}
else{
this.from = this.elementToSlide[this.currentIndex];
this.to = this.elementToSlide[this.currentIndex+1];
}
this.from.style.display = "block" ;
this.from.style.top = 0 + "px";
this.to.style.top = - (this.from.parentNode.offsetHeight )+ "px";
this.to.style.display = "block";
//Posionement horizontal
this.to.style.left = 0 + "px";
this.from.style.left = 0 + "px" ;
}
else if(this.direction == 'top'){
if(this.currentIndex == 0){
this.from = this.elementToSlide[this.currentIndex];
this.to = this.elementToSlide[this.elementToSlide.length-1]; // dernier élément
}
else{
this.from = this.elementToSlide[this.currentIndex];
this.to = this.elementToSlide[this.currentIndex-1];
}
this.from.style.display = "block" ;
this.from.style.top = 0 + "px";
this.to.style.top = (this.from.parentNode.offsetHeight )+ "px";
this.to.style.display = "block";
//Posionement horizontal
this.to.style.left = 0 + "px";
this.from.style.left = 0 + "px" ;
}
};
</script>
</head>
<body>
<div class="slideIn">
<div id="la1" style="background-color:#FFFFFF;display:block">la première</div>
<div id="la2" style="background-color:blue;">la</div>
<div id="la3" style="background-color:#ccc;">la</div>
<div id="la4" style="background-color:yellow;">la jolie</div>
<div id="la5" style="background-color:black;">la</div>
<div id="la6" style="background-color:pink;">la 6 eme</div>
</div>
<input type="button" value="gauche" onclick="newSlide.SlideToLeft()">
<input type="button" value="droite" onclick="newSlide.SlideToRight()">
<input type="button" value="Top" onclick="newSlide.SlideToTop()">
<input type="button" value="Bottom" onclick="newSlide.SlideToBottom()">
<input type="button" value="Auto : on" onclick="newSlide.auto = (newSlide.auto) ? false:true;(!newSlide.auto) ?this.value='auto : off':this.value='auto : on'">
<span id="debug"></span>
<script type="text/javascript">
//On instancie un slide
var newSlide = new SlideIn();
//On ajoute les éléments souhaités ( ici un tableau ) on peut utiliser la méthode AddElement pour ajouter un seul élément. on peut ajouter un id ou directement l'élément ;-)
newSlide.AddElements(Array('la1','la2','la3','la4','la5',document.getElementById('la6')));
//On définit le temps de pause entre deux slide
newSlide.timePause = 2000;
//On passe auto a true
newSlide.auto = true;
//On indique la direction souhaité
newSlide.direction = 'left';
//On lance :-)
newSlide.go();
</script>
</body>
</html> |
Partager