gpt4 book ai didi

javascript - 如何在点击时启动动画

转载 作者:行者123 更新时间:2023-11-30 12:16:34 28 4
gpt4 key购买 nike

我的页面上有一个按钮,标签为“深色主题”。我想让这个按钮改变背景的效果。当你点击它时,我希望它有彩虹效果,当你再次点击它时,我希望它回到黑暗主题。我附上了相关代码:

body {
margin: 0px;
padding: 0px;
overflow-y: scroll;
font-family: Helvetica;
font-size: 18px;
background-color: #bdbdbd;
background-color: #45CEEF;
-webkit-animation: pulse 1s infinite alternate;
}

h3 {
font-weight: bold;
text-decoration: underline;
}
h2 {
font-weight: bold;
text-decoration: underline;
}

/*Light Switch*/

label {
display: block;
height: 25px;
width: 100px;
background: white;
text-align: center;
font: 14px/25px Helvetica, Arial, sans-serif;
margin: 20px 0;
position: absolute;
color: black;
transition: background 0.2s ease-out, color 0.2s ease-out;
}
label:hover {
background-color: #099;
color: white;
cursor: pointer;
transition: background-color 0.1s ease-in, color 0.1s ease-in;
}
input#lightswitch {
position: fixed;
top: -9999px;
left: -9999px;
}
input#lightswitch + .content {
color: black;
transition: background-color 0.5s ease-out, color 0.5s ease-out;
}
/*Switched Off*/

input#lightswitch:checked + .content {
background-color: #222;
color: #D3D3D3;
transition: background-color 0.5s ease-in, color 0.5s ease-in;
}
@-webkit-keyframes pulse {
0% {
background-color: #45CEEF;
}
25% {
background-color: #FFF5A5;
}
50% {
background-color: #FFD4DA;
}
75% {
background-color: #99D2E4;
}
100% {
background-color: #D8CAB4;
}
}
<link rel="stylesheet" type="text/css" href="bda.css">

<body class="body">
<label for="lightswitch">Dark Theme</label>
<input type="checkbox" id="lightswitch" />
</body>

我的代码:https://jsfiddle.net/em07jce9/1/

最佳答案

单击按钮时,使用 Javascript 将 CSS 类添加到要设置动画的元素。在您的样式表中,将动画附加到该特定类。

CSS:

.body.animated {
-webkit-animation: pulse 1s infinite alternate;
}

JS:

$(function() {
$('button').on('click', function() {
$('.body').addClass('animated');
});
});

如果您使用复选框作为切换,请使用 change 事件:

$(function() {
$('input#lightswitch').on('change', function() {
$('.body').toggleClass('animated');
});
});

关于javascript - 如何在点击时启动动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32291738/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com