[英]How to rewrite in pure JavaScript code from JQuery code?
我在使用 JavaScript 時遇到了一些問題。
var $animation_left_right_elements = $('.bowl');
function check_if_facilites_in_view() {
var window_height = $window.height();
var window_top_position = $window.scrollTop();
var window_bottom_position = (window_top_position + window_height);
$.each($animation_left_right_elements, function() {
var $element = $(this);
var element_height = $element.outerHeight();
var element_top_position = $element.offset().top;
var element_bottom_position = (element_top_position + element_height);
//check to see if this current container is within viewport
if ((element_bottom_position >= window_top_position) &&
(element_top_position <= window_bottom_position)) {
$element.addClass('animationToRight');
} else {
$element.removeClass('animationToRight');
}
});
}
這是我的 JQuery 代碼。
我檢查了這個問題convert jquery each function to pure javascript ,但不能解決我的問題。
我希望有人能幫助我。 先感謝您。
更新
這是我試過的代碼。
var animation_left_right_elements = document.querySelectorAll('bowl');
function check_if_facilites_in_view() {
var window_height = window.innerHeight;
var window_top_position = $window.scrollY;
var window_bottom_position = (window_top_position + window_height);
animation_left_right_elements.forEach(function() {
var element = this;
var element_height = element.outerHeight();
var element_top_position = element.offset().top;
var element_bottom_position = (element_top_position + element_height);
//check to see if this current container is within viewport
if ((element_bottom_position >= window_top_position) &&
(element_top_position <= window_bottom_position)) {
element.addClass('animationToRight');
} else {
element.removeClass('animationToRight');
}
});
}
@GrafiCode 說回答我自己的問題對我有好處。 這是我的答案。
var animation_left_right_elements = document.querySelectorAll('.bowl');
function check_if_facilites_in_view() {
var window_height = window.innerHeight;
var window_top_position = window.scrollY;
var window_bottom_position = (window_top_position + window_height);
animation_left_right_elements.forEach(function(item) {
var element = item;
var element_height = element.scrollHeight;
var element_top_position = element.offsetTop;
var element_bottom_position = (element_top_position + element_height);
var video = element.firstElementChild.firstElementChild;
if ((element_bottom_position >= window_top_position) &&
(element_top_position <= window_bottom_position)) {
if (!element.classList.contains('animationToRight')) {
element.classList.add('animationToRight');
}
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.