4

JQuery .animate() 함수를 사용하여 컨테이너 div에서 div를 슬라이드합니다. 이것은 Chrome에서 문제없이 작동하지만 Firefox 나 IE에서 시도 할 때 div가 엉망으로 엉망이되어 실제 슬라이드하지 않습니다. 나는 JavaScript에 익숙하지 않고 브라우저 호환성의 방식을 모르는 사람이 누구든지 올바른 방향으로 나를 가리킬 수 있습니까?JQuery .animate()는 Chrome에서만 작동합니다.

html로

<div id="slider"> 
    <div id="main" class="content"> 
    </div> 

    <div id="projects" class="content"> 
    </div> 

    <div id="about" class="content"> 
    </div> 

    <div id="contact" class="content"> 
    </div> 
</div> 

CSS의

#slider { 
    width: 100px; 
    height: 100px; 
    overflow: hidden; 
    position: relative; 
} 

#main { 
    background-color: red; 
    width: inherit; 
    height: inherit; 
    position: absolute; 
} 

#projects { 
    background-color: blue; 
    width: inherit; 
    height: inherit; 
    position: absolute; 
} 

#about { 
    background-color: yellow; 
    width: inherit; 
    height: inherit; 
    position: absolute; 
} 

#contact { 
    background-color: green; 
    width: inherit; 
    height: inherit; 
    position: absolute; 
} 

.content { 
    left: 0; 
    top: 0; 
} 

자바 스크립트

$(document).ready(function() { 

var contentWidth = '100px'; 

for(var i=0; i < 2; i++) { 
    $('.gallery' + (i + 1)).colorbox({ opacity:0.5 , rel:'gallery' + (i+1)}); 
} 

$('.content').css({ 
    position: 'absolute', 
    left: contentWidth 
}); 

$('#main').addClass('visible'); 
document.getElementById('main').style.left = "0"; 

$("li a").click(function() { 
    event.preventDefault(); 
    var $blockID = $($(this).attr('href')); 

    if ($blockID.hasClass('visible')) { return; } 

    $('.content.visible') 
    .removeClass('visible') 
    .animate({ left: '-=100px' }, { 
     duration: 'slow', 
     complete: function() { 
     $(this).css('left', '100px'); 
     } 
    } 
); 

$blockID 
    .addClass('visible') 
    .animate({ left: 0 }, {duration: 'slow'}); 
}); 

}); 

JSFiddle : http://jsfiddle.net/bwvVZ/

나는 또한 할 수 여기에 관련 코드입니다 문제의 사이트에 대한 링크를 제공하십시오. 규칙에 어긋나는 지 확실하지 않기 때문에 연기하겠습니다. 어떤 도움을 많이 주시면 감사하겠습니다!

답변

5

당신은 당신의 클릭 핸들러

$("li a").click(function(){ 
    event.preventDefault(); 
    //... 
}); 

에서 event 인수를 누락이

$("li a").click(function (event){ 
    event.preventDefault(); 
    //... 
}); 

DEMO해야한다.

+0

헤이 셰이크, 답변 해 주셔서 감사합니다. Chrome이 전혀 작동하지 않는 것은 이상합니다. 어쨌든, 다시 한번 감사드립니다! – Max

1

IE에서 직접 테스트 할 수는 없지만 Firefox를 수정하면 returnValue가 IE를 수정해야합니다. CSSDeck Test - 내 현재 위치에서 jsfiddle에 액세스 할 수 없습니다.

$("li a").click(function (event){ 
    event.returnValue = false; //ie 
    if(event.preventDefault) //prevents error if not found 
     event.preventDefault(); 

    var $blockID = $($(this).attr('href')); 

    ... 
+0

안녕하세요 Abrosia, 감사합니다, (이벤트 추가) 도왔습니다! Windows XP VM에서만 IE를 테스트 할 수 있지만, 그냥 (이벤트)가 추가 된 것처럼 작동합니다. returnValue는 IE의 이전 버전을 도움이됩니까? 다시 한 번 감사드립니다! – Max

+0

Microsft 웹 사이트는 이전 버전의 IE 용 레거시 코드라고 주장하고 "이전 버전"으로 간주되는 것을 나열하지 않습니다. 나는 이것이 8보다 작은 버전이 될 지원 사이클에 없을 수 있다고 생각한다. 또한 사이드 노트, 'e'와 같은 것이 아닌 'event'라는 이름을 사용해야한다. 그렇지 않으면 전역 이벤트 객체는 그렇지 않다. IE8에서 사용됩니다. – SimonDever

관련 문제