admin管理员组文章数量:1022982
I've looked around for an answer and nothing works for me. I want to be able to click a div and have the background change, then on the next click of the div I want it to change back again. The first part works, however I cannot figure out how to do the second part.
This is what works (using jQuery):
$(document).ready(function(){
$('.button').click(function() {
$('.icon').css("background-position", "0px 0px");
});
});
Any help would be appreciated, thanks.
I've looked around for an answer and nothing works for me. I want to be able to click a div and have the background change, then on the next click of the div I want it to change back again. The first part works, however I cannot figure out how to do the second part.
This is what works (using jQuery):
$(document).ready(function(){
$('.button').click(function() {
$('.icon').css("background-position", "0px 0px");
});
});
Any help would be appreciated, thanks.
Share Improve this question asked Apr 6, 2012 at 8:58 AnarcheiAnarchei 31 silver badge2 bronze badges3 Answers
Reset to default 5Rather than use the .css method, I remend using .toggleClass and put the relevant css into a class:
$('.icon').toggleClass('myClass');
your class definition:
.myClass { background-position: 0px 0px };
The .toggleClass method will add the class if it's not already present or remove it if it is.
I agree with reendations to use .toggleClass()
, but if you want to use .css()
try this:
$(document).ready(function(){
$('.icon').each(function(){
$(this).data('default-bg-pos', $(this).css('background-position'));
$(this).data('state', 0);
});
$('.button').click(function() {
$('.icon').each(function(){
if ($(this).data('state')) {
$(this).data('state', 0);
$(this).css('background-position', $(this).data('default-bg-pos'));
} else {
$(this).data('state', 1);
$(this).css("background-position", "0px 0px");
}
});
});
});
You should use a css class and toggle it.
.zero{
background-position: "0px 0px";
}
$(document).ready(function(){
$('.button').click(function() {
$('.icon').toggleClass("zero")
});
});
I've looked around for an answer and nothing works for me. I want to be able to click a div and have the background change, then on the next click of the div I want it to change back again. The first part works, however I cannot figure out how to do the second part.
This is what works (using jQuery):
$(document).ready(function(){
$('.button').click(function() {
$('.icon').css("background-position", "0px 0px");
});
});
Any help would be appreciated, thanks.
I've looked around for an answer and nothing works for me. I want to be able to click a div and have the background change, then on the next click of the div I want it to change back again. The first part works, however I cannot figure out how to do the second part.
This is what works (using jQuery):
$(document).ready(function(){
$('.button').click(function() {
$('.icon').css("background-position", "0px 0px");
});
});
Any help would be appreciated, thanks.
Share Improve this question asked Apr 6, 2012 at 8:58 AnarcheiAnarchei 31 silver badge2 bronze badges3 Answers
Reset to default 5Rather than use the .css method, I remend using .toggleClass and put the relevant css into a class:
$('.icon').toggleClass('myClass');
your class definition:
.myClass { background-position: 0px 0px };
The .toggleClass method will add the class if it's not already present or remove it if it is.
I agree with reendations to use .toggleClass()
, but if you want to use .css()
try this:
$(document).ready(function(){
$('.icon').each(function(){
$(this).data('default-bg-pos', $(this).css('background-position'));
$(this).data('state', 0);
});
$('.button').click(function() {
$('.icon').each(function(){
if ($(this).data('state')) {
$(this).data('state', 0);
$(this).css('background-position', $(this).data('default-bg-pos'));
} else {
$(this).data('state', 1);
$(this).css("background-position", "0px 0px");
}
});
});
});
You should use a css class and toggle it.
.zero{
background-position: "0px 0px";
}
$(document).ready(function(){
$('.button').click(function() {
$('.icon').toggleClass("zero")
});
});
本文标签: javascriptChange CSS on click and back again on next clickStack Overflow
版权声明:本文标题:javascript - Change CSS on click and back again on next click - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745498492a2153279.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论