刚做的,IE Only
FF兼容未做,滚到底就不动了。有兴趣的帮写下兼容代码,感激不尽
<html>
<head>
    <title></title>
    <script language="javascript" type="text/javascript">
        function VScrollPanel() {
            this.width = null;
            this.height = null;
            this.delay = 100;
            this.step = 1;
        }
        VScrollPanel.prototype = {
            Bind: function (domID) {
                this.dom = document.getElementById(domID);
                if (this.width) {
                    this.dom.style.width = this.width + "px";
                }
                if (this.height) {
                    this.dom.style.height = this.height + "px";
                }
                this.dom.style.overflow = "hidden";                this.Scrolling();
            },
            Scrolling: function () {
                if (this.dom.scrollHeight <= this.dom.clientHeight) return;
                if (this.willSwap) {
                    this.dom.appendChild(this.lastOne);
                    this.willSwap = false;
                }
                var _this = this;
                this.dom.scrollTop += this.step;
                if (this.dom.scrollTop + this.dom.clientHeight
>= this.dom.scrollHeight) {//到达底部
                    if (this.tmp != null) this.dom.removeChild(this.tmp);
                    this.lastOne = this.dom.firstChild;
                    this.tmp = this.lastOne.cloneNode(true);
                    this.lastOne.replaceNode(this.tmp);
                    this.willSwap = true;
                }
                setTimeout(function () { _this.Scrolling(); }, this.delay);
            },
            Pause: function () {
                this.pauseStep = this.step;
                this.step = 0;
            },
            Continue: function () {
                this.step = this.pauseStep;
            }
        };        /*经过多次实验,水平滚动通过新建一个表格来设置布局
        才能最有效的保证布局不会乱,不会发生换行
        所以只好水平、垂直滚动的分开做了*/
        function HScrollPanel() {
            this.width = null;
            this.height = null;
            this.delay = 100;
            this.step = 1;
        }
        HScrollPanel.prototype = {
            Bind: function (domID) {
                this.dom = document.getElementById(domID);
                this.rootDom = document.createElement("table");
                this.rootDom.border = "0";
                this.rootDom.cellPadding = "0";
                this.rootDom.cellSpacing = "0";
                var tbody = document.createElement("tbody");
                this.rootDom.appendChild(tbody);
                this.row = document.createElement("tr");
                tbody.appendChild(this.row);                var child = this.dom.firstChild;
                while (child != null) {
                    this.dom.removeChild(child);
                    if (child.nodeType == 1) {
                        var td = document.createElement("td");
                        td.vAlign = "top";
                        td.appendChild(child);
                        this.row.appendChild(td);
                    }
                    child = this.dom.firstChild;
                }
                this.dom.appendChild(this.rootDom);                if (this.width) {
                    this.dom.style.width = this.width + "px";
                }
                if (this.height) {
                    this.dom.style.height = this.height + "px";
                }
                this.dom.style.overflow = "hidden";
                this.Scrolling();
            },
            Scrolling: function () {
                if (this.dom.scrollWidth <= this.dom.clientWidth) return;
                if (this.willSwap) {
                    this.row.appendChild(this.lastOne);
                    this.willSwap = false;
                }
                var _this = this;
                this.dom.scrollLeft += this.step;
                if (this.dom.scrollLeft + this.dom.clientWidth
>= this.dom.scrollWidth) {
                    if (this.tmp != null) {
                        this.row.removeChild(this.tmp);
                    }
                    this.lastOne = this.row.firstChild;
                    this.tmp = this.lastOne.cloneNode(true);
                    this.lastOne.replaceNode(this.tmp);
                    this.willSwap = true;
                }
                setTimeout(function () { _this.Scrolling(); }, this.delay);
            },
            Pause: function () {
                this.pauseStep = this.step;
                this.step = 0;
            },
            Continue: function () {
                this.step = this.pauseStep;
            }
        };
    </script>
    <style type="text/css">
        body,td
        {
            font-family:"宋体";
            font-size:12px;
        }
        .item
        {
            width:150px;
            background-color:#f0f0f0;
            text-align:center;
            margin:5px;
            padding:3px;
        }
    </style>
</head>
<body>
垂直滚动示例
<div id="divVScroll" onmouseover="vsp.Pause();" onmouseout="vsp.Continue()">
    <div class="item">AAAAAA</div>
    <div class="item">BBBBBB</div>
    <div class="item">CCCCCC</div>
    <div class="item">DDDDDD</div>
    <div class="item">EEEEEE</div>
    <div class="item">FFFFFF</div>
    <div class="item">GGGGGG</div>
</div>
    <script language="javascript" type="text/javascript">
        var vsp = new VScrollPanel();
        vsp.height = 100;
        vsp.Bind("divVScroll");
    </script>
<br /><br />
水平滚动示例
<div id="divHScroll" onmouseover="hsp.Pause();" onmouseout="hsp.Continue()">
    <div class="item">AAAAAA</div>
    <div class="item">BBBBBB</div>
    <div class="item">CCCCCC</div>
    <div class="item">DDDDDD</div>
    <div class="item">EEEEEE</div>
    <div class="item">FFFFFF</div>
    <div class="item">GGGGGG</div>
</div>
    <script language="javascript" type="text/javascript">
        var hsp = new HScrollPanel();
        hsp.width = 400;
        hsp.delay = 10;
        hsp.Bind("divHScroll");
    </script>
</body>
</html>

解决方案 »

  1.   

    代码这么长啊 
    <script language="javascript" type="text/javascript">
    <!--
    $("temp").innerHTML = $("main").innerHTML;
    function Marquee(){

    if($("temp").offsetTop-$("main").scrollTop<=0)    
    $("main").scrollTop=0;    
        else{
         $("main").scrollTop++;
        }
    }
    var ics=setInterval("Marquee()",20);
    $("content").onmouseover = function(){clearInterval(ics);}
    $("content").onmouseout = function(){ics=setInterval("Marquee()",20);}
    -->
    </script> 
      

  2.   

    用JS写出来的,再长都是本事,用jq写出来的,再短都是利用。以前用JS写过一个兼容的,但好久没用,搞掉了
      

  3.   

    现在写JS能尽可能把代码写少,效率写高,兼容性写强就是好!如果遇到要封装的,能封,那个技术就已经很不错了,不管你是写什么哪怕是JQUERY写的,也得有那真本事啊,不了解原理用JQUERY也不可能写得出来,当然如果能外加完美注释那就更强大了。
      

  4.   

    function $(obj)
    {
      return document.getElementById(obj);
    }
    这也能算是利用JQuery?
      

  5.   

    楼上的代码确实很短,但不知道能不能达到无缝的要求:最后一项滚动出来以后,紧接着滚动出现的是第一项,而不是跳回到第一项。<marquee>这样最短</marquee>
      

  6.   

    <html>
    <head>
        <title></title>
        <script language="javascript" type="text/javascript">
            function VScrollPanel() {
                this.width = null; //滚动范围宽度
                this.height = null; //滚动范围高度
                this.delay = 100;  //时间延迟
                this.step = 1;  //单步滚动像素
            }
            VScrollPanel.prototype = {
                //滚动指定ID的内容
                Bind: function (domID) {
                    this.dom = document.getElementById(domID);
                    if (this.width) {
                        this.dom.style.width = this.width + "px";
                    }
                    if (this.height) {
                        this.dom.style.height = this.height + "px";
                    }
                    this.dom.style.overflow = "hidden";                this.Scrolling();
                },
                //滚动方法
                Scrolling: function () {
                    //如果内容太少,已经完全可以显示,不需滚动
                    if (this.dom.scrollHeight <= this.dom.clientHeight) return;
                    
                    //滚动到最后一项,切换位置
                    if (this.willSwap) {
                        this.dom.appendChild(this.lastOne);
                        this.willSwap = false;
                    }
                    
                    //一个对象自身的引用
                    var _this = this;
                    
                    if (this.dom.scrollTop+ this.step + this.dom.clientHeight
    >= this.dom.scrollHeight) {
    //滚动到达底部

    //删除临时添加的首项
                        if (this.tmp != null) this.dom.removeChild(this.tmp);
                        
                        //复制第一项,即将移至最后一项
                        this.lastOne = this.dom.firstChild;
                        this.tmp = this.lastOne.cloneNode(true);
                        
                        //添加第一项
                        //this.lastOne.replaceNode(this.tmp);
                        //在此用replaceChild替代replaceNode即可兼容FF
                        this.dom.replaceChild(this.tmp,this.lastOne);
                        /*至于为什么要复制第一项,并临时插入
                        是因为如果在滚动内容刚好足够滚动时,如果没有这个临时项,
                        直接移动第一项到最后一项的时候,会发生跳动现象。
                        也就是,比如:如果有4项,不会发生滚动,而如果有5项的时候,就发生这种现象
                        但6项以上的时候,正常*/
                        
                        this.willSwap = true;
                    }
                    this.dom.scrollTop += this.step;
                    setTimeout(function () {
                            /*此处必须用 _this 代替 this 否则错误,原因在此不阐述*/
                            _this.Scrolling();
                        }, this.delay);
                },
                Pause: function () {
                    this.pauseStep = this.step;
                    this.step = 0;
                },
                Continue: function () {
                    this.step = this.pauseStep;
                }
            };        /*经过多次实验,水平滚动通过新建一个表格来设置布局
            才能最有效的保证布局不会乱,不会发生换行
            所以只好水平、垂直滚动的分开做了*/
            
            //水平滚动。以下代码思路同上,不再注释
            function HScrollPanel() {
                this.width = null;
                this.height = null;
                this.delay = 100;
                this.step = 1;
            }
            HScrollPanel.prototype = {
                Bind: function (domID) {
                    this.dom = document.getElementById(domID);
                    this.rootDom = document.createElement("table");
                    this.rootDom.border = "0";
                    this.rootDom.cellPadding = "0";
                    this.rootDom.cellSpacing = "0";
                    var tbody = document.createElement("tbody");
                    this.rootDom.appendChild(tbody);
                    this.row = document.createElement("tr");
                    tbody.appendChild(this.row);                var child = this.dom.firstChild;
                    while (child != null) {
                        this.dom.removeChild(child);
                        if (child.nodeType == 1) {
                            var td = document.createElement("td");
                            td.vAlign = "top";
                            td.appendChild(child);
                            this.row.appendChild(td);
                        }
                        child = this.dom.firstChild;
                    }
                    this.dom.appendChild(this.rootDom);                if (this.width) {
                        this.dom.style.width = this.width + "px";
                    }
                    if (this.height) {
                        this.dom.style.height = this.height + "px";
                    }
                    this.dom.style.overflow = "hidden";
                    this.Scrolling();
                },
                Scrolling: function () {
                    if (this.dom.scrollWidth <= this.dom.clientWidth) return;
                    if (this.willSwap) {
                        this.row.appendChild(this.lastOne);
                        this.willSwap = false;
                    }
                    var _this = this;
                    if (this.dom.scrollLeft + this.step + this.dom.clientWidth
    >= this.dom.scrollWidth) {
                        if (this.tmp != null) {
                            this.row.removeChild(this.tmp);
                        }
                        this.lastOne = this.row.firstChild;
                        this.tmp = this.lastOne.cloneNode(true);
                        //this.lastOne.replaceNode(this.tmp);
                        this.row.replaceChild(this.tmp,this.lastOne);
                        this.willSwap = true;
                    }
                    this.dom.scrollLeft += this.step;
                    setTimeout(function () { _this.Scrolling(); }, this.delay);
                },
                Pause: function () {
                    this.pauseStep = this.step;
                    this.step = 0;
                },
                Continue: function () {
                    this.step = this.pauseStep;
                }
            };
        </script>
        <style type="text/css">
            body,td
            {
                font-family:"宋体";
                font-size:12px;
            }
            .item
            {
                width:150px;
                background-color:#f0f0f0;
                text-align:center;
                margin:5px;
                padding:3px;
            }
        </style>
    </head>
    <body>
    垂直滚动示例<input type="text" value="100" onchange="SetDelay(this);" />
    <div id="divVScroll" onmouseover="vsp.Pause();" onmouseout="vsp.Continue()">
        <div class="item">AAAAAA</div>
        <div class="item">BBBBBB</div>
        <div class="item">CCCCCC</div>
        <div class="item">DDDDDD</div>
        <div class="item">EEEEEE</div>
        <div class="item">FFFFFF</div>
        <div class="item">GGGGGG</div>
    </div>
        <script language="javascript" type="text/javascript">
            var vsp = new VScrollPanel();
            vsp.height = 100;
            vsp.Bind("divVScroll");
            function SetDelay(tb){
                vsp.delay=tb.value
            }
        </script>
    <br /><br />
    水平滚动示例
    <div id="divHScroll" onmouseover="hsp.Pause();" onmouseout="hsp.Continue()">
        <div class="item">AAAAAA</div>
        <div class="item">BBBBBB</div>
        <div class="item">CCCCCC</div>
        <div class="item">DDDDDD</div>
        <div class="item">EEEEEE</div>
        <div class="item">FFFFFF</div>
        <div class="item">GGGGGG</div>
    </div>
        <script language="javascript" type="text/javascript">
            var hsp = new HScrollPanel();
            hsp.width = 400;
            hsp.delay = 10;
            hsp.Bind("divHScroll");
        </script>
    </body>
    </html>
    FF兼容版本来了,并做了些修改,添加注释
      

  7.   

    程序要不停的clone,append,remove()好像有点不好,呵呵