(function($){ $.fn.news_ticker = function(options){ var defaults = { term: 4000 ,speed: 500 ,moveH: 32 ,easing: null }, settings = $.extend({}, defaults, options); this.each(function(){ var $this = $(this); var $items; var $btn_prev; var $btn_next; var $btn_play; var $btn_pause; var nCurrentIndex; var total; var nTimerID; var auto; // 요소 초기화. init(); // 위치 설정하기. setPosition(); // 뉴스리스트 움직이기. playLoop(); // 버튼 설정 initMoveBtn(); //뉴스리스트 이벤트 initItemListEvent(); // 요소 초기화. function init(){ // 계속해서 사용할 요소들이기 때문에 전역변수에 담아 둔다. $items = $this.find("ul li"); $btn_prev = $this.find(".roll_prev"); $btn_next = $this.find(".roll_next"); $btn_play = $this.find(".roll_play"); $btn_pause = $this.find(".roll_pause"); nCurrentIndex = 0; nTimerID = 0; total = $items.length; auto = true; } // 뉴스리스트 위치를 초기화 function setPosition(){ // 모든 뉴스리스트의 위치값을 출력화면 영역에서 보이지 않도록 만든다. $items.css({top:settings.moveH}); // 첫번째 리스트를 화면에 활성화 시켜준다. $items.eq(0).css({top:0}); } // 상하버튼 설정 function initMoveBtn(){ //이전 $btn_prev.mouseenter(function(){ if(auto) stopLoop(); }).mouseleave(function(){ if(auto) playLoop(); }).click(function(){ on_StartMovePrev(); return false; }); // 다음 $btn_next.mouseenter(function(){ if(auto) stopLoop(); }).mouseleave(function(){ if(auto) playLoop(); }).click(function(){ on_StartMoveNext(); return false; }); //플레이 $btn_play.click(function(){ playLoop(); return false; }); //정지 $btn_pause.click(function(){ stopLoop(); auto = false; return false; }); } //뉴스리스트 focus function initItemListEvent(){ $items.find('a').on({ 'focusin':function (event) { if(auto) stopLoop(); showBannerAt($(this).parent().index(), 1, 'static'); } ,'focusout':function (event) { if(auto) playLoop(); } }); } //인터벌 function playLoop(){ if(nTimerID > 0) return; if(total > 1) { nTimerID = setInterval(on_StartMoveNext, settings.term); auto = true; } } function stopLoop(){ // alert('stoploop'); if(auto){ clearInterval(nTimerID); nTimerID = 0; } } // 다음 배너 계산하기. function on_StartMoveNext(){ if(total <= 1) return; if(nCurrentIndex + 1 >= total) showBannerAt(0, 1); else showBannerAt(nCurrentIndex+1, 1); } // 이전 배너 계산하기. function on_StartMovePrev(){ if(total <= 1) return; if(nCurrentIndex - 1 < 0) showBannerAt(total - 1, -1); else showBannerAt(nCurrentIndex - 1, -1); } // nIndex에 해당하는 배너를 현재배너로 활성화시킴. function showBannerAt(nIndex, dir, state){ // 현재배너를 구한다. var $current = $items.eq(nCurrentIndex); // 다음배너를 구한다. var $next = $items.eq(nIndex); if(state == 'static'){ // 현재배너를 위쪽으로 움직이기. $current.stop(true, false).css({top:(-settings.moveH * dir) + "px"}); // 다음배너를 움직이기 전에, 다음배너위치의 시작위치 설정하기. $next.stop(true, false).css({top:0}); }else{ // 현재배너를 위쪽으로 움직이기. $current.stop(true, false).animate({top:(-settings.moveH * dir) + "px"}, settings.speed ,settings.easing ); // 다음배너를 움직이기 전에, 다음배너위치의 시작위치 설정하기. $next.stop(true, false).css({top:settings.moveH * dir}).animate({top:0}, settings.speed ,settings.easing ); } // 현재배너 index값을 업데이트 시켜준다. nCurrentIndex = nIndex; /*console.log("nCurrentIndex" + nCurrentIndex);*/ } }); return this; } })(jQuery);