日期:2014-05-16  浏览次数:20419 次

jquery 怎样实现div 的滚动条自动滚动?
问题
  在div中有一张很长的图片,宽度有 12000 ,我想让这div 的 滚动条自动滚动.这javaScript不知该怎写,
  还请各位路过的高手指点...^_^

------解决方案--------------------
HTML code

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>
        <style>
            div {
                width:100px; height:100px;
                overflow:scroll;
            }
        </style>
    </head>
    <body>
        <div id="t">
            <img src="http://avatar.profile.csdn.net/1/D/5/2_fnsx_wfs.jpg" width="1200" />
        </div>
        <script>
            var $ = function(id){
                return document.getElementById(id);
            };
            var obj = $('t');
            var i = 1;
            setInterval(function(){
                obj.scrollLeft = i
                obj.scrollTop = i
                i += 5;
            }, 30)
        </script>
    </body>
</html>

------解决方案--------------------
HTML code
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
var scrollLeft = 0;
function autoScroll(i) {
    var o = $("#demo");
    if (o.scrollLeft() == $("#demo img").eq(0).width - 200) clearInterval(t);
    else {
        scrollLeft += i;
        o.scrollLeft(scrollLeft);
    }
}
$(document).ready( function() {
    var t = window.setInterval('autoScroll(1)', 10);
});
</script>
</head>

<body>
<style type="text/css">
#demo {
    width:200px;
    overflow:scroll;
}
</style>
<div id="demo"><img src="http://pic16.nipic.com/20110909/2238196_094518206167_2.jpg" /></div>
<span id="scrollLert"></span>
</body>
</html>