js實現帶按鈕的上下滾動效果

來源:互聯網
上載者:User

   本文執行個體講述了js實現帶按鈕的上下滾動效果。分享給大家供大家參考。具體實現方法如下:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 <!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>js帶按鈕上下滾動效果</title> <style type="text/css"> ul{ margin:0; padding:0; list-style:none; height:100000px; } li{ margin:0; padding:0; width:100px; height:100px; display:block; float:top; margin-bottom:5px; background:#009900; } #img_bag{ width:110px; height:600px; background:#F2F2F2; margin:0 auto; text-align:center; } #img_bag #img{ width:100px; height:525px; background:#969696; overflow:hidden; margin:auto; } </style> </head> <body> <div id="img_bag"> <a href="javascript:void(0)" onmousedown="moveTop()"> <img src="upp.jpg" border="0" /></a> <div id="img"> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>10</li> <li>11</li> <li>12</li> </ul> </div> <a href="javascript:void(0)" onmousedown="moveBottom()"> <img src="down.jpg" border="0" /></a> </div> <script type="text/javascript"> function $(obj){ //擷取指定對象 return document.getElementById(obj); } var maxHeight=$("img").getElementsByTagName("ul")[0].getElementsByTagName("li").length*105; //滾動圖片的最大高度 var targety = 211; //一次滾動距離 var dx; var a=null; function moveTop(){ var le=parseInt($("img").scrollTop); if(le>211){ targety=parseInt($("img").scrollTop)-211; }else{ targety=parseInt($("img").scrollTop)-le-1; } scTop(); } function scTop(){ dx=parseInt($("img").scrollTop)-targety; $("img").scrollTop-=dx*.3; clearScroll=setTimeout(scTop,50); if(dx*.3<1){ clearTimeout(clearScroll); } } function moveBottom(){ var le=parseInt($("img").scrollTop)+211; var maxL=maxHeight-600; if(le<maxL){ targety=parseInt($("img").scrollTop)+211; }else{ targety=maxL } scBottom(); } function scBottom(){ dx=targety-parseInt($("img").scrollTop); $("img").scrollTop+=dx*.3; a=setTimeout(scBottom,50); if(dx*.3<1){ clearTimeout(a) } } </script> </body> </html>

  運行效果如下所示:

  希望本文所述對大家的javascript程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.