2014-06-12 3 views
0

This script은 컬렉션의 가장 가까운 요소를 검색하여 가져 오는 키에 따라 스크롤합니다. IP.Board 포럼의 다음 및 이전 post_block 요소로 스크롤하도록 설정됩니다. 또한 다른 키와 함께 페이지를 건너 뜁니다.Chrome에서 작동하지만 Firefox에서는 작동하지 않는 사용자 스크립트

Chrome에서 예상대로 작동하지만 Firefox에서는 스크롤이 작동하지 않습니다. 페이지 점프는 않습니다. 그것은 어떤 오류도 던지지 않으며, 단지 작동하지 않습니다.

관련 코드 : 무엇이 잘못되었는지에

// Questionable functions 
function getPosition(element) { 
    var xPosition = 0, 
    yPosition = 0; 

    while (element) { 
     xPosition += (element.offsetLeft 
      + element.clientLeft); 
     yPosition += (element.offsetTop 
      + element.clientTop); 
     element = element.offsetParent; 
    } 
    return {x: xPosition, y: yPosition}; 
} 

Math.easeInOutQuad = function (time, start, change, duration) { 
    time /= duration/2; 
    if (time < 1) { 
     return change/2 * time * time + start; 
    } 
    time--; 
    return -change/2 * (time * (time - 2) - 1) + start; 
}; 

function scrollTo(element, to, duration) { 
    var start = element.scrollTop, 
    change = to - start, 
    currentTime = 0, 
    increment = 1; 

    var animateScroll = function() { 
     var val = Math.easeInOutQuad(currentTime, start, change, duration); 
     element.scrollTop = val; 
     currentTime += increment; 
     if (currentTime <= duration) { 
      setTimeout(animateScroll, increment); 
     } 
    }; 

    animateScroll(); 
} 

function scrollToNext(context, collection, dir) { 
    var item, 
    i = 0; 

    switch (dir) { 
     case 'up': 
      for (i = collection.length - 1; i >= 0; i--) { 
       item = collection[i]; 

       if (getPosition(item).y < context.scrollTop - 2) { 
        scrollTo(context, getPosition(item).y, 30); 
        break; 
       } 
      } 
     break; 
     case 'down': 
     default: 
      for (i = 0; i < collection.length; i++) { 
       item = collection[i]; 

       if (getPosition(item).y > context.scrollTop + 2) { 
        scrollTo(context, getPosition(item).y, 30); 
        break; 
       } 
      } 
     break; 
    } 
} 

// Trigger in keydown handler 
if (event.keyCode === shortcuts['next_post'] && checkModifiers('next_post', event, shortcuts)) { 
    event.preventDefault(); 
    scrollToNext(document.body, document.getElementsByClassName('post_block'), 'down'); 
} else if (event.keyCode === shortcuts['previous_post'] && checkModifiers('previous_post', event, shortcuts)) { 
    event.preventDefault(); 
    scrollToNext(document.body, document.getElementsByClassName('post_block'), 'up'); 
} 

어떤 아이디어?

+0

_StackOverflow_는 전체 스크립트를 검토하지 않으려 고 시도한 함수 또는 함수 조합이 구체적으로 작동하지 않는 범위로 좁히려 고 시도한 다음 여기에 코드를 게시하십시오. –

+0

시도 할 것이지만 뭔가가 있다고 생각합니다. 함수가 선언 된 순서와 관련있다. 우리가 보게 될 것이다. – Makaze

답변

0

발견. Firefox는 document.documentElement를 사용하여 스크롤하는 동안 document.body를 사용합니다.

모든 브라우저에서 작동하려면이 두 가지 방법을 모두 사용해야했습니다.

관련 문제