Javascript touchmove与jquery在画布上从span获取文本
我有以下 Javascript,鼠标悬停工作正常,但当我从支持的设备跟踪一条线时,触摸移动事件没有被触发,我在这里缺少什么,当我们使用触摸将鼠标悬停在跨度上时,有没有办法提醒文本事件?
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6rc1.js"></script>
<script type="text/javascript">
function init() {
setupDrawingCanvas();
}
function setupDrawingCanvas() {
var x, y, ctx, down=false;
var canvas = document.getElementById("graph");
canvas.width = window.innerWidth-45;
canvas.height = window.innerHeight-45;
var maxLength = (canvas.width > canvas.height) ? canvas.width : canvas.height;
function onTouchMove(e){
if (e.targetTouches.length == 1){
e.preventDefault();
ctx.beginPath();
ctx.moveTo(x,y);
ctx.lineTo(e.touches[0].pageX - canvas.offsetLeft, e.touches[0].pageY - canvas.offsetTop);
x = e.touches[0].pageX - canvas.offsetLeft;
y = e.touches[0].pageY - canvas.offsetTop;
ctx.stroke();
}
}
function onTouchEnd(e){
if (e.targetTouches.length==1){
e.preventDefault();
window.removeEventListener("touchmove", onTouchMove, false);
window.removeEventListener("touchend", onTouchEnd, false);
}
}
function onTouchStart(e){
if (e.touches.length == 1){
e.preventDefault();
x = e.touches[0].pageX - canvas.offsetLeft;
y = e.touches[0].pageY - canvas.offsetTop;
window.addEventListener("touchmove", onTouchMove, false);
window.addEventListener("touchend", onTouchEnd, false);
}
}
function dropEvent(e)
{
e.stopPropagation();
e.preventDefault();
}
if (canvas.getContext){
ctx = canvas.getContext("2d");
ctx.strokeStyle = "#000000";
canvas.addEventListener("touchstart", onTouchStart, false);
}
var func = (function() {
var num = parseInt($(this).text());
alert ("moved over" + num );
});
var $graph = $('#graph');
$('<span></span>')
.addClass('circle')
.html(1)
.css({ 'top': 471, 'left': 430 })
.mouseover(func)
.insertAfter($graph);
$('<span></span>')
.addClass('circle')
.html(2)
.css({ 'top': 451, 'left': 410 })
.mouseover(func)
.insertAfter($graph);
// This is not being called when the touchevent is over the span
$('circle').each.bind('touchmove',function(e) {
e.preventDefault();
var num = parseInt($(this).text());
alert ("moved over" + num );
});
}
</script>
<style type="text/css">
.circle {
background: url('circle.png');
width: 24px;
height: 24px;
text-align: center;
font-size: .8em;
line-height: 24px;
display: block;
position: absolute;
cursor: pointer;
color: #333;
z-index: 100;
}
#graph {
left: 200px;
top: 20px;
position: relative;
border: 1px dotted #ddd;
}
</style>
<body onload="init();">
<canvas id="graph" width="680" height="680"></canvas>
</body>
I have the following Javascript, The mouseover works fine but the touchmove event is not being fired when I trace a line from a supported device, What am I missing here and is there a way to alert the text when we hover over the span using touch event?
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6rc1.js"></script>
<script type="text/javascript">
function init() {
setupDrawingCanvas();
}
function setupDrawingCanvas() {
var x, y, ctx, down=false;
var canvas = document.getElementById("graph");
canvas.width = window.innerWidth-45;
canvas.height = window.innerHeight-45;
var maxLength = (canvas.width > canvas.height) ? canvas.width : canvas.height;
function onTouchMove(e){
if (e.targetTouches.length == 1){
e.preventDefault();
ctx.beginPath();
ctx.moveTo(x,y);
ctx.lineTo(e.touches[0].pageX - canvas.offsetLeft, e.touches[0].pageY - canvas.offsetTop);
x = e.touches[0].pageX - canvas.offsetLeft;
y = e.touches[0].pageY - canvas.offsetTop;
ctx.stroke();
}
}
function onTouchEnd(e){
if (e.targetTouches.length==1){
e.preventDefault();
window.removeEventListener("touchmove", onTouchMove, false);
window.removeEventListener("touchend", onTouchEnd, false);
}
}
function onTouchStart(e){
if (e.touches.length == 1){
e.preventDefault();
x = e.touches[0].pageX - canvas.offsetLeft;
y = e.touches[0].pageY - canvas.offsetTop;
window.addEventListener("touchmove", onTouchMove, false);
window.addEventListener("touchend", onTouchEnd, false);
}
}
function dropEvent(e)
{
e.stopPropagation();
e.preventDefault();
}
if (canvas.getContext){
ctx = canvas.getContext("2d");
ctx.strokeStyle = "#000000";
canvas.addEventListener("touchstart", onTouchStart, false);
}
var func = (function() {
var num = parseInt($(this).text());
alert ("moved over" + num );
});
var $graph = $('#graph');
$('<span></span>')
.addClass('circle')
.html(1)
.css({ 'top': 471, 'left': 430 })
.mouseover(func)
.insertAfter($graph);
$('<span></span>')
.addClass('circle')
.html(2)
.css({ 'top': 451, 'left': 410 })
.mouseover(func)
.insertAfter($graph);
// This is not being called when the touchevent is over the span
$('circle').each.bind('touchmove',function(e) {
e.preventDefault();
var num = parseInt($(this).text());
alert ("moved over" + num );
});
}
</script>
<style type="text/css">
.circle {
background: url('circle.png');
width: 24px;
height: 24px;
text-align: center;
font-size: .8em;
line-height: 24px;
display: block;
position: absolute;
cursor: pointer;
color: #333;
z-index: 100;
}
#graph {
left: 200px;
top: 20px;
position: relative;
border: 1px dotted #ddd;
}
</style>
<body onload="init();">
<canvas id="graph" width="680" height="680"></canvas>
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
修改以下调用,将“circle”更改为“.circle”...
Modify the following call, changing 'circle' to '.circle'...