1
1

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 3 years have passed since last update.

jQueryでaddClass()とfadeIn()を一緒にやる方法

Posted at

ある要素をクリックし、別の要素を表示させる処理を実装しているときに、おしゃれに表示させたいと思い、fadeIn()を使うことを考えましたが、addClass()と一緒に使う方法に詰まりました。

ここでは備忘録も兼ねてその方法を残しておきたいと思います。

addClass()とfadeIn()を一緒に使う方法

まずはHTMLです。尚、jQueryはCDNを使用しております。

index.html
    <div class="js-click-one">1</div>
    <div class="js-two invisible">2</div>

次にCSS。

style.css
.invisible{
    display: none;
}

仕組みとしては、1をクリックするとCSSのinvisibleクラスがjs-click-oneに付与され表示されなくなり、js-twoのinvisibleクラスをとり、fadeInで表示させることをイメージしています。

そして、そのイメージをコード化したのが下記です。

app.js
$(function (){
    $('.js-click-one').on('click', function (){
        $('.js-click-one').hide().addClass('invisible');
        $('.js-two').fadeIn(3000, function (){
            $(this).removeClass('invisible');
        })
    })
})

こうすれば、addClass()とfadeIn()を同時に使えます。
ぜひご参考に。

1
1
0

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?