隨筆 - 28, 文章 - 0, 評(píng)論 - 3, 引用 - 0
          數(shù)據(jù)加載中……

          DIV+CSS結(jié)合js控制頁(yè)面滾動(dòng)的效果(轉(zhuǎn))

          DIV+CSS結(jié)合js控制頁(yè)面滾動(dòng)的效果,非常實(shí)用,兼容ie,ff。
              

          <head>
          <style type="text/css">
            #container{
            text-align: left;
            background-color: #faf7ec;
            width: 500px;
            margin: 20px auto 0 auto;
            padding: 0;
            font-family:Arial, Helvetica, sans-serif;
            font-size:12px;
           }
          #block0, #block1, #block2, #block3, #block4, #block5
          {
            border-top: solid 1px #785a3c;
            margin: 0;
            padding: 10px;
          }
          .active {  background-color: #fff;}
          .visited {  background-color: #ede7da;}
          #block0, #block0.active, #block0.visited
          {
            text-align: center;
            background-color: #a0dcf8;
            border-top: none;
            border-bottom: solid 4px #785a3c;
          }
          </style>

          <script>
          var ScrollWin = {
            w3c : document.getElementById,
            iex : document.all,
            scrollLoop : false,
            scrollInterval : null, // setInterval id
            currentBlock : null,   // object reference
            getWindowHeight : function(){
              if(this.iex) return (document.documentElement.clientHeight) ?
          document.documentElement.clientHeight : document.body.clientHeight;
              else return window.innerHeight;
            },
            getScrollLeft : function(){
              if(this.iex) return (document.documentElement.scrollLeft) ?
          document.documentElement.scrollLeft : document.body.scrollLeft;
              else return window.pageXOffset;
            },
            getScrollTop : function(){
              if(this.iex) return (document.documentElement.scrollTop) ?
          document.documentElement.scrollTop : document.body.scrollTop;
              else return window.pageYOffset;
            },
            getElementYpos : function(el){
              var y = 0;
              while(el.offsetParent){
                y += el.offsetTop
                el = el.offsetParent;
              }
              return y;
            },
            scroll : function(num){
              if(!this.w3c){
                location.href = "#"+this.anchorName+num;
                return;
              }
              if(this.scrollLoop){
                clearInterval(this.scrollInterval);
                this.scrollLoop = false;
                this.scrollInterval = null;
              }
              if(this.currentBlock != null) this.currentBlock.className = this.offClassName;
              this.currentBlock = document.getElementById(this.blockName+num);
              this.currentBlock.className = this.onClassName;
              var doc = document.getElementById(this.containerName);
              var documentHeight = this.getElementYpos(doc) + doc.offsetHeight;
              var windowHeight = this.getWindowHeight();
              var ypos = this.getElementYpos(this.currentBlock);
              if(ypos > documentHeight - windowHeight) ypos = documentHeight - windowHeight;
              this.scrollTo(0,ypos);
            },
            scrollTo : function(x,y){
              if(this.scrollLoop){
                var left = this.getScrollLeft();
                var top = this.getScrollTop();
                if(Math.abs(left-x) <= 1 && Math.abs(top-y) <= 1){
                  window.scrollTo(x,y);
                  clearInterval(this.scrollInterval);
                  this.scrollLoop = false;
                  this.scrollInterval = null;
                }else{
                  window.scrollTo(left+(x-left)/10, top+(y-top)/10);  
                }
              }else{
                this.scrollInterval = setInterval("ScrollWin.scrollTo("+x+","+y+")",20);   
                this.scrollLoop = true;
              }
            }
          };
          ScrollWin.containerName = "container";
          ScrollWin.anchorName    = "anchor";  
          ScrollWin.blockName     = "block";    
          ScrollWin.onClassName   = "active";   
          ScrollWin.offClassName  = "visited";  
          </script>
          </head>


          <body>
          <div align="center">
          <div id="container">
            <a name="anchor0"></a>
            <div id="block0">
              <a href="javascript:ScrollWin.scroll('1')">鏈接 1</a> |
              <a href="javascript:ScrollWin.scroll('2')">鏈接 2</a> |
              <a href="javascript:ScrollWin.scroll('3')">鏈接 3</a> |
              <a href="javascript:ScrollWin.scroll('4')">鏈接 4</a> |
              <a href="javascript:ScrollWin.scroll('5')">鏈接 5</a>
            </div>
            <a name="anchor1"></a>
            <div id="block1">
              <h3><a href="#" onclick="javascript:ScrollWin.scroll('0'); return false;">頂部</a>
            鏈接 1</h3>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
            </div>
            <a name="anchor2"></a>
            <div id="block2">
              <h3><a href="#" onclick="javascript:ScrollWin.scroll('0'); return false;">頂部</a>
            鏈接 2</h3>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
            </div>
            <a name="anchor3"></a>
            <div id="block3">
              <h3><a href="#" onclick="javascript:ScrollWin.scroll('0'); return false;">頂部</a>
            鏈接 3</h3>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
            </div>
            <a name="anchor4"></a>
            <div id="block4">
              <h3><a href="#" onclick="javascript:ScrollWin.scroll('0'); return false;">頂部</a>
            鏈接 4</h3>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
            </div>
            <a name="anchor5"></a>
            <div id="block5">
              <h3><a href="#" onclick="javascript:ScrollWin.scroll('0'); return false;">頂部</a>
            鏈接 5</h3>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
              <p>內(nèi)容</p>
            </div>
          </div>
          </div>
          </body>

          posted on 2010-01-22 17:25 Angle Wang 閱讀(1640) 評(píng)論(2)  編輯  收藏 所屬分類(lèi): CSS

          評(píng)論

          # re: DIV+CSS結(jié)合js控制頁(yè)面滾動(dòng)的效果(轉(zhuǎn))  回復(fù)  更多評(píng)論   

          體驗(yàn)不是很好
          2010-06-04 11:06 | s

          # re: DIV+CSS結(jié)合js控制頁(yè)面滾動(dòng)的效果(轉(zhuǎn))[未登錄](méi)  回復(fù)  更多評(píng)論   

          不錯(cuò)的文章,正好需要用到這種效果,謝謝博主分享
          2012-12-04 14:55 | eric
          主站蜘蛛池模板: 衡水市| 德保县| 化德县| 蒙阴县| 烟台市| 板桥市| 东阿县| 芷江| 鄱阳县| 双流县| 贡觉县| 静乐县| 巨野县| 焦作市| 浑源县| 龙海市| 万安县| 兴义市| 临桂县| 桂平市| 潼南县| 新郑市| 临海市| 高清| 射洪县| 栖霞市| 望江县| 达尔| 枣阳市| 定兴县| 确山县| 育儿| 赤壁市| 绿春县| 滦南县| 四平市| 潍坊市| 洪雅县| 三穗县| 静宁县| 佛学|