WEBデザインの勉強

初心者が1からWEBデザインを勉強するブログ。

コンテンツを縦横斜めにスライドさせるjQuery faux-3D Viewport

f:id:mmmmofu:20161024110631p:plain

デモはこちら

http://efu.sakura.ne.jp/web/3d/

jQueryjQuery easingを読み込んでおく

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>jQuery faux-3D Viewport</title>
    <link rel="stylesheet" href="css/style.css">
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="js/jquery.easing.1.3.js"></script>
<script src="js/script.js"></script>
</head>
<body>
    <div id="enclosure">
        <div id="panel">
            <div class="slide-wrap">1</div>
            <div class="slide-wrap">2</div>
            <div class="slide-wrap">3</div>
            <div class="slide-wrap">4</div>
            <div class="slide-wrap">5</div>
            <div class="slide-wrap">6</div>
            <div class="slide-wrap">7</div>
            <div class="slide-wrap">8</div>
            <div class="slide-wrap">9</div>
        </div>
    </div>
    <ul id="nav">
        <li><a href="#1"><p>1</p></a></li>
        <li><a href="#2"><p>2</p></a></li>
        <li><a href="#3"><p>3</p></a></li>
        <li><a href="#4"><p>4</p></a></li>
        <li><a href="#5"><p>5</p></a></li>
        <li><a href="#6"><p>6</p></a></li>
        <li><a href="#7"><p>7</p></a></li>
        <li><a href="#8"><p>8</p></a></li>
        <li><a href="#9"><p>9</p></a></li>
    </ul>
</body>
</html>

CSS

@charset "utf-8";
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
  display: block;
}
body {
  line-height: 1;
}
ol, ul {
  list-style: none;
}
blockquote, q {
  quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
  content: '';
  content: none;
}
table {
  border-collapse: collapse;
  border-spacing: 0;
}
.clearfix:after {
  content: '.';
  display: block;
  clear: both;
  height: 0;
  visibility: hidden;
}
body {
  font-family: "游ゴシック",YuGothic,"Helvetica","Arial","ヒラギノ角ゴ ProN W3", "Hiragino Kaku Gothic ProN", "メイリオ", Meiryo, "Helvetica Neue", Helvetica, Arial, sans-serif;
  //font-family: "游明朝", "YuMincho", "ヒラギノ明朝 ProN W3", "Hiragino Mincho ProN", "HG明朝E", "MS 明朝", "MS P明朝", serif;
  // font-family: "Helvetica Neue", Helvetica, Arial, "ヒラギノ角ゴ ProN W3", "Hiragino Kaku Gothic ProN", "游ゴシック", YuGothic, "メイリオ", Meiryo, sans-serif;
  text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
}
* {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
body{
  font-family: 'Crimson Text', serif;
}

#enclosure {
  width:600px; height:400px;overflow:hidden; position:relative;
}

#panel {
  width:1800px; height:1800px; position:absolute;
}

.slide-wrap {
 float:left;  width:580px; height:380px; margin:10px; border:30px solid #DDD;
}

#nav {
  width: 150px;
}

#nav li {
  float: left;
  width:40px;
  height: 40px;
  background: #000;
  margin: 5px;
}

#nav li a {
  display: block;
  color: #FFF;
  text-decoration: none;
  text-align: center;
  padding: 10px;
}

#nav li a:hover {
  opacity: 0.6;
}

JS

$(function(){
    var shiftSpeed = 1500; //スライドスピード
    var SlideWidth = 600; //スライドの大きさ
    var SlideHeight = 400; //スライドの幅
    var columnCount = 3; //縦列の数
    var rowCount = 3; //横列の数

    $("ul#nav li a").click( function() {
        navIndex = $("ul#nav li a").index(this);
        shiftBoxes( navIndex  );
    });

    function shiftBoxes(boxN) {
        var col = ( (boxN) % rowCount ) * -1;
        var row = Math.floor( (boxN) / columnCount ) * -1;
        $("#panel").stop().animate({
            left: SlideWidth * col,
            top: SlideHeight * row },
                                   shiftSpeed, "easeInOutCubic"  );
    }
});