http://black-flag.net/jquery/20131217-4909.html
1 2 3 4 5 6 7 8 9 10 | < div class = "viewer" > < ul > < li >< a href = "#1" >< img src = "img/photo1.jpg" alt = "" ></ a ></ li > < li >< a href = "#2" >< img src = "img/photo2.jpg" alt = "" ></ a ></ li > < li >< a href = "#3" >< img src = "img/photo3.jpg" alt = "" ></ a ></ li > < li >< a href = "#4" >< img src = "img/photo4.jpg" alt = "" ></ a ></ li > < li >< a href = "#5" >< img src = "img/photo5.jpg" alt = "" ></ a ></ li > < li >< a href = "#6" >< img src = "img/photo6.jpg" alt = "" ></ a ></ li > </ ul > </ div > <!--/.viewer--> |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | ◆CSS /* .viewer ------------------------- */ .viewer { margin : 0 auto ; width : 100% ; position : relative ; overflow : hidden ; } .viewer ul { width : 100% ; overflow : hidden ; position : relative ; } .viewer ul li { top : 0 ; left : 0 ; width : 100% ; position : absolute ; } .viewer ul li img { width : 100% ; } /* sideNavi ------------------------- */ .viewer .btnPrev, .viewer .btnNext { margin-top : -25px ; top : 50% ; width : 50px ; height : 50px ; position : absolute ; z-index : 101 ; } .viewer .btnPrev { left : 10px ; background : #ccc url (../img/btnPrev.jpg) no-repeat center center ; } .viewer .btnNext { right : 10px ; background : #ccc url (../img/btnNext.jpg) no-repeat center center ; } /* ======================================= ClearFixElements ======================================= */ .viewer ul:after { content : "." ; height : 0 ; clear : both ; display : block ; visibility : hidden ; } .viewer ul { display : inline-block ; overflow : hidden ; } |
1 2 | $(window).load( function (){} は表示速度がおそくなるからとった方がいいね |
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | <script type= "text/javascript" src= "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" ></script> <script> $( function (){ $(window).load( function (){ var setElm = $( '.viewer' ), setMaxWidth = 800, setMinWidth = 320, fadeSpeed = 1500, switchDelay = 5000, sideNavi = 'off' , // 'on' or 'off' sideHide = 'hide' , // 'hide' or 'show' naviOpc = 0.5; setElm.each( function (){ var targetObj = $(this), findUl = targetObj.find( 'ul' ), findLi = targetObj.find( 'li' ), findLiFirst = targetObj.find( 'li:first' ); findLi.css({display: 'block' ,opacity: '0' ,zIndex: '99' }); findLiFirst.css({zIndex: '100' }).stop().animate({opacity: '1' },fadeSpeed); function timer(){ setTimer = setInterval( function (){ slideNext(); },switchDelay); } timer(); function slideNext(){ findUl.find( 'li:first-child' ).not( ':animated' ).animate({opacity: '0' },fadeSpeed).next( 'li' ).css({zIndex: '100' }).animate({opacity: '1' },fadeSpeed). end ().appendTo(findUl).css({zIndex: '99' }); } function slidePrev(){ findUl.find( 'li:first-child' ).not( ':animated' ).css({zIndex: '99' }).animate({opacity: '0' },fadeSpeed).siblings( 'li:last-child' ).css({zIndex: '100' }).animate({opacity: '1' },fadeSpeed).prependTo(findUl); } targetObj.css({width:setMaxWidth,display: 'block' }); // メイン画像をベースにエリアの幅と高さを設定 var setLiImg = findLi.find( 'img' ), baseWidth = setLiImg.width(), baseHeight = setLiImg.height(); // レスポンシブ動作メイン function imgSize(){ var windowWidth = parseInt($(window).width()); if (windowWidth >= setMaxWidth) { targetObj.css({width:setMaxWidth,height:baseHeight}); findUl.css({width:baseWidth,height:baseHeight}); findLi.css({width:baseWidth,height:baseHeight}); } else if (windowWidth < setMaxWidth) { if (windowWidth >= setMinWidth) { targetObj.css({width: '100%' }); findUl.css({width: '100%' }); findLi.css({width: '100%' }); } else if (windowWidth < setMinWidth) { targetObj.css({width:setMinWidth}); findUl.css({width:setMinWidth}); findLi.css({width:setMinWidth}); } var reHeight = setLiImg.height(); targetObj.css({height:reHeight}); findUl.css({height:reHeight}); findLi.css({height:reHeight}); } } $(window).resize( function (){imgSize();}).resize(); // サイドナビボタン(有り無し) var agent = navigator.userAgent; if (sideNavi == 'on' ){ targetObj.append( '<a href="javascript:void(0);" class="btnPrev"></a><a href="javascript:void(0);" class="btnNext"></a>' ); var btnPrev = targetObj.find( '.btnPrev' ),btnNext = targetObj.find( '.btnNext' ),btnPrevNext = targetObj.find( '.btnPrev,.btnNext' ); if (agent.search(/iPhone/) != -1 || agent.search(/iPad/) != -1 || agent.search(/iPod/) != -1 || agent.search(/Android/) != -1){ btnPrevNext.css({opacity:naviOpc}); } else { btnPrevNext.css({opacity:naviOpc}).hover( function (){ $(this).stop().animate({opacity:naviOpc+0.2},200); }, function (){ $(this).stop().animate({opacity:naviOpc},200); }); } if (sideHide == 'hide' ){ if (agent.search(/iPhone/) != -1 || agent.search(/iPad/) != -1 || agent.search(/iPod/) != -1 || agent.search(/Android/) != -1){ btnPrevNext.css({visibility: 'visible' }); } else { btnPrevNext.css({visibility: 'hidden' }); targetObj.hover( function (){ btnPrevNext.css({visibility: 'visible' }); }, function (){ btnPrevNext.css({visibility: 'hidden' }); }); } } btnPrev.click( function (){switchPrev();}); btnNext.click( function (){switchNext();}); } // ボタン移動動作 function switchNext(){ findLi.not( ':animated' ).parents( 'ul' ).each( function (){ clearInterval(setTimer); slideNext(); timer(); }); } function switchPrev(){ findLi.not( ':animated' ).parents( 'ul' ).each( function (){ clearInterval(setTimer); slidePrev(); timer(); }); } }); }); }); </script> |
スクリプト開始部分にある設定値の内容は以下の様になっています。
var setElm = $(‘.viewer’) | スライドショー全体を囲うブロック要素名(IDでも可) |
---|---|
setMaxWidth = 800 | スライドショー部分の最大幅 |
setMinWidth = 320 | スライドショー部分の最小幅 |
fadeSpeed = 1500 | フェードアニメーションスピード |
switchDelay = 5000 | スライドアニメーション待機時間 |
sideNavi = ‘off’ | 左右NEXT/BACKの表示/非表示(表示する場合は「on」) |
sideHide = ‘hide’ | 左右NEXT/BACKのロールオーバーで表示切り替えON/OFF(常に表示させる場合は「show」等) |
naviOpc = 0.5 | 左右NEXT/BACKの透過度 |
これらの設定値を変更することでもろもろ調整が可能になっています。
スクリプト全体はあまりシンプルと言える構成ではありませんが
少しの設定値を調整するだけで簡単に設置することは可能になっているかと思います。
画像サイズ(比率)によって各エリアの幅と高さを取得して制御していることもあり
使用する(切り替える)画像は全て幅と高さを同サイズのものを使用する必要があります。
オプション機能としてスライドショーの左右にNEXT/BACKのボタンを表示させるようにもしてあり、
「sideNavi」を「’on’」や「’off’」とすることで表示/非表示を切り替えることができます。
(記述は「off」でなくても「on」じゃなければ何でもいいです・・・)
表示したNEXT/BACKボタンをスライドショーエリアにカーソルが乗った時のみ
表示させるようにするには「sideHide」を「’hide’」とします。
常にボタンを表示させるにはここの値は「’show’」とします。
(記述は「show」でなくても「hide」じゃなければ何でもいいです・・・)
スライドショー自体は1ページ内に複数設置することも可能になっていますが
複数設置した際にNEXT/BACKボタンをつけるとどうも動作がうまくいかなくなるようなので
複数設置する場合はNEXT/BACKボタンは「off」にしてください。
※不完全ですみません。。