简体   繁体   English

单击按钮时更改背景颜色

[英]changing background color when clicking button

I am a new to coding and I have a little thing I'm working on. 我是编码的新手,我正在做一些小事情。 I am trying to get the background of the page to change when I click on this button. 单击此按钮时,我试图更改页面的背景。 As I am new to coding, I don't know the right method to use for this. 由于我是编码的新手,因此我不知道用于此目的的正确方法。 Any suggestions would help. 任何建议都会有所帮助。 Here's what I have in html & css so far. 到目前为止,这是我在html&css中拥有的内容。

<div class="wrapper>
<p>Click on the button to see the magic happen!</p>
<input class="button" type="button" value="Click Me">

Then in CSS I have the following: 然后在CSS中,我有以下内容:

.wrapper{
text-align: center;
}
.button{
display: inline-block;
padding: 15px 25px;
font-size: 24px;
cursor: pointer;
text-align: center;
text-decoration: none;
outline: none;
color: #fff;
background-color: #EE835A;
border: none;
border-radius: 15px;
box-shadow: 0 9px #999;
}
.button:hover {background-color: #EE8354A}

.button:active {
background-color: #4A6273;
box-shadow: 0 5px #F68B8B;
transform: translateY(4px);
}

Took the time to do it, you have a few things wrong, take a look and see. 花时间去做,您有一些错误,请看一看。 This is also a pure CSS solution. 这也是一个纯CSS解决方案。

You have one too many characters in your hover color. 悬停颜色中的字符过多。 There should be 6 not 7. I replaced it with black #000000, as I was uncertain what color you want. 应该有6个而不是7个。我不确定黑色是#000000,因为我不确定您想要什么颜色。

Also, adding a subtle transition is a nice look. 此外,添加微妙的过渡效果也不错。

transition: .3s

 .button:hover {background-color: #000000; transition: .3s} body {background-color: white;} .button:active { background-color: #4A6273; box-shadow: 0 5px #F68B8B; transform: translateY(4px); } .wrapper{ text-align: center; } .button{ display: inline-block; padding: 15px 25px; font-size: 24px; cursor: pointer; text-align: center; text-decoration: none; outline: none; color: #fff; background-color: #EE835A; border: none; border-radius: 15px; box-shadow: 0 9px #999; transition: .3s; } 
 <div class="wrapper"> <p>Click on the button to see the magic happen!</p> <button class="button" onclick="document.body.style.backgroundColor = 'green';"></button> 

A simple JQuery script will accomplish this: 一个简单的JQuery脚本将完成此任务:

$('.button').click(function(e){
    e.preventDefault();
    $('body').css({'background':'red'});
});

https://jsfiddle.net/f1rkyxs7/2/ https://jsfiddle.net/f1rkyxs7/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM