간단한 자바스크립트 실행방법 (function)
본문
안녕하세요, 아주 간단한 질문인데요
아래 js파일을 불러오는 메인의 html이 있는데
버튼 클릭했을때 function nextPage 함수 실행하면서 특정 페이지로 이동하게 하려면
<button onclick="javascript:nextPage(11);"> 버튼이다 </button>
이렇게 하면 되는거 아닌가요? 펑션이 실행이 안되서 여쭤봅니다.
alert('hel') 이런걸로 실험하면 되는데 이상하네요
감사합니다~
아래는 js 파일의 시작부터 function nextPage() 부분 대부분입니다
var PageTransitions = (function() {
var $main = $( '#pt-main' ),
$pages = $main.children( 'div.pt-page' ),
$iterate = $( '#iterateEffects' ),
animcursor = 1,
pagesCount = $pages.length,
current = 0,
isAnimating = false,
endCurrPage = false,
endNextPage = false,
animEndEventNames = {
'WebkitAnimation' : 'webkitAnimationEnd',
'OAnimation' : 'oAnimationEnd',
'msAnimation' : 'MSAnimationEnd',
'animation' : 'animationend'
},
// animation end event name
animEndEventName = animEndEventNames[ Modernizr.prefixed( 'animation' ) ],
// support css animations
support = Modernizr.cssanimations;
function init() {
$pages.each( function() {
var $page = $( this );
$page.data( 'originalClassList', $page.attr( 'class' ) );
} );
$pages.eq( current ).addClass( 'pt-page-current' );
$( '#dl-menu' ).dlmenu( {
animationClasses : { in : 'dl-animate-in-2', out : 'dl-animate-out-2' },
onLinkClick : function( el, ev ) {
ev.preventDefault();
nextPage( el.data( 'animation' ) );
}
} );
$iterate.on( 'click', function() {
if( isAnimating ) {
return false;
}
if( animcursor > 67 ) {
animcursor = 1;
}
nextPage( animcursor );
++animcursor;
} );
}
function nextPage(options ) {
var animation = (options.animation) ? options.animation : options;
if( isAnimating ) {
return false;
}
isAnimating = true;
var $currPage = $pages.eq( current );
if(options.showPage){
if( options.showPage < pagesCount - 1 ) {
current = options.showPage;
}
else {
current = 0;
}
}
else{
if( current < pagesCount - 1 ) {
++current;
}
else {
current = 0;
}
}
var $nextPage = $pages.eq( current ).addClass( 'pt-page-current' ),
outClass = '', inClass = '';
switch( animation ) {
case 1:
outClass = 'pt-page-moveToLeft';
inClass = 'pt-page-moveFromRight';
break;
case 2:
답변 1
이 경우 nextPage 함수는 PageTransitions 함수에 의해 감싸지게 됩니다.
소스가 짤려서 안보이지만 클래스를 만드는 팩토리펑션으로 보이는데
var pageTransition = PageTransition(); 으로 생성하는 부분이 없나요?
만약 있다면 window.pageTransition.nextPage(11); 뭐 이런식으로 호출하면 될 것 같네요.
답변을 작성하시기 전에 로그인 해주세요.