2015-12-08 62 views
0

我一直在寻找通过jQuery在不同方向滑动。我在stackoverflow中找到了一个自定义函数。但我不知道如何使用它。如何在jQuery中使用自定义函数

$.fn.fadeSlideRight = function(speed,fn) { 
return $(this).animate({ 
    'opacity' : 1, 
    'width' : '750px' 
},speed || 400, function() { 
    $.isFunction(fn) && fn.call(this); 
}); 

}

请告诉我在哪里贴吧以及如何使用它。提前致谢 。

回答

0

你只需把它放在你使用之前(可能是靠近你的脚本的开头),并可以调用它像这样:

$.fn.fadeSlideRight = function(speed,fn) { 
 
    return $(this).animate({ 
 
    'opacity' : 1, 
 
    'width' : '750px' 
 
    },speed || 400, function() { 
 
    $.isFunction(fn) && fn.call(this); 
 
    }); 
 
}; 
 

 
$("#test").fadeSlideRight(500,function(){ 
 
    alert("fadeSlideRight callback"); 
 
    });
#test{ 
 
    opacity:0; 
 
    width:100px; 
 
    background-color:red; 
 
    height:50px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="test">test</div>

+0

它的工作很好,当我按下“运行代码片段“。但它不会当我在我的电脑 –

+0

@ M.Rashid你可以发布你的代码如何使用它?包括您尝试使用它的元素的CSS以及该元素上的实际函数调用。并在控制台中有任何错误? – spaniol6

+0

我只是把你的代码分解成几块并粘贴到外部的css和脚本页面,但是失败了。但script.js,style.css和jquery库正在加载。但该功能不起作用 –