另外,如果iframe的src不是网址,而是本地的html,则需要给HTML的DOM添加监听事件,让html的body可以监听到touch事件,让嵌套的html也可以滚动。
var toScrollFrame = function(iFrame, mask) { if (!navigator.userAgent.match(/iPad|iPhone/i)) return false; //do nothing if not iOS devie var mouseY = 0; var mouseX = 0; jQuery(iFrame).ready(function() {//wait for iFrame to load //remeber initial drag motition jQuery(iFrame).contents()[0].body.addEventListener('touchstart', function(e) { mouseY = e.targetTouches[0].pageY; mouseX = e.targetTouches[0].pageX; }); //update scroll position based on initial drag position jQuery(iFrame).contents()[0].body.addEventListener('touchmove', function(e) { e.preventDefault(); //prevent whole page dragging var box = jQuery(mask); box.scrollLeft(box.scrollLeft() + mouseX - e.targetTouches[0].pageX); box.scrollTop(box.scrollTop() + mouseY - e.targetTouches[0].pageY); //mouseX and mouseY don't need periodic updating, because the current position //of the mouse relative to th iFrame changes as the mask scrolls it. }); }); return true; }; toScrollFrame('.myFrame', '.myMask');
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <title>wrapScroller demo</title> <link rel="stylesheet" href="../style/wrapScroller.css" type="text/css" media="screen" /> <script type="text/javascript" src="../jquery-1.8.0.min.js"></script> <script type="text/javascript"> </script> </head> <body style="background: #ccc;"> <div> HEADER - use 2 fingers to scroll contents: </div> <div id="scrollee" style="width:300px;height:300px;-webkit-overflow-scrolling:touch; overflow: scroll;"> <iframe id="object" height="90%" width="100%" type="text/html" src="http://en.wikipedia.org/"></iframe> </div> </body> </html>