我正在为iPad上的Safari创建一个网站。 我需要防止双击事件缩放,但我有两个问题:
- 双击不会生成任何事件,所以我无法使用“event.preventDefault();”
- 只有在满足某些条件时才需要这样做,因此我不能使用标记“<meta name =“viewport” content =“user-scalable = no”>”... 如果我那样做,用户将无法在我的页面上缩放。
如何解决这些问题?
$('.prev,.next').nodoubletapzoom(); 的内容。(编辑:现在还忽略捏合手势)
// jQuery no-double-tap-zoom plugin
// Triple-licensed: Public Domain, MIT and WTFPL license - share and enjoy!
(function($) {
var IS_IOS = /iphone|ipad/i.test(navigator.userAgent);
$.fn.nodoubletapzoom = function() {
if (IS_IOS)
$(this).bind('touchstart', function preventZoom(e) {
var t2 = e.timeStamp
, t1 = $(this).data('lastTouch') || t2
, dt = t2 - t1
, fingers = e.originalEvent.touches.length;
$(this).data('lastTouch', t2);
if (!dt || dt > 500 || fingers > 1) return; // not double-tap
e.preventDefault(); // double tap - prevent the zoom
// also synthesize click events we just swallowed up
$(this).trigger('click').trigger('click');
});
};
})(jQuery);
尝试使用这个修改后的代码,它应该适用于安卓和IOS设备。
(function($) {
$.fn.nodoubletapzoom = function() {
$(this).bind('touchstart', function preventZoom(e){
var t2 = e.timeStamp;
var t1 = $(this).data('lastTouch') || t2;
var dt = t2 - t1;
var fingers = e.originalEvent.touches.length;
$(this).data('lastTouch', t2);
if (!dt || dt > 500 || fingers > 1){
return; // not double-tap
}
e.preventDefault(); // double tap - prevent the zoom
// also synthesize click events we just swallowed up
$(e.target).trigger('click');
});
};
})(jQuery);
$("body").nodoubletapzoom();
移动版Safari不支持javascript的ondblclick事件。在Safari中,它被解释为“缩放”。
Raul Sanchez发布了一个潜在的解决方案:http://appcropolis.com/implementing-doubletap-on-iphones-and-ipads/
// jQuery no-double-tap-zoom plugin
// Triple-licensed: Public Domain, MIT and WTFPL license - share and enjoy!
//
// chris.thomas@antimatter-studios.com: I modified this to
// use modernizr and the html.touch detection and also to stop counting two
// clicks at once, but count each click separately.
(function($) {
$.fn.nodoubletapzoom = function() {
if($("html.touch").length == 0) return;
$(this).bind('touchstart', function preventZoom(e){
var t2 = e.timeStamp;
var t1 = $(this).data('lastTouch') || t2;
var dt = t2 - t1;
var fingers = e.originalEvent.touches.length;
$(this).data('lastTouch', t2);
if (!dt || dt > 500 || fingers > 1){
return; // not double-tap
}
e.preventDefault(); // double tap - prevent the zoom
// also synthesize click events we just swallowed up
$(this).trigger('click');
});
};
})(jQuery);
$("body").nodoubletapzoom();
您需要翻译的内容
。<body>
<div class="content">...your content and everything in your page</div>
</body>
$(".content")
.on(".mydomelement","click",function(){...})
.on("button","click",function(){...})
.on("input","keyup blur",function(){...});
// etc etc etc, add ALL your handlers in this way
$('#nozoom').on('touchstart', function(e)
{
fn_start(e);
e.preventDefault();
});
fn_start()
(实际的回调函数)每次触摸开始时,但防止默认的缩放等操作。
这里有一个工作的比较示例:http://jsbin.com/meluyevisi/1/。 绿色框阻止,红色框允许。
仅仅设置视口可能不足够 - 在某些情况下,您可能还需要在touchstart处理程序上调用event.preventDefault();
对我来说,被接受的“双击”答案有点“臃肿”,而且使用了时间戳……为什么?看看这个简单的实现,没有过多的“臃肿”。
function simulateDblClickTouchEvent(oo)
{
var $oo = !oo?{}:$(oo);
if( !$oo[0] )
{ return false; }
$oo.bind('touchend', function(e)
{
var ot = this,
ev = e.originalEvent;
if( ev && typeof ev.touches == 'object' && ev.touches.length > 1 )
{ return; }
ot.__taps = (!ot.__taps)?1:++ot.__taps;
if( !ot.__tabstm ) // don't start it twice
{
ot.__tabstm = setTimeout( function()
{
if( ot.__taps >= 2 )
{ ot.__taps = 0;
$(ot).trigger('dblclick');
}
ot.__tabstm = 0;
ot.__taps = 0;
},800);
}
});
return true;
};
使用方法:
simulateDblClickTouchEvent($('#example'));
or
simulateDblClickTouchEvent($('.example'));
当事件被绑定或未被绑定时,函数返回true或false。
为了防止缩放和滚动,请执行以下操作:
function disableTouchScroll()
{
try {
document.addEventListener('touchmove', function(e) { e.preventDefault(); }, true );
$j('body')[0].addEventListener('touchmove', function(e) { e.preventDefault(); }, true );
}
catch(ee) { return false; }
return true;
}
同时,使用CSS也很容易避免缩放:
body * { -webkit-user-select:none; }
干杯!
function preventZoom(e) {
var t2 = e.timeStamp;
var t1 = e.currentTarget.dataset.lastTouch || t2;
var dt = t2 - t1;
var fingers = e.touches ? e.touches.length : 0;
e.currentTarget.dataset.lastTouch = t2;
if (!dt || dt > 500 || fingers > 1) return; // not double-tap
e.preventDefault();
e.target.click();
}
Angular 10 示例:
<!— Template —>
<span (click)="clicked($event)">Click me</span>
Component
clicked(event) {
this.preventZoom(event);
// your code
}
preventZoom(e) {
var t2 = e.timeStamp;
var t1 = e.currentTarget.dataset.lastTouch || t2;
var dt = t2 - t1;
var fingers = e.touches?.length;
e.currentTarget.dataset.lastTouch = t2;
if (!dt || dt > 500 || fingers > 1) return; // not double-tap
e.preventDefault();
e.target.click();
}
$(this).data('lastTouch', 0);
。如果没有它,当你三次点击时会得到四次点击(四次或更多)。有了它,三次点击会得到三次点击,四次点击会得到四次点击。这个想法是你可能有一个元素需要响应许多连续的点击(而不是被按住,出于任何原因)。 - Radu C