我創建了一個可以通過JavaScript滾動的頁面,但是在某些瀏覽器中,當您和頁面發送文字時,它不會非常順暢地滾動。在鉻合金中,它似乎沒有任何作用。使用javascript滾動HTML頁面?
我的問題是:
什麼是使用跨瀏覽器工作的JavaScript創建平滑滾動html頁面的最佳方法。
要了解我正在嘗試做什麼,下面是我製作的測試頁。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<title>test</title>
<style type="text/css">
html
{
overflow:hidden;
}
#fixedtop
{
padding:1%;
position:fixed;
float:left;
vertical-align:middle;
}
table.scollTable td
{
background-color:Gray;
height:12px;
width:12px;
}
table.scollTable td:hover
{
background-color:Lime;
height:20px;
width:20px;
}
.container
{
background:url(http://sstatic.net/so/img/logo.png) repeat;
height:5000px;
width:5000px;
}
</style>
</head>
<body>
<form name="form1" method="post" action="TestPage.aspx" id="form1">
<div>
</script>
<div id="fixedtop">
<table class="scollTable" border="0" cellpadding="0" cellspacing="0">
<tr>
<td onmouseover="scroll(-20,-20,this)"></td>
<td onmouseover="scroll(-10,-20,this)"></td>
<td onmouseover="scroll(0,-20,this)"></td>
<td onmouseover="scroll(10,-20,this)"></td>
<td onmouseover="scroll(20,-20,this)"></td>
</tr>
<tr>
<td onmouseover="scroll(-20,-10,this)"></td>
<td onmouseover="scroll(-10,-10,this)"></td>
<td onmouseover="scroll(0,-10,this)"></td>
<td onmouseover="scroll(10,-10,this)"></td>
<td onmouseover="scroll(20,-10,this)"></td>
</tr>
<tr>
<td onmouseover="scroll(-20,0,this)"></td>
<td onmouseover="scroll(-10,0,this)"></td>
<td></td>
<td onmouseover="scroll(10,0,this)"></td>
<td onmouseover="scroll(20,0,this)"></td>
</tr>
<tr>
<td onmouseover="scroll(-10,10,this)"></td>
<td onmouseover="scroll(-10,10,this)"></td>
<td onmouseover="scroll(0,10,this)"></td>
<td onmouseover="scroll(10,10,this)"></td>
<td onmouseover="scroll(10,10,this)"></td>
</tr>
<tr>
<td onmouseover="scroll(-20,20,this)"></td>
<td onmouseover="scroll(-10,20,this)"></td>
<td onmouseover="scroll(0,20,this)"></td>
<td onmouseover="scroll(10,20,this)"></td>
<td onmouseover="scroll(20,20,this)"></td>
</tr>
</table>
</div>
<div class="container"></div>
</form>
</body>
</html>
<script id="sTest" type="text/javascript" onload="test()">
function scroll(x,y, elem) {
var iScroll = setInterval(
function() {
SetScroll((x + GetXScroll()), (y + GetYScroll()))
}, 1);
elem.onmouseout = function() { clearInterval(iScroll) };
}
function GetYScroll() {
return window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
}
function GetXScroll() {
return window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft;
}
function SetScroll(x,y) {
if (document.body.scrollTop) {
document.body.scrollLeft = x;
document.body.scrollTop = y;
}
if(document.documentElement){
document.documentElement.scrollLeft = x;
document.documentElement.scrollTop = y;
}
if (window.pageYOffset) {
try {
window.pageXOffset = x;
window.pageYOffset = y;
} catch (e) { }
}
}
</script>
我的第一個想法之一是使用$()。animate();但它很緊張,並沒有在鉻合金工作。我將代碼發佈爲儘可能通用,因爲我期待有些例子,並且不希望將這篇文章的範圍縮小到jQuery。似乎每個人對js問題的回答是「只要使用jQuery」,然後再閱讀問題。我不是說不使用jQuery只是儘量不要這麼一般。 – 2009-10-23 14:22:51