admin管理员组文章数量:1326670
How do i turn this into a toggle button? So that when the button is clicked, it toggles the light and dark background.
$(document).ready(function(){
$("button").click(function(){
$(".content1").css("background-image", "url(../images/light.png)");
});
});
CSS
.content1 {
background-image: url('../images/dark.png');
}
Thanks
How do i turn this into a toggle button? So that when the button is clicked, it toggles the light and dark background.
$(document).ready(function(){
$("button").click(function(){
$(".content1").css("background-image", "url(../images/light.png)");
});
});
CSS
.content1 {
background-image: url('../images/dark.png');
}
Thanks
Share Improve this question edited Sep 8, 2016 at 14:25 bipen 36.6k9 gold badges50 silver badges62 bronze badges asked Sep 8, 2016 at 14:23 user5635857user5635857 1- 2 toggle a class... – epascarello Commented Sep 8, 2016 at 14:23
1 Answer
Reset to default 7Toggle a class instead of setting the background directly. It will be easier to maintain and to know what state the code is in.
$(document).ready(function() {
$("button").click(function() {
$(".content1").toggleClass("active");
});
});
.content1 {
background-image: url('../images/dark.png');
background-color: red;
}
.content1.active {
background-image: url('../images/light.png');
background-color: green;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button>Clicky</button>
<div class="content1"> Hello </div>
本文标签: javascriptChange background image with a toggle (jQuery)Stack Overflow
版权声明:本文标题:javascript - Change background image with a toggle (jQuery) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742202942a2432295.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论