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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 | /* * jQuery Image rolling Plugin * @author ddakker * @date 2012-01-30 * ex) <div id="div"> * <img> * <img> * ... * </div> * <button id="btPrev">Up,Left</button> * <button id="btNext">Down,Right</button> * <script> jQuery("#div").rolling(4, 5000, 1000, "btNext", "btPrev", "DOWN", true, callbackRollingFun); </script> * */ ;( function ($) { /** * 상하 이미지 롤링(추후 좌우 추가) * @param viewCount 이미지 보여줄 갯수 * @param delay 이동 주기 * @param duration Effect 지속 시간 * @param btNextId Up,Left 이동 버튼 * @param btPrevId Down,Right 이동 버튼 * @param direction 이동방향 * @param autoRolling 자동 롤링 * @param callbackRollingFun 이미지 롤링 시 호출될 콜백 함수 ex) callbackRollingFun(direction, options.rollingEl, selectedIndex); */ $.fn.rolling = function (viewCount, delay, duration, btNextId, btPrevId, direction, bAutoRolling, callbackMoveFun) { if ( this .children().length == 0 ) return ; var clickArr = new Array(); var options = {}; var bEffectProgress = false ; var autoRollingTimer = null ; var clickedProcessTimer = null ; options.el = $( this ); options.viewCount = viewCount; options.imgCount = options.el.children().length; options.direction = (direction==undefined)? "DOWN" :direction; options.delay = delay; options.duration = duration; options.itemWidth = options.el.children().eq(0).width() + Number(options.el.children().eq(0).attr( "border" ))*2; options.itemHeight = options.el.children().eq(0).height() + Number(options.el.children().eq(0).attr( "border" ))*2; options.btNextId = btNextId; options.btPrevId = btPrevId; options.autoRolling = (bAutoRolling==undefined)? true :bAutoRolling; options.callbackMoveFun = (callbackMoveFun==undefined)? null :callbackMoveFun;; options.rollingEl = null ; options.el.children().each( function (){ if ( $( this ).attr( "onclick" ) ) $( this ).css( "cursor" , "pointer" ); }); this .css( "position" , "relative" ); this .css( "overflow" , "hidden" ); if ( options.direction == "UP" || options.direction == "DOWN" ){ this .css( "width" , options.itemWidth); this .css( "height" , options.viewCount * options.itemHeight); } else { this .css( "width" , options.viewCount * options.itemWidth); this .css( "height" , options.itemHeight); } options.el.children().wrapAll( '<div></div>' ); options.rollingEl = options.el.children( "div" ); options.rollingEl.css( "position" , "absolute" ); options.rollingEl.attr( "align" , "left" ); options.rollingEl.css( "left" , "0" ); options.rollingEl.css( "top" , "0" ); if ( options.direction == "UP" || options.direction == "DOWN" ){ options.rollingEl.css( "height" , this .height()*2); } else { options.rollingEl.css( "width" , this .width()*2); } options.rollingEl.children().each( function (i){ options.rollingEl.children().eq(i).attr( "idx" , i); }); if ( options.imgCount >= viewCount ){ $( "#" + btPrevId).bind( "click" , function (){ if ( options.direction == "UP" || options.direction == "DOWN" ) clickArrAdd( "UP" ); else clickArrAdd( "LEFT" ); }).css( "cursor" , "pointer" ); $( "#" + btNextId).bind( "click" , function (){ if ( options.direction == "UP" || options.direction == "DOWN" ) clickArrAdd( "DOWN" ); else clickArrAdd( "RIGHT" ); }).css( "cursor" , "pointer" ); if ( options.autoRolling ){ autoRolling(); this .mouseover( function () { stopRolling(); }).mouseout( function (){ autoRolling(); }); } } // 동작 추가 및 호출 function clickArrAdd(direction){ clickArr.push(direction); stopRolling(); clickArrProcess(); } // 동작 처리 호출 function clickArrProcess(){ if ( clickedProcessTimer != null ) return ; clickedProcessTimer = setInterval( function (){ if ( bEffectProgress == false && clickArr.length > 0 ){ move(clickArr.slice(0, 1), true ); clickArr = clickArr.slice(1); } }, 100); } function autoRolling(){ if ( clickedProcessTimer ){ clearInterval(clickedProcessTimer); clickedProcessTimer = null ; } if ( clickedProcessTimer != null ) return ; autoRollingTimer = setInterval( function (){ if ( bEffectProgress == false ){ move( null , false ); } }, options.delay); } function stopRolling(){ clearInterval(autoRollingTimer); autoRollingTimer = null ; } // 동작 처리 function move(direction, btClicked){ if ( direction ) options.direction = direction; var topMoveSize = 0; var leftMoveSize = 0; if ( options.direction == "UP" || options.direction == "LEFT" ){ options.rollingEl.append(options.rollingEl.children().eq(0).clone()); if ( options.direction == "UP" ){ topMoveSize = "-=" + options.itemHeight + "px" ;; leftMoveSize = 0; } else { topMoveSize = 0; leftMoveSize = "-=" + options.itemWidth + "px" ;; } } else if ( options.direction == "DOWN" || options.direction == "RIGHT" ){ options.rollingEl.prepend(options.rollingEl.children().eq(options.rollingEl.children().length-1).clone()); if ( options.direction == "DOWN" ){ options.rollingEl.css( "top" , (options.itemHeight*-1)+ "px" ); topMoveSize = "+=" + options.itemHeight + "px" ;; leftMoveSize = 0; } else { options.rollingEl.css( "left" , (options.itemWidth*-1)+ "px" ); topMoveSize = 0; leftMoveSize = "+=" + options.itemWidth + "px" ;; } } bEffectProgress = true ; options.rollingEl.animate({ top: topMoveSize, left: leftMoveSize }, options.duration, function (){ if ( options.direction == "UP" || options.direction == "LEFT" ){ options.rollingEl.children().eq(0).remove(); options.rollingEl.css( "top" , "0px" ); options.rollingEl.css( "left" , "0px" ); } else if ( options.direction == "DOWN" || options.direction == "RIGHT" ){ options.rollingEl.children().eq(options.rollingEl.children().length-1).remove(); } bEffectProgress = false ; if ( btClicked == true && clickArr.length == 0 ){ autoRolling(); } // 롤링 후 콜백 함수 호출 if ( options.callbackMoveFun != null ) options.callbackMoveFun( "" + options.direction, options.rollingEl, Number(options.rollingEl.children().eq(0).attr( "idx" ))); }); } }; })(jQuery); |
이미지 롤링(Image rolling)
2012. 1. 31. 13:22