Ajuster le positionnement du scroll sur un contenu
Bonjour la communauté,
Je suis actuellement sur le développement d'un site internet comprenant plusieurs éléments JavaScript. Dans ce cadre, je souhaiterais ajuster le positionnement de la scrollbar en fonction du container le plus proche.
Modèle opérationnel : http://www.takumitaniguchi.com/tokyoblue/
Mon code javascript qui est dans un fichier scrolladjust.js appelé sur le bas de la page via [<script type="text/javascript" src="js/scrolladjust.js"></script>] :
Code:
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
| function scrollTo(a){
//Get current scroll position
var current = (document.all ? document.scrollTop : window.pageYOffset);
//Define variables for data collection
var target = undefined;
var targetpos = undefined;
var dif = 0;
//check each selected element to see witch is closest
$(a).each(function(){
//Save the position of the element to avoid repeated property lookup
var t = $(this).position().top;
//check if there is an element to check against
if (target != undefined){
//save the difference between the current element's position and the current scroll position to avoid repeated calculations
var tdif = Math.abs(current - t);
//check if its closer than the selected element
if(tdif < dif){
//set the current element to be selected
target = this;
targetpos = t;
dif = tdif;
}
} else {
//set the current element to be selected
target = this;
targetpos = t;
dif = Math.abs(current - t);
}
});
//check if an element has been selected
if (target != undefined){
//animate scroll to the elements position
$('html,body').animate({scrollTop: targetpos}, 2000);
}
} |
Mais cela ne marche pas, quelqu'un aurait une idée ?
Merci d'avance !
Sean.