单击鼠标,通过将类名从一个元素传递到下一个元素来更改图像
Change image on click of mouse by passing class name from one element to the next
我有一种情况,我想在单击鼠标时更改背景图像。我想从一个element
中删除class
,并将相同的class
添加到下一个element
。然后,基于特定的class
名称,我想使用jquery
执行fadeIn
和fadeOut
。然而,当我使用class
时,只有第一个图像被改变。当我点击第二张图片时,它不会改变。但当我用div
替换class
时,每次单击时背景图像都会发生变化,直到到达最后一个图像。有人能帮我理解我错过了什么吗。非常感谢。
$(function(){
$("div").not(".active").hide();
$(".active").click(function(){ //When I change .active to div, it works.
$(this).fadeOut(function(){
$(this).removeClass("active");
$(this).next().addClass("active");
$(this).next().fadeIn()
})
})
})
body, html{
height: 100%;
width:100%;
background-color: yellow;
}
#one{
background:url("../Images/one.jpg") fixed center no-repeat;
width:100%;
height: 100%;
}
#two{
background:url("../Images/two.jpg");
width:100%;
height: 100%;
}
#three{
background:url("../Images/three.jpg");
width:100%;
height: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
<div id="one" class="active"></div>
<div id="two"></div>
<div id="three"></div>
</body>
您需要使用on()
而不是click()
,因为您是动态添加类的。
$(function(){
$("div").not(".active").hide();
$("body").on('click', '.active', function(){ //When I change .active to div, it works.
$(this).fadeOut(function(){
$(this).removeClass("active");
$(this).next().addClass("active");
$(this).next().fadeIn()
})
})
})
也许您需要使用.delegate
这样更改代码:
$('body').delegate('.active', 'click', function() {
$(this).fadeOut(function() {
$(this).removeClass("active");
$(this).next().addClass("active");
$(this).next().fadeIn()
})
})
或者在body
上更改绑定click
以判断e.target
请参阅此处的详细信息delegate
您可以这样做:
-
首先将特定的类
.bg-div
添加到这些div中,因为用这个$("div")
瞄准ALLdiv是非常通用的,它会隐藏除活动div之外的所有内容,以防您有其他div,它们会被隐藏。 -
隐藏类为
.bg-div
的所有div,但具有.active
的div除外。 -
单击事件时,您仍然可以以
bg-div
div为目标,因为只有一个具有.active
类,因为所有其他bgDivs
都已从上一步中隐藏。fadeOut()
这个。 -
将计数器CCD_ 26增加一。
- 现在,如果
index
的值等于bgDivs
的长度-数字-,则将coutner重置为0
,这是在显示最后一个bg-div
时使用的 fadeIn()
下一个div
jsFiddle 1
$(function() {
var bgDivs = $(".bg-div"),
index = 0;
bgDivs.not(".active").hide();
bgDivs.on('click', function() {
$(this).fadeOut('fast');
index += 1;
// if the counter value is less than the number of bgDivs, keep this value
// else, set it to 0 and start over because we're on the last div
index = (index < bgDivs.length) ? index : 0;
$(bgDivs[index]).fadeIn('fast');
});
})
body,
html {
height: 100%;
width: 100%;
margin: 0;
padding: 0;
background-color: yellow;
}
.bg-div {
width: 100%;
height: 100%;
}
#one {
background: url("//dummyimage.com/1000x700?text=one") fixed center no-repeat;
background-size: cover;
}
#two {
background: url("//dummyimage.com/1000x700?text=two") fixed center no-repeat;
background-size: cover;
}
#three {
background: url("//dummyimage.com/1000x700?text=three") fixed center no-repeat;
background-size: cover;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<body>
<div id="one" class="bg-div active"></div>
<div id="two" class="bg-div"></div>
<div id="three" class="bg-div"></div>
</body>
更新1:
您会注意到,在切换背景div时,有一段时间会出现水平滚动条,这是因为这些div堆叠在一起,但当隐藏时没有问题,问题只在切换后台div时出现,要解决此问题,请将这些行添加到.bg-div
CSS:
position:absolute;
top:0;
left:0;
jsFiddle 2
更新2:
如果在.bg-div
CSS中添加泛型display:none;
,然后在#one
规则中添加display:block
,则可以完全摆脱.active
类,而不依赖它。这样你就不需要这条线了:
bgDivs.not(".active").hide();
jsFiddle 3
这很有用,不仅因为代码更少,而且当页面从一开始第一次加载时,除了第一个之外的后台div都会被隐藏,因为它在CSS中,而不是等到js脚本将它们隐藏起来
- 使用clickToggle并在单击另一个元素时关闭元素
- 表追加而不附加最后一个元素
- 滚动到容器中的下一个元素-几乎到了
- 是否有一个JS/jQuery函数可以获取某个类的每个元素的ID
- 使用CSS或js,使用动态选择器选择任意li的下一个元素
- 按我自己的类克隆另一个元素的内容和顺序
- Javascript获取上一个元素的内容
- 如何通过HTML+CSS中的另一个元素Selector更改元素的属性值
- 在数组中的一个元素上设置多个值
- 指令的模板必须只有一个根元素:With restrict E&替换true
- HTML JavaScript,我如何能够通过给每个元素一个不同的Id来使用JavaScript使这个函数工作
- 如何创建一个独立的 DOM 元素?一个不从父母那里继承 css 等
- 如何给图像元素一个“;选择“;使用Raphael.js查看
- 如何显示隐藏的
- 元素一个接一个或全部同时
- 使用"span"创建一个自定义的基于web的文本编辑器;元素——一个坏主意
- 引导旋转木马传递元素一个接一个
- 给所有元素一个匹配数组内容的ID
- 我如何得到Json数组元素一个接一个的jquery上不同的id'
- 一次淡入附加元素一个
- 如何使用AngularJS检索外部网站的内容(一个元素一个元素)