今回はJQueryで指定した要素を見せたり隠したりできるshow、hide、toggleについて解説します。
それではいってみましょう(*゚▽゚)ノ
Menu
hide 隠す
構文
hide(ミリ秒)
hideサンプル
下記の例は1秒かけて隠します。
引数に時間を指定できます。1000ミリ秒→1秒
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <!DOCTYPE html> <html lang="jn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="http://code.jquery.com/jquery-1.8.3.js"></script> <title>hide</title> <style> .sample{ height: 100px; width: 100px; background-color: red; } </style> </head> <body> <script> $(function() { $('button').click(function() { $('.sample').hide(1000); //隠す }); }); </script> <div class="sample"></div> <button>hide</button> </body> </html> |
show 表示する
構文
hide(ミリ秒)
showサンプル
下記の例は1秒かけて表示します。
引数に時間を指定できます。1000ミリ秒→1秒
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <!DOCTYPE html> <html lang="jn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="http://code.jquery.com/jquery-1.8.3.js"></script> <title>show</title> <style> .sample{ height: 100px; width: 100px; background-color: red; display: none; } </style> </head> <body> <script> $(function() { $('button').click(function() { $('.sample').show(1000); //表示させる }); }); </script> <div class="sample"></div> <button>show</button> </body> </html> |
toggle 隠れてたら表示、表示してたら隠す
構文
toggle(ミリ秒)
toggleサンプル
下記の例は1秒かけて隠れてたら表示、表示してたら隠します。
引数に時間を指定できます。1000ミリ秒→1秒
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <!DOCTYPE html> <html lang="jn"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="http://code.jquery.com/jquery-1.8.3.js"></script> <title>toggle</title> <style> .sample{ height: 100px; width: 100px; background-color: red; } </style> </head> <body> <script> $(function() { $('button').click(function() { $('.sample').toggle(1000); //表示↔隠す }); }); </script> <div class="sample"></div> <button>toggle</button> </body> </html> |
まとめ
このような動きを自分のサイトやブログのどこの部分に実装するかを考え、是非動的のあるカッコイイサイトを作成してみてください!
以上です٩(ˊωˋ*)و