2

しかし、非常に簡単に言えば、ボディ要素の touchmove イベントを防ぎたいのですが、別の要素に対しては有効にしたままにします。うまく無効にできます...しかし、別の場所で再度有効にする方法がわかりません!

以下return trueは の反対であるため、理論的には機能すると思いますpreventDefaultが、私には機能しません。$altNav要素が入っ $bodいるからでしょうか?

JS:

$bod.bind('touchmove', function(event){

    event.preventDefault();
});
$altNav.bind('touchmove', function(event){

    return true;
});
4

3 に答える 3

4

実際に使用している lib はわかりませんが、jQuery を想定します (jQ 以外のものを使用している場合は、browser-native-js にも同じコードを投稿します)。

$bod.delegate('*', 'touchstart',function(e)
{
    if ($(this) !== $altNav)
    {
        e.preventDefault();
        //and /or
        return false;
    }
    //current event target is $altNav, handle accordingly
});

それはすべての世話をする必要があります。ここでのコールバックは、すべてのtouchmove イベントを処理し、 以外preventDefault要素でイベントがトリガーされるたびにメソッドを呼び出します。$altNav

std browser-js では、このコードは次のようになります。

document.body.addEventListener('touchmove',function(e)
{
    e = e || window.event;
    var target = e.target || e.srcElement;
    //in case $altNav is a class:
    if (!target.className.match(/\baltNav\b/))
    {
        e.returnValue = false;
        e.cancelBubble = true;
        if (e.preventDefault)
        {
            e.preventDefault();
            e.stopPropagation();
        }
        return false;//or return e, doesn't matter
    }
    //target is a reference to an $altNav element here, e is the event object, go mad
},false);

が特定の ID を持つ要素である場合$altNavは、その要素を次のように置き換えます。 幸運を祈ります。これが役立つことを願っていますtarget.className.match()target.id === 'altNav'

于 2012-12-21T06:10:38.797 に答える
2

カスタム CSS クラスを使用し、ドキュメント ハンドラーでテストします。例:

<div>
    This div and its parents cannot be scrolled.
    <div class="touch-moveable">
        This div and its children can.
    </div>
</div>

それから:

jQuery( document ).on( 'touchmove', function( ev )
{
    if (!jQuery( ev.target ).parents().hasClass( 'touch-moveable' ))
    {
         ev.preventDefault();
    }
});

http://tinyurl.com/mo6vwrq

于 2013-08-16T05:11:50.030 に答える
1

このように引数を追加できます

$bod.bind('touchmove', function(event,enable){
   if(enable){
       event.preventDefault();
   }

});
于 2012-12-21T06:10:28.370 に答える