私は現在、スクロールの位置を特定のcssクラスに調整するためのJavaScript(たとえば、Jquery lib)ソリューションを探しています。目標は、最も近いコンテナにスクロールバーを再配置することです。
このウェブサイトとして:http ://www.takumitaniguchi.com/tokyoblue/
JUJUありがとう
私は現在、スクロールの位置を特定のcssクラスに調整するためのJavaScript(たとえば、Jquery lib)ソリューションを探しています。目標は、最も近いコンテナにスクロールバーを再配置することです。
このウェブサイトとして:http ://www.takumitaniguchi.com/tokyoblue/
JUJUありがとう
JQueryを使用する場合、質問を正しく理解していれば、そのようなことは比較的簡単に実現できます。これが私がそれを行う方法を大まかに示しています:
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);
}
}
これは最善の方法ではないかもしれませんが、機能するはずです。関数を呼び出して、最初の引数としてJQueryの選択を渡すだけです。