[英]How to transition between colours using jQuery click()
im looking to transition between colours when a user selects different radio button values. 我希望在用户选择不同的单选按钮值时在颜色之间切换。 I already have a 'working' example at the moment with just the immediate changing of the body's background colour, however i tried to use the fadeOut() and fadeTo() methods, but to no avail. 目前,我已经有了一个“有效”的示例,只是立即更改了主体的背景色,但是我尝试使用了fadeOut()和fadeTo()方法,但无济于事。
here's what i have thus far, if someone could point me to where i should be altering and which method to utilise. 如果有人可以指出我应该在哪里更改以及要使用哪种方法,这就是我到目前为止的情况。
many thanks in advance! 提前谢谢了!
colourChange.js colourChange.js
$(document).ready(function () {
$("#goodRad").click(function () {
$("body").css('background-color', '#90EE90');//LightGreen
console.log("make body green!");
});
$("#okRad").click(function () {
$("body").css('background-color', '#FFA07A');//LightSalmon
console.log("make body amber!");
});
$("#badRad").click(function () {
$("body").css('background-color', '#F08080');//LightCoral
console.log("make body red!");
});
});
You can use css to transition
background-color
. 您可以使用CSS transition
background-color
。 I also reworked your jquery to a little, but all you need to do is add the CSS from this answer. 我还对jquery进行了一些修改,但您所需要做的就是从此答案中添加CSS。
$('input').on('change',function() { $('body').css('background-color',$(this).attr('data-color')); })
body { transition: background-color .5s; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input name="color" data-color="#90EE90" type="radio"><input name="color" data-color="#FFA07A" type="radio"><input name="color" data-color="#F08080" type="radio">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.