あなたがスムーズバニラJSスクロールのためのいくつかのイージング関数をする必要があります。
Robert Pennerは私がHTML5のコースのためにいくつかの時間前にやったdemo on jsbinを持っ緩和男
です。ソースに興味があるかもしれません。上記の例で使用されるスクロール用
デモ機能:?正確に間違っている何
function scrollToItemId(containerId, srollToId) {
var scrollContainer = document.getElementById(containerId);
var item = document.getElementById(scrollToId);
//with animation
var from = scrollContainer.scrollTop;
var by = item.offsetTop - scrollContainer.scrollTop;
if (from < item.offsetTop) {
if (item.offsetTop > scrollContainer.scrollHeight - scrollContainer.clientHeight) {
by = (scrollContainer.scrollHeight - scrollContainer.clientHeight) - scrollContainer.scrollTop;
}
}
var currentIteration = 0;
/**
* get total iterations
* 60 -> requestAnimationFrame 60/second
* second parameter -> time in seconds for the animation
**/
var animIterations = Math.round(60 * 0.5);
(function scroll() {
var value = easeOutCubic(currentIteration, from, by, animIterations);
scrollContainer.scrollTop = value;
currentIteration++;
if (currentIteration < animIterations) {
requestAnimationFrame(scroll);
}
})();
//without animation
//scrollContainer.scrollTop = item.offsetTop;
}
//example easing functions
function linearEase(currentIteration, startValue, changeInValue, totalIterations) {
return changeInValue * currentIteration/totalIterations + startValue;
}
function easeOutCubic(currentIteration, startValue, changeInValue, totalIterations) {
return changeInValue * (Math.pow(currentIteration/totalIterations - 1, 3) + 1) + startValue;
}
はフィドルを提供します。 –
私はjQueryコードと同じ結果を達成したいと思いますが、バニラのJavascriptを使用しています。私はそれを達成する方法を説明することができません。 Javascriptをよく知っている人がコードを書くことができたら、私はそれを感謝します。 – NoName84