- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嘿伙计们,我想做一个像这样的函数:fadeIn()fadeIn() 函数不接受任何参数,但它会自动知道我正在谈论的元素。例子:我如何知道如何创建一个函数:
function make100HW(el){
$(el).animate({height:"100px",width:"100px"},400);
}
make100HW("#something");
//what I want is something like
// $("#something").make100HW(); without putting the element inside the parameters.
//just like $("#something").fadeIn();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="something" style="background:red">
</div>
最佳答案
要制作 jQuery 插件,您需要将函数添加到 jQuery.fn
对象(通常也可以通过 $.fn
获得,但是一个编写良好的插件不应该假设 $
指的是 jQuery)。在函数中,this
将是调用插件的 jQuery 集(不是单个 DOM 元素,因为它在事件处理程序回调等中)。
通常,插件会足够复杂,需要辅助功能。出于这个原因,并且因为您不应该依赖 $
,所以通常会看到封装在 IIFE 中的插件代码接受 $
作为参数并传入 jQuery
:
(function($) {
// Code using $ here, which is fine even though noConflict
// may have been used in the global context...
})(jQuery);
所以:
// Your plugin code
(function($) {
$.fn.make100HW = function make100HW() {
// 1. Use `this`, which is hte current jQuery set
// 2. Return `this`, for chaining (which animate does)
return this.animate({height:"100px",width:"100px"},400);
};
})(jQuery);
// Using it
$("#something").make100HW();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="something" style="background:red">
</div>
关于jquery - 如何制作一个像 fadeIn 这样不将元素作为参数的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47413082/
我正在努力编写我的第一个 jQuery 脚本。我的页面上有一个 DIV,该 DIV 设置为通过 CSS 隐藏。然后,我运行这个脚本以使其淡入、淡出,然后再次淡入: (function($) {
我刚刚学习了一些 jQUery,以便在我为酒店创建的网站上建立一个基本的图片库,但目前还没有计划。我明白了,所以箭头会在图像中循环(还没有动画),但我决定箭头应该在图像悬停时淡入,而不是淡出,但这不知
我有一个具有不同模块的交互式表单系统,其中每个模块的表单包含在不同的 div 元素内。当我从一个模块更改为另一个模块时,我经常这样做: $('#module_1').hide(); $('#modul
我想在滚动页面时淡入我的标题。这是我到目前为止所得到的,除了 .fadeIn() 之外一切正常。 $(document).scroll(function() { if( $(this).scr
我有一个 jquery 错误,我已经找了好几个小时了,我不知道出了什么问题......我有这段代码: $(document).ready(function(){ $('#ulPhotos a
我试图搜索并查看是否在此下方列出了任何内容,但我什么也没看到。我有一个使用 Isotope 布局的图库,它工作正常,但是在页面加载时,图库中的图像显示在页面中间,然后一旦加载就跳转到各自的位置。 我试
如果我执行更改表格尺寸的 fadeIn(),Firefox 会剪切新显示行的背景。 如果我在远离标签的地方单击焦点,然后单击返回,背景将恢复。 Chrome 中没有裁剪问题。我在 Firefox 26
现在,当我淡入叠加层 div 时,它会自动设置为 display: block;。但我希望它是 display: table。有没有像 fadein() 这样的 jQuery 函数可以做到这一点? j
我有这个函数,非常适合延迟加载。 panel.find('img[data-src]').each(function(){ element = $(this);
有人可以告诉我我做错了什么吗: 风格: .warning{border: 1px solid #F0AAAA; background:#FFBABA; color: #C90000;} 标记: A
当我单击切换按钮时,它会将元素的样式属性从“不显示”更改为“阻止”。现在,当发生这种情况时,我想为菜单的左侧位置设置动画。我有这样的代码,但工作有问题,而且,当它应该返回到右侧时,它出现在左侧.. v
我有这段代码,可以在一定的延迟后切换图像的不透明度: $(".pattern-overlay").css("background","black").delay(2000).queue(functio
我对 jQuery 很陌生,所以请耐心等待。 我想逐步淡入列表。但我的功能出了什么问题。请问有人可以帮助我吗? 非常感谢! 编辑:我检查了所有内容,没有发现任何错误。主体具有所需的类。 $(".sid
我希望有人能帮助我。我遇到以下问题: http://jsfiddle.net/zhPAF/ 标记: About Us
这是我的代码..我想要应用淡入淡出,我按照下面的方式做了,但淡入淡出没有应用 $(document).ready(function(){ var i = 1; $
我使用 jquery 加载和淡入目标 div 中的内容。这很好用,我喜欢它看起来很整洁。但是,如果您单击链接多次(快速)淡入内容,则内容似乎不会完全不透明。因此,如果我按链接两次(再次非常快),它只能
非常简单的 jQuery 元素作为实践。我已经设置了一个按钮(#btn),我想看看当我单击该按钮时是否可以淡入 Div。我设置了一个 Fiddle,代码本身有什么问题吗? 链接到JsFiddle .
我正在开发一个网站,我希望每次打开网页时都会淡入文本,所以我做了这样的事情: $(document).ready(function() { $('#title').fadeIn(100); }
请有人让我摆脱痛苦......我已经投入了一个又一个小时...... 我已经(这是缩写)创建了一个函数,该函数使用 append 将框添加到页面。问题是一旦添加了它们,淡入功能就不起作用了。 但是,如
与我在这里看到的其他帖子不同,我使用 jQuery fadeIn 来处理可变文本而不是图像。我看到它逐渐褪色,最后一个脉冲变成全彩。我希望它能逐渐淡出。我已将其发布在 jsFiddle 上:http:
我是一名优秀的程序员,十分优秀!