如何从 JQuery 代码重写成纯 JavaScript 代码?
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 代码。
我检查了这个问题,但无法解决我的问题。
我希望有人能帮助我。
提前谢谢你。
更新
这是我试过的代码。
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');
}
}
});
}
我在使用 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 代码。
我检查了这个问题
我希望有人能帮助我。
提前谢谢你。
更新
这是我试过的代码。
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');
}
}
});
}