简体   繁体   English

如何从 JQuery 代码改写成纯 JavaScript 代码?

[英]How to rewrite in pure JavaScript code from JQuery code?

I have some trouble using JavaScript.我在使用 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');
        }
    });
}

This is my JQuery code.这是我的 JQuery 代码。

I checked this question convert jquery each function to pure javascript , but cannot solve my problem.我检查了这个问题convert jquery each function to pure javascript ,但不能解决我的问题。
I want someone can help me.我希望有人能帮助我。 Thank you in advance.先感谢您。

Update更新

This is the code I tried.这是我试过的代码。

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 said it's good for me to answer my own question. @GrafiCode 说回答我自己的问题对我有好处。 This is my answer.这是我的答案。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM