のみ窓/スクリーンサイズに基づいて、ホバー機能を起動するスクリプトに条件を追加します。
if($(window).width() > 1024) {
$(".dropdown").hover(function() {
$('.dropdown-menu', this).not('.in .dropdown-menu').stop(true, true).slideDown("800");
$(this).toggleClass('open');
}, function() {
$('.dropdown-menu', this).not('.in .dropdown-menu').stop(true, true).slideUp("800");
$(this).toggleClass('open');
});
$(".dropdown-2").hover(function() {
$('.dropdown-menu-2', this).not('.in .dropdown-menu-2').stop(true, true).slideDown("800");
$(this).toggleClass('open');
}, function() {
$('.dropdown-menu-2', this).not('.in .dropdown-menu-2').stop(true, true).slideUp("800");
$(this).toggleClass('open');
});
} else {
$(".dropdown").click(function() {
$('.dropdown-menu', this).not('.in .dropdown-menu').stop(true, true).slideDown("800");
$(this).toggleClass('open');
}, function() {
$('.dropdown-menu', this).not('.in .dropdown-menu').stop(true, true).slideUp("800");
$(this).toggleClass('open');
});
$(".dropdown-2").click(function() {
$('.dropdown-menu-2', this).not('.in .dropdown-menu-2').stop(true, true).slideDown("800");
$(this).toggleClass('open');
}, function() {
$('.dropdown-menu-2', this).not('.in .dropdown-menu-2').stop(true, true).slideUp("800");
$(this).toggleClass('open');
});
}
チェックは画面サイズは1024個のピクセルが(これを変更することを偉大であるかどうかを確認するために行われていますどんなスイートでも)。
「はい」の場合、ホバー機能付きのドロップダウンが実行されます。いいえ(モバイル)、クリック機能でドロップダウンを実行します。
注: 大きな画面上の応答機能をテストする場合は、画面サイズが、それ以来変更されているかどうかをチェックします「リサイズ」機能を実行する必要があります。
function hoverOrClick() {
if($(window).width() > 1024) {
$(".dropdown").hover(function() {
$('.dropdown-menu', this).not('.in .dropdown-menu').stop(true, true).slideDown("800");
$(this).toggleClass('open');
}, function() {
$('.dropdown-menu', this).not('.in .dropdown-menu').stop(true, true).slideUp("800");
$(this).toggleClass('open');
});
$(".dropdown-2").hover(function() {
$('.dropdown-menu-2', this).not('.in .dropdown-menu-2').stop(true, true).slideDown("800");
$(this).toggleClass('open');
}, function() {
$('.dropdown-menu-2', this).not('.in .dropdown-menu-2').stop(true, true).slideUp("800");
$(this).toggleClass('open');
});
} else {
$(".dropdown").click(function() {
$('.dropdown-menu', this).not('.in .dropdown-menu').stop(true, true).slideDown("800");
$(this).toggleClass('open');
}, function() {
$('.dropdown-menu', this).not('.in .dropdown-menu').stop(true, true).slideUp("800");
$(this).toggleClass('open');
});
$(".dropdown-2").click(function() {
$('.dropdown-menu-2', this).not('.in .dropdown-menu-2').stop(true, true).slideDown("800");
$(this).toggleClass('open');
}, function() {
$('.dropdown-menu-2', this).not('.in .dropdown-menu-2').stop(true, true).slideUp("800");
$(this).toggleClass('open');
});
}
}
hoverOrClick(); // Runs code the first time
$(window).resize(function(){
hoverOrClick(); // Runs code again to see if screen size has changed
}
興味深いアプローチが、デバイスは、より良い選択肢だろうタッチ対応であるかどうかを検出:だから、再び呼び出すための関数でコード全体をラップする方が良いだろう。 – jcaron
@jcaron Upvoted。しかし、彼は新しいタッチ対応デスクトップコンピュータに注意する必要があります。私は、マウスのクリックとタッチの両方の機能を持っているかどうかは分かりませんが、デバイスはこの状態でホバー状態のバージョンにデフォルト設定されます。 – thisiskelvin
@jcaronおそらく2の組み合わせが必要です。タッチと画面が共通のモバイルサイズよりも小さい場合 – thisiskelvin