gpt4 book ai didi

javascript - 使用 onclick 将 'x' 颜色添加到按钮边框

转载 作者:太空宇宙 更新时间:2023-11-04 07:22:35 24 4
gpt4 key购买 nike

我实际上尝试在 'x' 次内为我的边框按钮的一部分添加颜色。到目前为止,我设法通过我的第一个 click 函数获得了初始边框。现在我只想修改边框的一半、1/3、1/4...的颜色。

我正在寻找的原则是:每次,我都会点击按钮,它会向按钮添加一个“lvl”。为了表明这一点,请稍微修改一下边框。最后一个“lvl”将填充边界。

我有两个问题。 1、除了使用 border-top-colorborder-left-colorborder-right-colorborder- bottom-color,我找不到拆分颜色填充的解决方案,它限制我只能使用四分之一。

我也可以使用 :after:before 来更改它们,但似乎我在这些方面苦苦挣扎。

第二,我在第一个函数中复制的第二个 js 函数似乎不起作用。我认为这是因为 var 声明。我知道 skill_unlocked 类不是直接写在我的 HTML 代码中,而是在后面添加。它会阻止功能启动吗?

编辑:我尝试修改 js :

if document.getElementsByClassName("btn").hasClass("skill_unlocked") {
document.getElementsByClassName("skill_unlocked").addEventListener("click", function() {
this.classList.add("skill_lvl_1");
});

好像取消了第一个功能

var buttons = document.getElementsByClassName("btn");

for (var i = 0; i < buttons.length; i++) {
document.getElementsByClassName("btn")[i].addEventListener("click", function() {
this.classList.add("skill_unlocked");
});
}

/* WORKS TOO (SIMPLIFIED METHOD?)
$(document).on('click', '.btn', function() {
$(this).addClass('skill_unlocked');
}); */

var skill_unlocked = document.getElementsByClassName("skill_unlocked");

for (var i = 0; i < skill_unlocked.length; i++) {
document.getElementsByClassName("skill_unlocked")[i].addEventListener("click", function() {
this.classList.add("skill_lvl_1");
});
}
body {
background-color: #575757;
}

.btn {
margin: 10px;
display: block;
opacity: 0.6;
border: hidden;
background-color: grey;
border-radius: 50%;
cursor: pointer;
height: 70px;
width: 70px;
outline: none;
}

.btn:hover {
opacity: 0.9;
background-color: #2B2B2B;
}

.fas {
font-size: 28px;
}

.skill_unlocked {
border: 2px solid white;
opacity: 1;
background-color: #2B2B2B;
}

.skill_lvl_1 {
border: 2px solid;
opacity: 1;
background-color: #2B2B2B;
border-top-color:green;
}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="button border.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.10/css/all.css" integrity="sha384-+d0P83n9kaQMCwj8F4RJB66tzIwOKmrdb46+porD/OvrJ+37WqIM7UoBtwHO6Nlg" crossorigin="anonymous">
</head>

<body>

<button class="btn"><i class=" fas fa-shipping-fast fa-inverse"></i></button>
<button class="btn"><i class=" fas fa-address-card fa-inverse"></i></button>
<button class="btn"><i class=" fas fa-camera-retro fa-inverse"></i></button>
<button class="btn"><i class="fas fa-flag fa-inverse"></i></button>
<button class="btn"></button>

<script type="text/javascript" src="button border.js"></script>

</body>
</html>

最佳答案

我假设您想为白色边框后的每次点击着色右上-下-左边框(按此顺序),如果正确则您可以检查此代码

body {
background-color: #575757;
}

.btn {
margin: 10px;
display: block;
opacity: 0.6;
border: hidden;
background-color: grey;
border-radius: 50%;
cursor: pointer;
height: 70px;
width: 70px;
outline: none;
}

.btn:hover {
opacity: 0.9;
background-color: #2B2B2B;
}

.fas {
font-size: 28px;
}

.skill_unlocked {
border: 2px solid white;
opacity: 1;
background-color: #2B2B2B;
}

.skill_lvl_1 {
border: 2px solid;
opacity: 1;
background-color: #2B2B2B;
border-top-color:green;
}

.skill_lvl_2 {
border: 2px solid;
opacity: 1;
background-color: #2B2B2B;
border-top-color:green;
border-right-color:green;
}

.skill_lvl_3 {
border: 2px solid;
opacity: 1;
background-color: #2B2B2B;
border-top-color:green;
border-right-color:green;
border-bottom-color:green;
}
.skill_lvl_4 {
border: 2px solid;
opacity: 1;
background-color: #2B2B2B;
border-top-color:green;
border-right-color:green;
border-bottom-color:green;
border-left-color:green;
}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="border.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.10/css/all.css" integrity="sha384-+d0P83n9kaQMCwj8F4RJB66tzIwOKmrdb46+porD/OvrJ+37WqIM7UoBtwHO6Nlg" crossorigin="anonymous">
</head>

<body>

<button class="btn"><i class=" fas fa-shipping-fast fa-inverse"></i></button>
<button class="btn"><i class=" fas fa-address-card fa-inverse"></i></button>
<button class="btn"><i class=" fas fa-camera-retro fa-inverse"></i></button>
<button class="btn"><i class="fas fa-flag fa-inverse"></i></button>
<button class="btn"></button>

<script type="text/javascript">
var buttons = document.getElementsByClassName("btn");

for (var i = 0; i < buttons.length; i++) {
document.getElementsByClassName("btn")[i].addEventListener("click", function() {
if(this.classList.contains("skill_lvl_3"))
this.classList.add("skill_lvl_4");
else if(this.classList.contains("skill_lvl_2"))
this.classList.add("skill_lvl_3");
else if(this.classList.contains("skill_lvl_1"))
this.classList.add("skill_lvl_2");
else if(this.classList.contains("skill_unlocked"))
this.classList.add("skill_lvl_1");
else
this.classList.add("skill_unlocked");
});
}
</script>

</body>
</html>

我希望这是你想要做的

关于javascript - 使用 onclick 将 'x' 颜色添加到按钮边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50145404/

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