js楼层跳跃
css
* {
margin: 0px;
padding: 0px;
list-style: none;
}
body,
html {
height: 100%;
}
ul {
height: 100%;
}
ul li {
height: 100%;
}
ol {
position: fixed;
top: 100px;
left: 30px;
}
ol li {
width: 50px;
height: 30px;
border: 1px solid #aaa;
margin-top: 10px;
line-height: 30px;
text-align: center;
cursor: pointer;
}
HTML
<body>
<ul>
<li>袜子</li>
<li>裤子</li>
<li>鞋子</li>
<li>帽子</li>
<li>包包</li>
</ul>
<ol>
<li>袜子</li>
<li>裤子</li>
<li>鞋子</li>
<li>帽子</li>
<li>包包</li>
</ol>
</body>
js
var timer;
var target;
var leader = 0;
var ul = document.getElementsByTagName("ul")[0];
var ol = document.getElementsByTagName("ol")[0];
var ulLi = ul.children;
var olLi = ol.children;
var arrColor = ["pink", "blue", "green", "red", "yellow"];
for (var i = 0; i < ulLi.length; i++) {
ulLi[i].style.background = arrColor[i];
olLi[i].style.background = arrColor[i];
olLi[i].index = i;
olLi[i].onclick = function () {
clearInterval(timer);
target = ulLi[this.index].offsetTop;
timer = setInterval(function () {
var step = (target - leader) / 10;
step = step > 0 ? Math.ceil(step) : Math.floor(step);
leader = leader + step;
window.scrollTo(0, leader);
if (Math.abs(target - leader) <= Math.abs(step)) {
window.scrollTo(0, target);
clearInterval(timer);
}
}, 30);
}
}
window.onscroll = function () {
leader = scroll().top;
}
function scroll() {
return {
"top": window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop,
"left": window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft
};
}