通过编写适当的 JavaScript 函数来简化我的代码
Simplify my code by coding a proper javascript function
我有以下关于将参数传递给函数波纹管的问题。
目前完成的方式:
当鼠标悬停时,我在两个图像之间获得了过渡效果。我需要每个图像 1 个函数,因为每个图像的路径不同,我使用不同的类来区分所有图像。
Javascript:
</script>
$(function() {
$(".fade_image1")
.find("span")
.hide()
.end()
.hover(function() {
$(this).find("span").stop(true, true).fadeIn();
}, function() {
$(this).find("span").stop(true, true).fadeOut();
});
});
$(function() {
$(".fade_image2")
.find("span")
.hide()
.end()
.hover(function() {
$(this).find("span").stop(true, true).fadeIn();
}, function() {
$(this).find("span").stop(true, true).fadeOut();
});
});
</script>
.HTML:
<div>
<a href="#" class="fade_image1">Image<span></span></a>
</div>
<div>
<a href="#" class="fade_image2">Image<span></span></a>
</div>
.CSS:
.fade_image1{
display: inline-block;
position: relative;
text-indent: -9999px;
width: 303px;
height: 605px;
background: url(images/20130128_UK_Colour-Campaign_cropped_02.jpg) no-repeat;
}
.fade_image1 span {
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
background: url(images/images-rollover/20130128_UK_Colour-Campaign_cropped-rollover_02.jpg) no-repeat;
/*background-position: -50px 0;*/
}
.fade_image2{
display: inline-block;
position: relative;
text-indent: -9999px;
width: 608px;
height: 302px;
background: url(images/20130128_UK_Colour-Campaign_cropped_03.jpg) no-repeat;
}
.fade_image2 span {
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
background: url(images/images-rollover/20130128_UK_Colour-Campaign_cropped-rollover_03.jpg) no-repeat;
/*background-position: -50px 0;*/
}
所以这是我的问题,如何通过只有 1 个 javascript 函数适用于所有图像来简化它?我知道我需要将图像的路径传递到函数中,然后我可以使用 JQuery 的 css 内容,但我不知道更多:)
所以请帮我:)
这应该适合您的需求:
$(function() {
function setupFade(selector) {
$(selector)
.find("span")
.hide()
.end()
.hover(function() {
$(this).find("span").stop(true, true).fadeIn();
}, function() {
$(this).find("span").stop(true, true).fadeOut();
});
}
setupFade(".fade_image1");
setupFade(".fade_image2");
});
一个setupFade
函数,但两个调用。
这里有两个选项:
只需向图像添加一个类,然后使用此函数:
$(function() {
$(".fade_images")
.find("span")
.hide()
.end()
.hover(
function() {
$(this).find("span").stop(true, true).fadeIn();
}, function() {
$(this).find("span").stop(true, true).fadeOut();
}
);
});
添加的类:
<a href="#" class="fade_image1 fade_images">Image<span></span></a>
或者,检查类以 "fade_image"
开头的 <a>
标记,而不是向所有图像添加类:
$(function() {
$('a[class^=fade_image]') //<-- for all <a> tags whom class starts with `fade_img`. use *= instead of ^= if you need to filter for "Contains" instead of "Starts with".
.find("span")
// Etc...
相关文章:
- 如何将函数包装在函数中以避免代码重复
- 从var向代码隐藏函数传递值
- 为函数代码编写测试
- 如何简化jquery动画函数代码
- 用于删除数组中的零的Javascript函数代码
- 使用JavaScript中的高阶函数概念,用Python编写纯函数代码
- JQuery each() 函数代码即使在集合中没有元素时也在运行
- javascript.获取javascript函数位置或获取函数代码
- 部分函数代码是't已执行
- 什么'这是jQuery下面函数代码中的错误
- 为什么新函数(代码)比直接执行相同的代码更快
- 如何使用javascript调用ascx函数代码?DotNetNuke
- JQuery函数代码运行不正常
- Node.JS:MongoDB更新回调返回结果,返回函数代码
- 用nodejs在一定时间后运行函数/代码
- Jquery只处理第一个函数/代码块
- 如何确保我的dom操作函数/代码在dom被渲染后被调用
- 在jQuery中获取现成函数代码下面的元素
- Jquery函数返回的函数代码不是值
- 什么是'$(这个)'包含在下面的javascript函数代码中