将变量传递给函数

用户3550879

我正在尝试在我的函数中执行 2 组变量(在我的functions.php 中)

编辑 - 这是将 header-show/header-hide 类应用于一个 div(标题)的工作代码

jQuery(document).ready(function($){


// adjust this number to select when your button appears on scroll-down
var offset = 70,

// bind with the button link
$animation = $('header');

// apply animation
$(window).scroll(function(){
    ( $(this).scrollTop() > offset ) ? $animation.addClass('header-hide').removeClass("header-show"):
    $animation.addClass('header-show').removeClass("header-hide");
});

});

我想为第二个 div (#top-btn) 重用代码,但无法让它工作。我所拥有的如下:

jQuery(document).ready(function($){

    function reusuableAnimationFunc(elementName, offset, hideClass, showClass) {
    $animation = $(elementName);

    $(window).scroll(function(){
      ( $(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-hide', 'element-show')

});

不确定它是否编写正确,或者我是否需要将它的一部分放入我的 html 中。我只想为两个不同的 div 运行相同的函数

睡对了

该函数在 jQuery.ready 之外不可访问

您的函数应该在全局范围内。因此,您必须删除此“jQuery(document).ready(function($){...}”。

用这个替换它:

function reusuableAnimationFunc(elementName, offset, hideClass, showClass) {
        $animation = $(elementName);

        $(window).scroll(function() {
            ($(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-hide', 'element-show')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章