我正在嘗試將jquery函數更改爲vanilla JS,並且我有複製它的問題。我修復了一些錯誤,但現在我完全陷入了困境。如何將函數應用於所選元素,將jquery代碼轉換爲JS並獲取類型錯誤
jQuery代碼
jQuery(window).load(function() {
var windowHeight, windowScrollPosTop, windowScrollPosBottom = 0;
function calcScrollValues() {
windowHeight = jQuery(window).height();
windowScrollPosTop = jQuery(window).scrollTop();
windowScrollPosBottom = windowHeight + windowScrollPosTop;
}
jQuery.fn.revealOnScroll = function (direction, speed) {
return this.each(function() {
var objectOffset = jQuery(this).offset();
var objectOffsetTop = objectOffset.top;
if (!jQuery(this).hasClass("hidden")) {
// if argument is "right"
if (direction == "right") {
jQuery(this).css({
"opacity": 0,
"right": "700px",
"position": "relative"
});
// if argument is "left"
} else {
jQuery(this).css({
"opacity": 0,
"right": "-700px",
"position": "relative"
});
}
jQuery(this).addClass("hidden");
}
if (!jQuery(this).hasClass("animation-complete")) {
// if the page has been scrolled far enough to reveal the element
if (windowScrollPosBottom > objectOffsetTop) {
jQuery(this).animate({"opacity": 1, "right": 0}, speed).addClass("animation-complete");
} // end if the page has scrolled enough check
} // end only reveal the element once
});
}
function revealCommands() {
jQuery(".title").revealOnScroll("right", 1200);
}
calcScrollValues();
revealCommands();
// run the following on every scroll event
jQuery(window).scroll(function() {
calcScrollValues()
revealCommands();
}); // end on scroll
});
這項工作,如果你取消註釋筆jQuery代碼。
這裏是JS代碼
function calcScrollValues() {
windowHeight = screen.height;
windowScrollPosTop = document.body.scrollTop;
windowScrollPosBottom = windowHeight + windowScrollPosTop;
};
window.onload = function() {
var windowHeight, windowScrollPosTop, windowScrollPosBottom = 0;
function revealOnScroll(direction, speed) {
return this.each(function() {
var objectOffset = this.getBoundingClientRect();
var objectOffsetTop = getBoundingClientRect();
if (!this.classList.contains("hidden")) {
// if argument is "right"
if (direction == "right") {
this.setAttribute('style', 'opacity:0; right:700px; position: relative');
}
;
// if argument is "left"
} else {
this.setAttribute('style', 'opacity:0; right:700px; position: relative');
}
;
this.classList.add("hidden");
});
if (!this.classList.contains("animation-complete")) {
// if the page scrolled far enough to reveal the element
if (windowScrollPosBottom > objectOffsetTop) {
this.setAttribute('style', 'opacity:1; right:0; transition: speed + "ms"');
this.classList.add("animation-complete");
} // end if the page has scrolled enough check
}
; // end only reveal the element once
}
}
function revealCommands() {
document.querySelector(".title").revealOnScroll("right", 1200);
}
calcScrollValues();
revealCommands();
// run the following on every scroll event
window.addEventListener('scroll', function() {
calcScrollValues()
revealCommands();
});
我知道「返回this.each(函數(){}」的一部分,所以無法在JS工作,不知道如何讓JS代碼。這
而另一個主要問題是這個
'document.querySelector( 「.title僞 」)revealOnScroll(「 右」,1200);' 顯然給出了一個類型錯誤 「不是一個函數」
可能還有更多這個問題,但這是我堅持的一個。
Codepen:
http://codepen.io/damianocel/pen/yYKyaN
非常感謝你這一點。它運行,但有一個缺陷,如果你現在嘗試codepen(我正在使用p.test元素來測試,其餘的是單獨的代碼),你會看到在revealOnScroll中傳遞的元素垂直而不是來自屏幕旁邊。這實際上是我無論如何要做的下一件事,但現在我想知道爲什麼會發生這種情況。看起來問題在這裏: 「var objectOffset = element。getBoundingClientRect(); VAR objectOffsetTop = getBoundingClientRect();」 –
我想問一些細節,你所提供的代碼,所以,我們添加revealOnScroll方法的節點列表原型,並通過方向和動畫的速度作爲參數傳遞給我們使用節點列表作爲這就是從querySelectorAll返回的東西,到目前爲止我得到它然後, Array.prototype被擴展到使用forEach和使用call(),現在我不明白第一個參數調用(),「自我「什麼是」自我「? –
@damianocelent複製和粘貼錯誤,只需將其替換爲'this'即可。 –