为什么我的滚动功能不能正常工作?

时间:2017-04-29 18:58:24

标签: javascript jquery scroll

有人可以告诉我为什么我的代码无效吗?我觉得我已经尝试了一切来排除故障,但我不明白这是什么问题。

   $(document).ready(function () {
function load_arrow {
	$('.fa.fa-caret-square-o-down').addClass('load');

};
$(window).scroll(function () {
	function get_window_position {
		var $window = $(window);
		var window_top_position = $window.scrollTop();
		var window_height = $window.height();
		var window_bottom = (window_top_position + window_height);
		var $element = $('.element');
		var element_top = $element.offset().top;
		var element_height = $element.height();
		var element_bottom = (element_top + element_height);
		if ((window_bottom > element_top) && (window_top_position < element_bottom)) {
			$('.element').addClass('load');
		};
		else {
			$('.element')removeClass('load');
		};
	};
});
});

1 个答案:

答案 0 :(得分:0)

您的代码充满了语法错误。打开你的控制台(F12),它必须大喊大叫。

$(document).ready(function () {
    function load_arrow { // Should be function load_arrow() {
        $('.fa.fa-caret-square-o-down').addClass('load');

    };
    $(window).scroll(function () {
         // A function defined in a function and then never called...
        function get_window_position { // <---- Should be get_window_position() anyway.
            var $window = $(window);
            var window_top_position = $window.scrollTop();
            var window_height = $window.height();
            var window_bottom = (window_top_position + window_height);
            var $element = $('.element');
            var element_top = $element.offset().top;
            var element_height = $element.height();
            var element_bottom = (element_top + element_height);
            if ((window_bottom > element_top) && (window_top_position < element_bottom)) {
                $('.element').addClass('load');
            }; // <---------------  Syntax error, remove the ;
            else {
                $('.element')removeClass('load');
            };
        };
    });
});