2014-06-12 34 views
0

This script搜索並抓取集合中最接近的元素,並根據您按下的鍵來滾動。它被設置爲在IP.Board論壇上滾動到下一個和前一個post_block元素。它也通過其他鍵跳過頁面。在Chrome中工作但不在Firefox中的Userscript

它的工作原理與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進行滾動,而Chrome使用document.body。

我不得不使用它們才能在所有瀏覽器中使用它。

相關問題