0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

JQueryでハンバーガーメニューを開いき、他の要素をクリックすると閉じる。

0
Last updated at Posted at 2024-04-12

参考

<i class="fa-solid fa-bars"></i>
$(document).on('click',function(e) {
    if(!$(e.target).closest('.fa-bars,.menu').length) {
        // ターゲット要素の外側をクリックした時の操作
        $("body").removeClass('open');
    } else {
        // ターゲット要素をクリックした時の操作
        $("body").addClass('open');
    }
    });

アイコンをクリックしたときにbody要素にクラスopenが付き、ナビゲーションメニューを表示。

$("body").addClass('open');

もしアイコン以外の要素をクリックしたときはbody要素のクラスopenがなくなり、ナビゲーションメニューを閉じる。

    if(!$(e.target).closest('.fa-bars,.menu').length) {
        // ターゲット要素の外側をクリックした時の操作
        $("body").removeClass('open');
0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?