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

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

          DIV+CSS結(jié)合js控制頁面滾動的效果,非常實(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 閱讀(1642) 評論(2)  編輯  收藏 所屬分類: CSS

          評論

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

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

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

          不錯的文章,正好需要用到這種效果,謝謝博主分享
          2012-12-04 14:55 | eric
          主站蜘蛛池模板: 黄山市| 芦溪县| 万安县| 东港市| 鄯善县| 融水| 金阳县| 山阴县| 应城市| 普定县| 米林县| 富蕴县| 时尚| 西城区| 天水市| 甘孜| 古浪县| 三穗县| 瓮安县| 老河口市| 达州市| 邛崃市| 平顶山市| 吉首市| 桐乡市| 边坝县| 宝丰县| 巨鹿县| 永胜县| 临泉县| 佛坪县| 岳西县| 武山县| 宿州市| 桂阳县| 富平县| 金昌市| 且末县| 玉田县| 桦川县| 余姚市|