移動端網頁自定義滾動條
阿新 • • 發佈:2017-07-29
max view andro move .html class height transform inner
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta id="viewport" name="viewport" content="initial-scale=1.0,user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> <title>移動端網頁自定義滾動條</title> <style type="text/css"> #outer_div{ width: 100px; height: 100px;/*position: absolute;*/ } .inner_div{ width: 100px; height: 100px; border: 1px solid #efefef; } </style> </head> <body> <div> 我的開發經驗是,橫向滾動可以用overflow-x:scoll;<br /> 然而豎向滾動,當滾到底時,會帶動整張頁面。原諒我這裏忘了測試,好像iphone不會帶動,android會帶動。<br /> 這時候你要自己寫滾動。</div> <div id="outer_div"> <div class="inner_div">1</div> <div class="inner_div">2</div> <div class="inner_div">3</div> <div class="inner_div">4</div> <div class="inner_div">5</div> <div class="inner_div">6</div> <div class="inner_div"></div> <div class="inner_div"></div> <div class="inner_div"></div> <div class="inner_div"></div> <div class="inner_div"></div> <div class="inner_div"></div> <div class="inner_div"></div> <div class="inner_div"></div> </div> <script type="text/javascript"> vargundongX = 0; var gundongY = 0; var d = document; var g = ‘getElementById‘; var moveEle = d[g](‘outer_div‘); var stx = sty = etx = ety = curX = curY = 0; moveEle.addEventListener("touchstart", function(event) { //touchstart gundongX = 0; gundongY = 0; // 元素當前位置 etx = parseInt(getT3d(moveEle, "x")); ety = parseInt(getT3d(moveEle, "y")); // 手指位置 stx = event.touches[0].pageX; sty = event.touches[0].pageY; }, false); moveEle.addEventListener("touchmove", function(event) { // 防止拖動頁面 event.preventDefault(); // 手指位置 減去 元素當前位置 就是 要移動的距離 gundongX = event.touches[0].pageX - stx; gundongY = event.touches[0].pageY - sty; // 目標位置 就是 要移動的距離 加上 元素當前位置 curX = gundongX + etx; curY = gundongY + ety; // 自由移動 // moveEle.style.webkitTransform = ‘translate3d(‘ + (curX) + ‘px, ‘ + (curY) + ‘px,0px)‘; // 只能移動Y軸方向 moveEle.style.webkitTransform = ‘translate3d(‘ + 0 + ‘px, ‘ + (curY) + ‘px,0px)‘; }, false); moveEle.addEventListener("touchend", function(event) { //touchend etx = curX; ety = curY; }, false); function getT3d(elem, ename) { var str1 = elem.style.webkitTransform; if (str1 == "") return "0"; str1 = str1.replace("translate3d(", ""); str1 = str1.replace(")", ""); var carr = str1.split(","); if (ename == "x") return carr[0]; else if (ename == "y") return carr[1]; else if (ename == "z") return carr[2]; else return ""; } </script> </body> </html>
//來自:http://www.cnblogs.com/samwu/p/3508548.html
移動端網頁自定義滾動條