LoginSignup
5

More than 5 years have passed since last update.

jquery/js<レスポンシブでウィンドウサイズを変えた時だけ何かするjs>

Last updated at Posted at 2015-06-26

・・・1年ぶりにjqueryの勉強を再開したペペロン(p_p;)

js
<script type="text/javascript">

$(function() {
    var w = $(window).width();
    var x = 768;
    if (w <= x) {
    $(".hoge").addClass("pele");
    }
    var timer = false;
    $(window).resize(function() {
        if (timer !== false) {
            clearTimeout(timer);
        }
        timer = setTimeout(function() {
            var w = $(window).width();
            var x = 768;
            if (w <= x) {
                $(".hoge").addClass("pele");
            }else{
                $(".hoge").removeClass("pele");
            }
        }, 200);
    });
});


</script>
html&css
<style type="text/css">

.hoge {
    background: #eee;
}
.pele {
    background: yellow;
}

</style>

<p class="hoge">ほげっと</p>

結果を見る
http://jsfiddle.net/jackotonashi/coak0ard/

参考サイト:
http://kadoppe.com/archives/2012/02/jquery-window-resize-event.html
http://bl6.jp/web/javascript/change-process-get-window-size/

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
5