2014-12-19 160 views
-1

此代码只是允许在屏幕上拖动正方形。我用Adobe build构建它。我无法弄清楚为什么它不适用于移动平台(android和ios)。在网页上正常工作。手机拖动不能在移动平台上工作

<!doctype html> 
<html lang="en"> 
<head> 
<meta charset="utf-8"> 
<title>jQuery UI Draggable - Default functionality</title> 
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css"> 
<script type="text/javascript" src="phonegap.js"></script> 
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
<script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script> 
<script> 
#draggable { 
    height: 150px; 
    padding: 0.5em; 
    width: 150px; 
    border: 1px solid black; 
    background-color: #ddd; 
} 
</script> 
<link rel="stylesheet" href="css/style.css"> 
<script> 
$(document).ready(function() { 
    // are we running in native app or in a browser? 
    window.isphone = false; 
    if(document.URL.indexOf("http://") === -1 
     && document.URL.indexOf("https://") === -1) { 
     window.isphone = true; 
    } 

    if(window.isphone) { 
     document.addEventListener("deviceready", onDeviceReady, false); 
    } else { 
     onDeviceReady(); 
    } 
}); 

function onDeviceReady() { 
    // phonegap ready 
    init(); 
    $("#draggable").draggable(); 
    $("#msg").prepend("starting<br/>"); 
} 

function init() { 
    document.addEventListener("touchstart", touchHandler, true); 
    document.addEventListener("touchmove", touchHandler, true); 
    document.addEventListener("touchend", touchHandler, true); 
    document.addEventListener("touchcancel", touchHandler, true); 
} 
function touchHandler(event) { 
    var touches = event.changedTouches, 
    first = touches[0], 
    type = ""; 
    switch(event.type) 
    { 
     case "touchstart": type = "mousedown"; 
     $("#msg").prepend("touchstart<br/>"); 
     break; 
     case "touchmove": type="mousemove"; break;   
     case "touchend": type="mouseup"; break; 
     default: return; 
    } 
    var simulatedEvent = document.createEvent("MouseEvent"); 
    simulatedEvent.initMouseEvent(type, true, true, window, 1, 
         first.screenX, first.screenY, 
         first.clientX, first.clientY, false, 
         false, false, false, 0/*left*/, null); 
    first.target.dispatchEvent(simulatedEvent); 
    event.preventDefault(); 
} 
</script> 
</head> 
<body> 
<div id="draggable" class="ui-widget-content"> 
    <p>Drag me around</p> 
</div> 
<div id="msg"> 
</div> 
</body> 
</html> 

该代码是基于一个基本jQuery UI的例子,我在从各种其他示例的DeviceReady和TouchHandler代码拉动。 我做的似乎没有工作。我错过了什么吗?

+0

当你说“在网页上正常工作”时,你的意思是在手机上呢? – jcesarmobile 2014-12-19 09:49:16

+0

它可以在手机或触摸屏笔记本电脑上的浏览器中使用。 – eyn 2014-12-19 10:29:49

+0

我能够在telerik平台上构建一个可运行的应用程序。仍然无法获得Adobe平台来构建工作应用程序。 – eyn 2014-12-20 08:12:30

回答

1

我发现你的问题,它在URL来

当你把//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css的CDN的,PhoneGap的将其转换为file://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css

因此改变你的网址包含http:

<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css"> 
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> 
<script src="http://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script> 

甚至更​​好,下载这些文件并将它们放到您的www文件夹中,这样应用程序就不需要下载它们,应该加载速度更快,并且可以脱机工作。

+0

嗯,我把外部文件拖入项目目录。没有运气。仍然建立一个刚刚坐在那里并且没有互动的应用程序。但是,我在telerik平台上构建了相同的应用程序,它的效果非常好!所以,我仍然试图弄清楚如何在Adobe上构建它。如果我能在Adobe上运行,那么Telerik的成本只有1/5。 – eyn 2014-12-20 08:10:37

+0

你所说的真的很奇怪,我在本地构建了一个示例应用程序并开始工作 – jcesarmobile 2014-12-20 09:35:54