2016-11-18 141 views
0

我使用這個函數來設置一些元素的轉換屬性,但是在firefox中動畫並不那麼流暢,而且當窗口大小更大時(在任何瀏覽器中),它都不那麼流暢。我讀了很多事情的博客上說,我可以使用requestAnimationFrame更平滑的動畫,但我不明白我如何在我的函數內部實現它。可以有人解釋我如何在我的函數中使用它嗎?using requestAnimationFrame

function sectionMovement(delay,section) { 
    setTimeout(function() { 
    var val = ((v.sectionIndex + 1) > v.leavingSection) ? 
     val = 100 : 
     val = 0; 
    document.getElementById("sec_"+section+"").style.transform = "translateY(-"+val+"%)" 
    }, delay); 
}; 
+0

你是怎麼調用這個函數的? –

+0

for循環內部 - > sectionMovement(i * 750,((v.sectionIndex + 1)> v.leavingSection)?(SIV + 1):SIV) – Matija

+0

有你的答案。 https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame –

回答

0

事情是這樣的:

function updateSection(selector) { 
    var elem = document.getElementById("sec_" + section); 
    return function step() { 
    var val = ((v.sectionIndex + 1) > v.leavingSection) ? // not sure what 'v' is? 
     100 : 
     0; 

    elem.style.transform = "translateY(-"+val+"%)"; 
    requestAnimationFrame(step); 
    } 
} 

var sel = "myElementId"; 
requestAnimationFrame(updateSection(sel)); 

您也可能會需要一個外部變量來覈對來判斷何時停止動畫。

+0

「v」是對象... – Matija

+0

@Matija除了你沒有在你發佈的代碼中的任何其他地方引用它。這並不能解釋*它是什麼。它的JavaScript。 *一切*都是一個對象。 –

+0

對不起,我非常感謝你的幫助,那些「...」是隨機字符,所以我可以提交評論 – Matija