Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 为2个div运行一个带有变量的代码_Jquery_Html_Css - Fatal编程技术网

Jquery 为2个div运行一个带有变量的代码

Jquery 为2个div运行一个带有变量的代码,jquery,html,css,Jquery,Html,Css,尝试为两个单独的div运行相同的函数。当用户滚动一定距离(偏移量)时,它应该应用“header hide”/“header-show”类 一个div/元素(无变量)的工作代码为 但是,当我尝试使用变量将其应用于2个div时,它不起作用 $(文档).ready(函数($){ 函数reusuableAnimationFunc(elementName、offset、hideClass、showClass){ $animation=$(elementName); $(窗口)。滚动(函数(){ ($(t

尝试为两个单独的div运行相同的函数。当用户滚动一定距离(偏移量)时,它应该应用“header hide”/“header-show”类

一个div/元素(无变量)的工作代码为

但是,当我尝试使用变量将其应用于2个div时,它不起作用

$(文档).ready(函数($){
函数reusuableAnimationFunc(elementName、offset、hideClass、showClass){
$animation=$(elementName);
$(窗口)。滚动(函数(){
($(this.scrollTop()>offset)?$animation.addClass(hideClass).removeClass(showClass):
$animation.addClass(showClass).removeClass(hideClass);
});
});
reusuableAnimationFunc('header',70',header hide','header show')
reusuableAnimationFunc(“#top btn”,300,“元素隐藏”,“元素显示”)
.element隐藏,
.标题隐藏{
不透明度:0;
可见性:隐藏;
}
.元素秀,
.标题显示{
不透明度:1;
能见度:可见;
}

标题
副标题

javascript函数中存在语法错误。您正在使用
}关闭reusuableAnimationFunc函数,它应该是
}
。此外,您还需要在最后关闭
document.ready()
函数。该功能永远不会关闭

$(document).ready(function($) {    
          function reusuableAnimationFunc(elementName, offset, hideClass, showClass) {
            $(window).scroll(function() {
            $animation = $(elementName);                
              ($(this).scrollTop() > offset) ? $animation.removeClass(showClass).addClass(hideClass):
                $animation.addClass(showClass).removeClass(hideClass);
            });
          }    
        reusuableAnimationFunc('header', 70, 'header-hide', 'header-show');
        reusuableAnimationFunc('#top-btn', 300, 'element-hide', 'element-show');
        });

p.S move
$animation=$(elementName)
inside windows.scroll函数

你能添加你的HTML以便我们进行实际调试吗?当然,我不确定是否有必要。我在执行你的代码时遇到了问题。你能修复你的MVCE吗?我可以发布它的原始工作代码,但我无法让这段代码正常工作,因此这个问题也是一个更重要的性能提示,在父级上只使用一个滚动功能来检查滚动并检查其位置,最好只进行一次检查,而不是两次哦,好的,我的印象是,这是在功能之外。但你是对的
$(document).ready(function($) {    
          function reusuableAnimationFunc(elementName, offset, hideClass, showClass) {
            $(window).scroll(function() {
            $animation = $(elementName);                
              ($(this).scrollTop() > offset) ? $animation.removeClass(showClass).addClass(hideClass):
                $animation.addClass(showClass).removeClass(hideClass);
            });
          }    
        reusuableAnimationFunc('header', 70, 'header-hide', 'header-show');
        reusuableAnimationFunc('#top-btn', 300, 'element-hide', 'element-show');
        });