На прошлой неделе мы только что выпустили новую версию нашего веб-сайта с прокруткой Javascript.
Вы можете увидеть его в прямом эфире на http://beebole.com, но я извлек код для базового примера ниже:
<html lang="en">
<head>
<title>BeeBole - Simple and Fast Timesheets</title>
<style>
body{ padding:0 1em; margin:0;}
ul{ padding:0;margin:0;}
li{ list-style:none; float:left; margin:0 1em;}
h1{ clear:both;}
</style>
</head>
<body>
<a id="home" name="home"></a>
<div class="header">
<ul class="menu">
<li><a class="fr current" href="#home" onclick="return beebole.scrollTo(this)">Home</a>
<li><a class="fr" href="#pricing" onclick="return beebole.scrollTo(this)">Pricing</a>
<li><a class="fr" href="#tour" onclick="return beebole.scrollTo(this)">Tour</a>
</ul>
</div>
<div class="chapter home">
<h1>Home</h1>
<p>Lorem ipsum dolor sit ... in feugiat massa congue in.</p>
</div>
<a id="pricing" name="pricing"></a>
<div class="header">
<ul class="menu">
<li><a class="fr" href="#home" onclick="return beebole.scrollTo(this)">Home</a>
<li><a class="current fr" href="#pricing" onclick="return beebole.scrollTo(this)">Pricing</a>
<li><a class="fr" href="#tour" onclick="return beebole.scrollTo(this)">Tour</a>
</ul>
</div>
<div class="chapter pricing">
<h1>Pricing</h1>
<p>Lorem ipsum dolor sit ... in feugiat massa congue in.</p>
<p>Lorem ipsum dolor sit ... in feugiat massa congue in.</p>
</div>
<a id="tour" name="tour"></a>
<div class="header">
<ul class="menu">
<li><a class="fr" href="#home" onclick="return beebole.scrollTo(this)">Home</a>
<li><a class="fr" href="#pricing" onclick="return beebole.scrollTo(this)">Pricing</a>
<li><a class="current fr" href="#tour" onclick="return beebole.scrollTo(this)">Tour</a>
</ul>
</div>
<div class="chapter tour">
<h1>Take a tour</h1>
<p>Lorem ipsum dolor sit ... in feugiat massa congue in.</p>
<p>Lorem ipsum dolor sit ... in feugiat massa congue in.</p>
<p>Lorem ipsum dolor sit ... in feugiat massa congue in.</p>
</div>
<script>
(function(){
window.beebole = {
getPos: function( elm ){
var x = 0, y = 0;
while( elm != null ) {
x += elm.offsetLeft;
y += elm.offsetTop;
elm = elm.offsetParent ;
}
return {x:x, y:y};
},
damper:function(rfnAction, rfnDone, duration){
var interval,
startTime = new Date().getTime();
interval = setInterval( function(){
var pos,
t,
now = new Date().getTime();
t = now - startTime;
if(t >= duration){
clearInterval(interval);
rfnDone.call(beebole);
}else{
t = 2 * t / duration;
if (t < 1) {
pos = 0.5*t*t*t*t;
}else{
t -= 2;
pos = -0.5 * (t*t*t*t - 2);
}
rfnAction.call(beebole, pos);
}
}, 15 );
},
scrollTo: function( a ){
try{
var endName = a.href.split('#')[1],
endElm = document.getElementById(endName),
start = isNaN(window.pageYOffset) ?
document.documentElement.scrollTop :
window.pageYOffset,
end = beebole.getPos(endElm).y,
length = beebole.getPos(endElm).y - start;
this.damper(function(pos){
//when moving
window.scrollTo(0, start + length * pos);
}, function(){
//when done
window.location.hash = endName;
},
//duration
Math.max( Math.abs( Math.round(length / 3) ), 1200));
return false;
}catch(e){
return true;
}
}
};
})();
</script>
</body>
</html>
Либо добавьте больше контента там, где lorem ipsum
. Или сделайте окно браузера очень маленьким, чтобы иметь прокрутку.
Нажмите на ссылки, чтобы увидеть перемещение страницы.
Если в браузере отключен Javascript, браузер возьмет на себя прокрутку с помощью клавиши # по умолчанию. Но, очевидно, без эффекта демпфирования.
Он не был протестирован на IE6 и IE7.