javascript實現文字列表無縫向上滾動

來源:互聯網
上載者:User

轉自:http://www.codefans.net/jscss/code/1639.shtml

 

實際的應用中demo中的元素會有寬度的樣式所以我總是出現了 scrollTop總是小於OffsetTop 的現象。也就是demo2到demo最頂的高度總是大於捲軸可以到的最大高度,所以不會進入if語句,我這裡修改之後在我的項目中可以使用了。^_^

 

<body><div id="demo" style="overflow:hidden;height:80px;width:280px;"><ul id="demo1">  <li><a href="/soft/4085.shtml" target="_blank">XP Menu 仿QQ菜單管理器左側菜單</a></li><li><a href="/soft/5884.shtml" target="_blank">多層嵌套的一個層展開</a></li><li><a href="/soft/1852.shtml" target="_blank">《Java2核心技術卷2:進階特性》第7版</a></li><li><a href="/soft/1286.shtml" target="_blank">CSS、HTML教程打包下載 (CHM)</a></li><li><a href="/soft/4216.shtml" target="_blank">《C++ Primer》中文第四版 chm</a></li><li><a href="/soft/3388.shtml" target="_blank">C++工資管理系統(Access)</a></li><li><a href="/soft/2009.shtml" target="_blank">非常牛的左側欄JS摺疊菜單</a></li></ul> <div id="demo2"></div></div> <script>      var speed=40;      var demo=document.getElementById("demo");      var demo2=document.getElementById("demo2");      var demo1=document.getElementById("demo1");     demo2.innerHTML = demo1.innerHTML;       var demoMaxOffsetTop = demo.scrollHeight-demo.offsetHeight;//demo的最大scrollTop     function Marquee(){      if(demoMaxOffsetTop - demo.scrollTop<=0) {           demo.scrollTop-=demo1.offsetHeight;      }      else{         demo.scrollTop++;      }      }      var MyMar=setInterval(Marquee,speed);      demo.onmouseover=function() {clearInterval(MyMar);}      demo.onmouseout=function() {MyMar=setInterval(Marquee,speed);}     </script> </body>

 

對scollTop,offsetTop,offsetHeight的理解:

轉自 http://www.cnblogs.com/borthers11/articles/566243.html

7.scrollTop
對象的最頂部到對象在當前視窗顯示的範圍內的頂邊的距離.
即是在出現了縱向捲軸的情況下,捲軸拉動的距離.

1.offsetTop     :
當前對象到其上級層頂部的距離.
不能對其進行賦值.設定對象到頁面頂部的距離請用style.top屬性.

4.offsetHeight :
與style.height屬性的區別在於:如對象的寬度設定值為百分比高度,則無論頁面變大還是變小,style.height都返回此百分比,而offsetHeight則返回在不同頁面中對象的高度值而不是百分比值

 

2.offsetLeft    :
當前對象到其上級層左邊的距離.
不能對其進行賦值.設定對象到頁面左部的距離請用style.left屬性.

3.offsetWidth   :
當前對象的寬度.
與style.width屬性的區別在於:如對象的寬度設定值為百分比寬度,則無論頁面變大還是變小,style.width都返回此百分比,而offsetWidth則返回在不同頁面中對象的寬度值而不是百分比值

5.offsetParent  :
當前對象的上級層對象.
注意.如果對象是包括在一個DIV中時,此DIV不會被當做是此對象的上級層,(即對象的上級層會跳過DIV對象)上級層是Table時則不會有問題.
利用這個屬性,可以得到當前對象在不同大小的頁面中的絕對位置.

6.scrollLeft    :
對象的最左邊到對象在當前視窗顯示的範圍內的左邊的距離.
即是在出現了橫向捲軸的情況下,捲軸拉動的距離.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.