2014年12月31日水曜日

jQuery+CSSでウィンドウの拡大・縮小をやってみた。

jQuery+CSSでウィンドウの拡大・縮小を再現してみた。



Window.html

<!DOCTYPE html>

<html>

    <head>

        <title>TODO supply a title</title>

        <meta charset="UTF-8">

        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <link rel="stylesheet" href="./css/Window.css">

        <script src="./js/vendor/jquery-2.1.1.js"></script>

        <script src="./js/vendor/jquery-ui.js"></script>

        <script src="./js/Window.js"></script>

    </head>

    <body>

       

        <button class="show">ウィンドウを表示</button>

       

        <div class="window">

            <div class="title">

                ウィンドウのタイトル

            </div>

            <button class="minimize">-</button>

            <button class="maximize">□</button>

            <button class="close">×</button>

            <div class="content">

                コンテンツの内容はこんな感じですよ。

                コンテンツの内容はこんな感じですよ。

                コンテンツの内容はこんな感じですよ。

                コンテンツの内容はこんな感じですよ。

                コンテンツの内容はこんな感じですよ。

                コンテンツの内容はこんな感じですよ。

                コンテンツの内容はこんな感じですよ。

                コンテンツの内容はこんな感じですよ。

                コンテンツの内容はこんな感じですよ。

                コンテンツの内容はこんな感じですよ。

               

            </div>

        </div>

    </body>

</html>


Window.css
.window {

    position:relative;

    width: 260px;

    height: 180px;

    border-style: solid;

    border-width: 2px;

    border-color: rgb(150, 150, 150);

    background: rgb(218, 218, 218);

}



.window > .title {

    position: absolute;

    left: 0px;

    display: inline-block;

    background-color: rgb(0, 0, 230);

    border-color: rgb(0, 0, 180);

    border-width: 2px;

    height: 20px;

    width: 260px;

    margin: 0px;

    color: white;

}



.window > button {

    position: absolute;

    display: inline-block;

    border-width: 2px;

    border-style: solid;

    border-color: rgb(150, 150, 150);

    background-color: rgb(218, 218, 218);

    height: 20px;

    margin: 0px;

}



.window > .minimize {

    right: 40px;

}

.window > .maximize {

    right: 20px;

}

.window > .close {

    right: 0px;

}



.window > .content {

    position: relative;

    display: block;

    overflow: scroll;

    top: 20px;

    width: 260px;

    height: 160px;

}



.show {

    position: relative;

    display: block;

    top: 0px;

    left: 0px;

}
Window.js

$(function(){

   

    var $window = $('.window'),

        $maximize = $window.find('.maximize'),

        $minimize = $window.find('.minimize'),

        $close = $window.find('.close'),

        $title = $window.find('.title'),

        $contents = $window.find('.content'),

        $showButton = $('.show'),

        $baseWindow = $(window);

       

    var width = $baseWindow.width(),

        height = $baseWindow.height();

   

    var originalWidth = $window.width(),

        originalHeight = $window.height();

    

    var duration = 750;

   

    var windowWidth = $window.width();

    $title.css({ width: windowWidth+'px' });

   

    $close.on('click', function(){

        $window.css({ display: 'none' });

    });

   

    $maximize.on('click', function(){

        width = $baseWindow.width();

        height = $baseWindow.height();

        $(this).toggleClass('maximized');

        if($(this).hasClass('maximized')){

            $window.stop(true).animate({

                top: '0px',

                left: '0px',

                width: width+'px',

                height: height+'px'

            }, {

                duration: duration,

                queue: false

            });

            $title.stop(true).animate({

                width: width+'px'

            },{

                duration: duration,

                queue: false

            });

            $contents.stop(true).animate({

                width: width+'px',

                height: height+'px'

            },{

                duration: duration,

                euque: false

            });

        } else {

            $window.stop(true).animate({

                top: '0px',

                left: '0px',

                width: originalWidth+'px',

                height: originalHeight+'px'

            }, {

                duration: duration,

                queue: false

            });

            $title.stop(true).animate({

                width: originalWidth+'px'

            },{

                duration: duration,

                queue: false

            });

            $contents.stop(true).animate({

                width: originalWidth+'px',

                height: originalHeight+'px'

            },{

                duration: duration,

                euque: false

            });

        }

    });

   

    $showButton.on('click', function(){

        $window.css({ display: 'inline-block' });

    });

   

});



0 件のコメント:

コメントを投稿