简化每个id的多个函数
Simplify multiple function for each IDs
我创建了一个简单的代码,使用JQuery和CSS滑动显示切换图像。当你点击另一个图像时,我使用了多个功能来隐藏/切换其他图像。有没有一种方法可以减少这个的使用?另外,当我第三次单击不同的图像时,我必须双击它才能滑回。我的代码. .及其JSFiddle
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js">
</script>
<script>
$(document).ready(function () {
$("#img1").toggle(
function () {
$("#img1").css({"transform": "translate3d(-100px, 0px, 0px)"});
$("#img2").css({"transform": "translate3d(0px, 0px, 0px)"});
$("#img3").css({"transform": "translate3d(0px, 0px, 0px)"});
},
function () {
$("#img1").css({"transform": "translate3d(0px, 0px, 0px)"});
}
);
$("#img2").toggle(
function () {
$("#img2").css({"transform": "translate3d(-100px, 0px, 0px)"});
$("#img1").css({"transform": "translate3d(0px, 0px, 0px)"});
$("#img3").css({"transform": "translate3d(0px, 0px, 0px)"});
},
function () {
$("#img2").css({"transform": "translate3d(0px, 0px, 0px)"});
}
);
$("#img3").toggle(
function () {
$("#img3").css({"transform": "translate3d(-100px, 0px, 0px)"});
$("#img2").css({"transform": "translate3d(0px, 0px, 0px)"});
$("#img1").css({"transform": "translate3d(0px, 0px, 0px)"});
},
function () {
$("#img3").css({"transform": "translate3d(0px, 0px, 0px)"});
}
);
});
</script>
</head>
<title></title>
<style>
.cardcont img {
transition: transform .2s ease-in-out;
}
.cardcont #img1 {
position:absolute;
left:0;
top:0;
z-index:-1;
}
.cardcont #img2 {
position:absolute;
left:200px;
top:0;
z-index:-1;
}
.cardcont #img3 {
position:absolute;
left:400px;
top:0;
z-index:-1;
}
</style>
</head>
<body>
<div class="cardcont">
<img src="http://i65.tinypic.com/fokk9j.jpg" id="img1"/>
<img src="http://i65.tinypic.com/fokk9j.jpg" id="img2"/>
<img src="http://i65.tinypic.com/fokk9j.jpg" id="img3"/>
</div>
</body>
</html>
这是一个使用this
和.not
的短板。
(添加)
.cardcont img {
transform: translate3d(0px, 0px, 0px);
transition: transform .2s ease-in-out;
}
.active-image {
transform: translate3d(-100px, 0px, 0px)!important;
}
jquery $(function() {
$('img').click(function() {
$('img').not(this).removeClass('active-image');
$(this).toggleClass('active-image');
});
});
小提琴
https://jsfiddle.net/Hastig/98dxLy5c/注意
您可能希望更具体地针对图像,使用.cardcont img
作为Vincent建议的一种可能性。
是的,您可以使用.each()
函数来实现此功能,例如:
$(document).ready(function () {
$(".cardcont img").each(function(){
$(this).click(function(){
if($(this).hasClass('active')){
$(this).removeClass('active');
$(this).css({"transform": "translate3d(0px, 0px, 0px)"});
}else{
$(".cardcont img").css({"transform": "translate3d(0px, 0px, 0px)"});
$(this).css({"transform": "translate3d(-100px, 0px, 0px)"});
$(this).addClass('active');
}
});
});
});
这里小提琴
这样,你就不必修改你的HTML结构了。
文档: http://api.jquery.com/jquery.each/
相关文章:
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- Javascript复选框函数:;缺少:在属性id之后"
- javascript点击函数不;不适用于ID和Class
- 将参数中类似形式的ID传递给函数click()
- JavaScript-onClick将按钮ID或单击按钮的文本传递给函数
- 不使用id的javascript中的onfocus函数
- 如何在Jquery函数中为自动完成文本区域指定一个变量作为Id
- 使用jQuery来foreach iframe-src,并通过函数获取ID和操作
- 如何传递元素's ID作为使用getElementById的函数的参数
- 将外部函数返回的id传递给内部函数
- 如何在具有相同 ID 的 Javascript 中按排序函数对关联数组进行排序
- AngularJS-点击对象,访问它's ID&以该ID作为参数激发函数
- 在具有多个值的 PHP 函数中传递 ID
- 如何在 javascript 文件函数中找到 Radnotification id
- 按类划分的元素数组 - 类型错误:$.id 不是一个函数
- HTML span id 作为 JavaScript 函数参数
- 悬停时函数在具有相同 id 的多个元素中不起作用
- 是否可以循环javascript函数id
- 如何使用jQuery's";最接近的";具有相同类的多个切换的函数/id's
- 动态生成的元素的构造函数id