IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

JavaScript Discussion :

Connaitre le déplacement du Scroll d'une DIV


Sujet :

JavaScript

  1. #1
    Membre habitué
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    478
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Décembre 2007
    Messages : 478
    Points : 189
    Points
    189
    Par défaut Connaitre le déplacement du Scroll d'une DIV
    Bonjour,

    Je vais essayer d’être clair.
    J'ai une fonction javascript récupérer sur une site qui permet de faire des cliquer/Déplacer dans des zones prédéfinies grace à des <ul></ul>
    Comme le nombre de zones est différent à chaque fois et que cela ferait une page très large 3000px, j'ai place ces zone dans une div scroollable.

    Le problème que je rencontre est que le point de référence qui est pris en compte est la div dhtmlgoodies_mainContainer et que celle-ci ne peut pas savoir si j'ai fais un scroll vers la gauche.

    J'avais donc pensé que je pouvais calculer le nombre de pixel lorsque on scrool la div avec l'ìd=scrool et ajouter cela au déplacement de la souris.

    Savez vous si cela est possible et comment le faire?

    Merci de votre aide

    Voici le bout du code qui permet d'obtenir la position de reference

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    function getLeftPos(inputObj)
    	{
    	  var returnValue = inputObj.offsetLeft;
    	  while((inputObj = inputObj.offsetParent) != null){
    	  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
    	  }
    	  return returnValue;
    	}
     
     
     
    <div id="scrool" style="width:800px;height:350px;overflow:auto;">
     
     
    	<div id="dhtmlgoodies_mainContainer">
    <ul id="2006.12.25">
     
    			</ul>
    		</div>
    <div><p> mar.  26/12/2006 </p>
     
     
     
     
    			<ul id="2006.12.26">
     
    			</ul>
    		</div>
    <div><p> mer.  27/12/2006 </p> 
    </div>
    </div>

    et voici le code au complet de la fonction
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
     
    <script type="text/javascript">
    	/************************************************************************************************************
    	Textarea maxlength
    	Copyright (C) November 2005  DTHMLGoodies.com, Alf Magne Kalleland
     
    	This library is free software; you can redistribute it and/or
    	modify it under the terms of the GNU Lesser General Public
    	License as published by the Free Software Foundation; either
    	version 2.1 of the License, or (at your option) any later version.
     
    	This library is distributed in the hope that it will be useful,
    	but WITHOUT ANY WARRANTY; without even the implied warranty of
    	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    	Lesser General Public License for more details.
     
    	You should have received a copy of the GNU Lesser General Public
    	License along with this library; if not, write to the Free Software
    	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
     
    	Dhtmlgoodies.com., hereby disclaims all copyright interest in this script
    	written by Alf Magne Kalleland.
     
    	Alf Magne Kalleland, 2010
    	Owner of DHTMLgoodies.com
     
    	************************************************************************************************************/
     
    	/* VARIABLES YOU COULD MODIFY */
    	var boxSizeArray = [10,10,10,10,10,10,10,10,10];	// Array indicating how many items there is rooom for in the right column ULs
     
     
    	var verticalSpaceBetweenListItems = 3;	// Pixels space between one <li> and next
    											// Same value or higher as margin bottom in CSS for #dhtmlgoodies_dragDropContainer ul li,#dragContent li
     
     
    	var indicateDestionationByUseOfArrow = true;	// Display arrow to indicate where object will be dropped(false = use rectangle)
     
    	var cloneSourceItems = false;	// Items picked from main container will be cloned(i.e. "copy" instead of "cut").
    	var cloneAllowDuplicates = false;	// Allow multiple instances of an item inside a small box(example: drag Student 1 to team A twice
     
    	/* END VARIABLES YOU COULD MODIFY */
     
    	var dragDropTopContainer = false;
    	var dragTimer = -1;
    	var dragContentObj = false;
    	var contentToBeDragged = false;	// Reference to dragged <li>
    	var contentToBeDragged_src = false;	// Reference to parent of <li> before drag started
    	var contentToBeDragged_next = false; 	// Reference to next sibling of <li> to be dragged
    	var destinationObj = false;	// Reference to <UL> or <LI> where element is dropped.
    	var dragDropIndicator = false;	// Reference to small arrow indicating where items will be dropped
    	var ulPositionArray = new Array();
    	var mouseoverObj = false;	// Reference to highlighted DIV
     
    	var MSIE = navigator.userAgent.indexOf('MSIE')>=0?true:false;
    	var navigatorVersion = navigator.appVersion.replace(/.*?MSIE (\d\.\d).*/g,'$1')/1;
     
    	var arrow_offsetX = -5;	// Offset X - position of small arrow
    	var arrow_offsetY = 0;	// Offset Y - position of small arrow
     
    	if(!MSIE || navigatorVersion > 6){
    		arrow_offsetX = -6;	// Firefox - offset X small arrow
    		arrow_offsetY = -13; // Firefox - offset Y small arrow
    	}
     
    	var indicateDestinationBox = false;
    	function getTopPos(inputObj)
    	{
    	  var returnValue = inputObj.offsetTop;
    	  while((inputObj = inputObj.offsetParent) != null){
    	  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
    	  }
    	  return returnValue;
    	}
     
    	function getLeftPos(inputObj)
    	{
    	  var returnValue = inputObj.offsetLeft;
    	  while((inputObj = inputObj.offsetParent) != null){
    	  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
    	  }
    	  return returnValue;
    	}
     
    	function cancelEvent()
    	{
    		return false;
    	}
    	function initDrag(e)	// Mouse button is pressed down on a LI
    	{
    		if(document.all)e = event;
    		var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
    		var sl = Math.max(document.body.scrollLeft,document.documentElement.scrollLeft);
     
    		dragTimer = 0;
    		dragContentObj.style.left = e.clientX + sl + 'px';
    		dragContentObj.style.top = e.clientY + st + 'px';
    		contentToBeDragged = this;
    		contentToBeDragged_src = this.parentNode;
    		contentToBeDragged_next = false;
    		if(this.nextSibling){
    			contentToBeDragged_next = this.nextSibling;
    			if(!this.tagName && contentToBeDragged_next.nextSibling)contentToBeDragged_next = contentToBeDragged_next.nextSibling;
    		}
    		timerDrag();
    		return false;
    	}
     
    	function timerDrag()
    	{
    		if(dragTimer>=0 && dragTimer<10){
    			dragTimer++;
    			setTimeout('timerDrag()',10);
    			return;
    		}
    		if(dragTimer==10){
     
    			if(cloneSourceItems && contentToBeDragged.parentNode.id=='allItems'){
    				newItem = contentToBeDragged.cloneNode(true);
    				newItem.onmousedown = contentToBeDragged.onmousedown;
    				contentToBeDragged = newItem;
    			}
    			dragContentObj.style.display='block';
    			dragContentObj.appendChild(contentToBeDragged);
    		}
    	}
     
    	function moveDragContent(e)
    	{
    		if(dragTimer<10){
    			if(contentToBeDragged){
    				if(contentToBeDragged_next){
    					contentToBeDragged_src.insertBefore(contentToBeDragged,contentToBeDragged_next);
    				}else{
    					contentToBeDragged_src.appendChild(contentToBeDragged);
    				}
    			}
    			return;
    		}
    		if(document.all)e = event;
    		var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
    		var sl = Math.max(document.body.scrollLeft,document.documentElement.scrollLeft);
     
     
    		dragContentObj.style.left = e.clientX + sl + 'px';
    		dragContentObj.style.top = e.clientY + st + 'px';
     
    		if(mouseoverObj)mouseoverObj.className='';
    		destinationObj = false;
    		dragDropIndicator.style.display='none';
    		if(indicateDestinationBox)indicateDestinationBox.style.display='none';
    		var x = e.clientX + sl;
    		var y = e.clientY + st;
    		var width = dragContentObj.offsetWidth;
    		var height = dragContentObj.offsetHeight;
     
    		var tmpOffsetX = arrow_offsetX;
    		var tmpOffsetY = arrow_offsetY;
     
    		for(var no=0;no<ulPositionArray.length;no++){
    			var ul_leftPos = ulPositionArray[no]['left'];
    			var ul_topPos = ulPositionArray[no]['top'];
    			var ul_height = ulPositionArray[no]['height'];
    			var ul_width = ulPositionArray[no]['width'];
     
    			if((x+width) > ul_leftPos && x<(ul_leftPos + ul_width) && (y+height)> ul_topPos && y<(ul_topPos + ul_height)){
    				var noExisting = ulPositionArray[no]['obj'].getElementsByTagName('LI').length;
    				if(indicateDestinationBox && indicateDestinationBox.parentNode==ulPositionArray[no]['obj'])noExisting--;
    				if(noExisting<boxSizeArray[no-1] || no==0){
    					dragDropIndicator.style.left = ul_leftPos + tmpOffsetX + 'px';
    					var subLi = ulPositionArray[no]['obj'].getElementsByTagName('LI');
     
    					var clonedItemAllreadyAdded = false;
    					if(cloneSourceItems && !cloneAllowDuplicates){
    						for(var liIndex=0;liIndex<subLi.length;liIndex++){
    							if(contentToBeDragged.id == subLi[liIndex].id)clonedItemAllreadyAdded = true;
    						}
    						if(clonedItemAllreadyAdded)continue;
    					}
     
    					for(var liIndex=0;liIndex<subLi.length;liIndex++){
    						var tmpTop = getTopPos(subLi[liIndex]);
    						if(!indicateDestionationByUseOfArrow){
    							if(y<tmpTop){
    								destinationObj = subLi[liIndex];
    								indicateDestinationBox.style.display='block';
    								subLi[liIndex].parentNode.insertBefore(indicateDestinationBox,subLi[liIndex]);
    								break;
    							}
    						}else{
    							if(y<tmpTop){
    								destinationObj = subLi[liIndex];
    								dragDropIndicator.style.top = tmpTop + tmpOffsetY - Math.round(dragDropIndicator.clientHeight/2) + 'px';
    								dragDropIndicator.style.display='block';
    								break;
    							}
    						}
    					}
     
    					if(!indicateDestionationByUseOfArrow){
    						if(indicateDestinationBox.style.display=='none'){
    							indicateDestinationBox.style.display='block';
    							ulPositionArray[no]['obj'].appendChild(indicateDestinationBox);
    						}
     
    					}else{
    						if(subLi.length>0 && dragDropIndicator.style.display=='none'){
    							dragDropIndicator.style.top = getTopPos(subLi[subLi.length-1]) + subLi[subLi.length-1].offsetHeight + tmpOffsetY + 'px';
    							dragDropIndicator.style.display='block';
    						}
    						if(subLi.length==0){
    							dragDropIndicator.style.top = ul_topPos + arrow_offsetY + 'px'
    							dragDropIndicator.style.display='block';
    						}
    					}
     
    					if(!destinationObj)destinationObj = ulPositionArray[no]['obj'];
    					mouseoverObj = ulPositionArray[no]['obj'].parentNode;
    					mouseoverObj.className='mouseover';
    					return;
    				}
    			}
    		}
    	}
     
    	/* End dragging
    	Put <LI> into a destination or back to where it came from.
    	*/
    	function dragDropEnd(e)
    	{
    		if(dragTimer==-1)return;
    		if(dragTimer<10){
    			dragTimer = -1;
    			return;
    		}
    		dragTimer = -1;
    		if(document.all)e = event;
     
     
    		if(cloneSourceItems && (!destinationObj || (destinationObj && (destinationObj.id=='allItems' || destinationObj.parentNode.id=='allItems')))){
    			contentToBeDragged.parentNode.removeChild(contentToBeDragged);
    		}else{
     
    			if(destinationObj){
    				if(destinationObj.tagName=='UL'){
    					destinationObj.appendChild(contentToBeDragged);
    				}else{
    					destinationObj.parentNode.insertBefore(contentToBeDragged,destinationObj);
    				}
    				mouseoverObj.className='';
    				destinationObj = false;
    				dragDropIndicator.style.display='none';
    				if(indicateDestinationBox){
    					indicateDestinationBox.style.display='none';
    					document.body.appendChild(indicateDestinationBox);
    				}
    				contentToBeDragged = false;
    				return;
    			}
    			if(contentToBeDragged_next){
    				contentToBeDragged_src.insertBefore(contentToBeDragged,contentToBeDragged_next);
    			}else{
    				contentToBeDragged_src.appendChild(contentToBeDragged);
    			}
    		}
    		contentToBeDragged = false;
    		dragDropIndicator.style.display='none';
    		if(indicateDestinationBox){
    			indicateDestinationBox.style.display='none';
    			document.body.appendChild(indicateDestinationBox);
     
    		}
    		mouseoverObj = false;
     
    	}
     
    	/*
    	Preparing data to be saved
    	*/
    	function saveDragDropNodes()
    	{
    		var saveString = "";
    		var uls = dragDropTopContainer.getElementsByTagName('UL');
    		for(var no=0;no<uls.length;no++){	// LOoping through all <ul>
    			var lis = uls[no].getElementsByTagName('LI');
    			for(var no2=0;no2<lis.length;no2++){
    				if(saveString.length>0)saveString = saveString + ";";
    				saveString = saveString + uls[no].id + '|' + lis[no2].id;
                                    //document.write('=' + saveString)
    				window.location.href = "yourpage.php?var=" + saveString;
     
    			}
    		}
     
    		//document.getElementById('saveContent').innerHTML = '<h1>Ready to save these nodes:</h1> ' + saveString.replace(/;/g,';<br>') + '<p>Format: ID of ul |(pipe) ID of li;(semicolon)</p><p>You can put these values into a hidden form fields, post it to the server and explode the submitted value there</p>';
     
    	}
     
     
    	function initDragDropScript()
    	{
    		dragContentObj = document.getElementById('dragContent');
    		dragDropIndicator = document.getElementById('dragDropIndicator');
    		dragDropTopContainer = document.getElementById('dhtmlgoodies_dragDropContainer');
    		document.documentElement.onselectstart = cancelEvent;;
    		var listItems = dragDropTopContainer.getElementsByTagName('LI');	// Get array containing all <LI>
    		var itemHeight = false;
    		for(var no=0;no<listItems.length;no++){
    			listItems[no].onmousedown = initDrag;
    			listItems[no].onselectstart = cancelEvent;
    			if(!itemHeight)itemHeight = listItems[no].offsetHeight;
    			if(MSIE && navigatorVersion/1<6){
    				listItems[no].style.cursor='hand';
    			}
    		}
     
    		var mainContainer = document.getElementById('dhtmlgoodies_mainContainer');
    		var uls = mainContainer.getElementsByTagName('UL');
    		itemHeight = itemHeight + verticalSpaceBetweenListItems;
    		for(var no=0;no<uls.length;no++){
    			uls[no].style.height = itemHeight * boxSizeArray[no]  + 'px';
    		}
     
    		var leftContainer = document.getElementById('dhtmlgoodies_listOfItems');
    		var itemBox = leftContainer.getElementsByTagName('UL')[0];
     
    		document.documentElement.onmousemove = moveDragContent;	// Mouse move event - moving draggable div
    		document.documentElement.onmouseup = dragDropEnd;	// Mouse move event - moving draggable div
     
    		var ulArray = dragDropTopContainer.getElementsByTagName('UL');
    		for(var no=0;no<ulArray.length;no++){
    			ulPositionArray[no] = new Array();
    			ulPositionArray[no]['left'] = getLeftPos(ulArray[no]);
    			ulPositionArray[no]['top'] = getTopPos(ulArray[no]);
    			ulPositionArray[no]['width'] = ulArray[no].offsetWidth;
    			ulPositionArray[no]['height'] = ulArray[no].clientHeight;
    			ulPositionArray[no]['obj'] = ulArray[no];
    		}
     
    		if(!indicateDestionationByUseOfArrow){
    			indicateDestinationBox = document.createElement('LI');
    			indicateDestinationBox.id = 'indicateDestination';
    			indicateDestinationBox.style.display='none';
    			document.body.appendChild(indicateDestinationBox);
     
     
    		}
    	}
     
     
    	</script>

  2. #2
    Membre à l'essai
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 9
    Points : 12
    Points
    12
    Par défaut
    Tu recherches peut-être la propriété scrollLeft ?

    https://developer.mozilla.org/fr/doc...ent.scrollLeft

  3. #3
    Membre habitué
    Profil pro
    Inscrit en
    Décembre 2007
    Messages
    478
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Décembre 2007
    Messages : 478
    Points : 189
    Points
    189
    Par défaut
    Merci! je vais pouvoir me debrouiller avec scrollLeft.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Détecter le scroll d'une div
    Par etienne31 dans le forum jQuery
    Réponses: 8
    Dernier message: 01/06/2015, 20h45
  2. Relier le scroll de la fenêtre au scroll d'une div
    Par Ceubex dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 20/07/2013, 00h38
  3. Scrolling dans une zone <div>
    Par Commodore dans le forum Langage
    Réponses: 3
    Dernier message: 12/07/2006, 10h09
  4. Déplacement dans une div scrollable
    Par kankrelune dans le forum Général JavaScript
    Réponses: 10
    Dernier message: 30/05/2006, 21h52
  5. Connaitre la height d'une div
    Par licorne dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 07/09/2005, 16h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo