如何防止手机间隙中的应用程序垂直滚动?

Cas*_*ynn 57 iphone ios cordova

我正在尝试手机间隙,我希望我的应用程序不会在用户将手指拖过屏幕时上下滚动.这是我的代码.任何人都可以告诉我为什么它仍然允许滚动?

   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta name = "viewport" content = "user-scalable=no,width=device-width" />
    <!--<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />-->

    <meta http-equiv="Content-type" content="text/html; charset=utf-8">

    <!-- iPad/iPhone specific css below, add after your main css >
    <link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" />        
    <link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" />       
    -->
    <!-- If you application is targeting iOS BEFORE 4.0 you MUST put json2.js from http://www.JSON.org/json2.js into your www directory and include it here -->
    <script type="text/javascript" charset="utf-8" src="phonegap.0.9.5.1.min.js"></script>
    <script type="text/javascript" charset="utf-8">


    // If you want to prevent dragging, uncomment this section
    /*
    function preventBehavior(e) 
    { 
      e.preventDefault(); 
    };
    document.addEventListener("touchmove", preventBehavior, false);
    */

    /* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
    see http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
    for more details -jm */
    /*
    function handleOpenURL(url)
    {
        // TODO: do something with the url passed in.
    }
    */

    function onBodyLoad()
    {       
        document.addEventListener("deviceready",onDeviceReady,false);
    }

    /* When this function is called, PhoneGap has been initialized and is ready to roll */
    /* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
    see http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
    for more details -jm */
    function onDeviceReady()
    {
        // do your thing!
        navigator.notification.alert("PhoneGap is working")
    }
    touchMove = function(event) {
        // Prevent scrolling on this element
        event.preventDefault();
    }

</script>
<style>
#container {
width:100%;
height:100%;
}
</style>

</head>

    <body onload="onBodyLoad()">
        <div id="container" ontouchmove="touchMove(event);">
        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

gre*_*tys 88

如果您使用的是Cordova 2.3.0 + find config.xml并添加以下行:

<preference name="UIWebViewBounce" value="false" />

或者在Cordova 2.6.0 +中:

<preference name="DisallowOverscroll" value="true" />

  • 还要注意你的meta`viewport`标签.`width`值不能大于屏幕.当你的应用程序的方向是**风景**时,这很棘手.我使用这些值:`<meta name ="viewport"content ="user-scalable = no,initial-scale = 1,maximum-scale = 1,minimum-scale = 1,width = 1024,height = 768,target-用于横向iPad应用的densitydpi = device-dpi"/>`. (5认同)

Fel*_*ahm 35

加载页面时运行此代码以禁用拖动:

document.addEventListener('touchmove', function(e) { e.preventDefault(); }, false);
Run Code Online (Sandbox Code Playgroud)

这是jQuery的一个例子:

$(document).ready(function() {
    document.addEventListener('touchmove', function(e) { e.preventDefault(); }, false);
});
Run Code Online (Sandbox Code Playgroud)

  • @techieWings我不明白你的评论......你读过这个问题了吗?这正是原始海报所要求的. (2认同)

Rey*_*raa 18

在您的配置文件中使用

<preference name="webviewbounce" value="false" />
<preference name="DisallowOverscroll" value="true" />
Run Code Online (Sandbox Code Playgroud)


Spa*_*der 10

如果您使用的是Cordova 2.6.0+ find config.xml,只需添加/修改此行:

<preference name="DisallowOverscroll" value="true" />

  • 效果很好.我想知道为什么默认情况下不激活它.请注意,如果您使用的是cordova-cli,则必须修改的config.xml是您在/ www目录中找到的,而不是/ platforms/ios中的那个. (3认同)

Rav*_*ila 5

将以下条目添加到config.xml文件中:

<preference name="DisallowOverscroll" value="true" />
Run Code Online (Sandbox Code Playgroud)


zin*_*gle 4

您没有说这是原生应用还是网络应用.

如果这是本机应用程序,您可以关闭webview上的滚动

UIScrollView* scroll;  //
for(UIView* theWebSubView in self.webView.subviews){  // where self.webView is the webview you want to stop scrolling.
    if([theWebSubView isKindOfClass:[UIScrollView class] ]){
        scroll = (UIScrollView*) theWebSubView;
        scroll.scrollEnabled = false;
        scroll.bounces = false;
    }
}
Run Code Online (Sandbox Code Playgroud)

否则这里是phonegap wiki上的一个链接,用于防止滚动. http://wiki.phonegap.com/w/page/16494815/Preventing-Scrolling-on-iPhone-Phonegap-Applications