LoginSignup
1
2

More than 5 years have passed since last update.

jQuery Animate

Posted at
こちらはjQueryの初心者向けです。

jQueryで要素をAnimateする

jQueryを使いながら、要素を動かしたい時があると思います。
その時は「.animate()」関数を使うと簡単に要素を動かすことができます。

まず、jQueryを導入しましょう。

animate.html
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

それから要素の定義やスタイルを決めましょう。

animate.html
<style type="text/css">
.animate-div {
  width: 100px;
  height: 100px;
  border: #000 2px solid;
}
</style>

<p><button>Animate</button></p>
<div class="animate-div"></div>

するとこんな感じの四角が表示されると思います。

Cap 2016-06-06 11-07-09-506.png

では、animateを定義してみましょう。

animate.html
<script type="text/javascript">
$(document).ready(function() {
  $(document).on("click", "button", function() {
    $(".animate-div").animate({
      width: "300px",
      height: "300px",
      borderColor: "#00f",
      backgroundColor: "#f00"
    });
  });
});
</script>

widthとheightは四角の大きさを変更します。
CSSでは「border-color」のタグがjQueryでは「borderColor」、
「background-color」のタグが「backgroundColor」になることが分かります。

上の「button」が押されたら「.animate-div」の「animate」が実行される感じです。

「Animete」と書いているボタンを押してみましょう。

すると

Cap 2016-06-06 11-11-11-737.png

こんな感じになりましたか?

もし色がだめの時はページの上部に次のコードを導入してみましょう。

animate.html
<script src="http://code.jquery.com/color/jquery.color-2.1.2.min.js" integrity="sha256-H28SdxWrZ387Ldn0qogCzFiUDDxfPiNIyJX7BECQkDE=" crossorigin="anonymous"></script>
1
2
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
2