2017-06-22 3 views
0

私はこのコードをtouchDownするとモバイルメニューを表示しますが、Firefoxでは何らかの形でdivは常にすぐに直ちにバックアップします。jQuery slideDown Firefoxでそれ自体をアップさせる

$('#burger').on('click touchstart', function (e) { 
    e.preventDefault(); 
    if (jQuery('#mobile-menu').is(":hidden")) { 
     jQuery('body').css('position','relative'); 
     jQuery('.page').css({'overflow':'hidden','position':'fixed'}); 
     jQuery('#mobile-menu').slideDown('slow'); 
    } else { 
     jQuery('#mobile-menu').slideUp('slow'); 
     jQuery('.page').css({'overflow':'visible','position':'static'}); 
     jQuery('body').css('position','static'); 
    } 
    return false; 
}); 
+0

'e.stopPropagation()' – tavnab

+0

@tavnabを追加してみてください。自動的にfalseが返されます。 – Barmar

+1

私は、 'click'イベントと' touchstart'イベントの両方が送信されていると思います。そこで彼らの1人がメニューを開き、もう1人がメニューを閉じます。両方のイベントが必要ですか? – Barmar

答えて

0

これはFirefoxのバグだと思われます。そうであれば、それほどエレガントではありませんが、作業パッチを必要とします。 e.stopPropagation()とともに、jQueryのでこれを呼び出して自動的にfalseを返すので、私はe.preventDefault()を削除

// when true, ignore the event 
 
var stifleEvent = false; 
 

 
$('#burger').on('click touchstart', function (e) { 
 
    if (stifleEvent) { 
 
    return false; 
 
    } 
 

 
    if (jQuery('#mobile-menu').is(":hidden")) { 
 
    $('body').css('position','relative'); 
 
    $('.page').css({'overflow':'hidden','position':'fixed'}); 
 
    $('#mobile-menu').slideDown('slow'); 
 
    } else { 
 
    $('#mobile-menu').slideUp('slow'); 
 
    $('.page').css({'overflow':'visible','position':'static'}); 
 
    $('body').css('position','static'); 
 
    } 
 
    
 
    // ignore more of this event for the next 10ms 
 
    stifleEvent = true; 
 
    setTimeout(() => { stifleEvent = false; }, 10); 
 
    
 
    return false; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="page"> 
 
    <div id="mobile-menu"> 
 
    Sliding Menu 
 
    </div> 
 
    <button id="burger" type="button"> 
 
    | | | 
 
    </button> 
 
</div>

注意。

+0

残念ながら、これは動作しません。これまでのところクリックを削除することは唯一のトリックです。 – user1801605

+0

あなたは正しいです。 「タッチスタート」と「クリック」との間の時間は、ユーザが指を持ち上げるのに要する時間と同じくらい大きくてもよい。 – tavnab

関連する問題