javascript - 使用 Switch/Toggle Css/javascript 更改文本颜色

标签 javascript jquery html css

感谢您阅读这个问题

  • 实际上,我正在尝试为学生和教师创建一个登录表单
  • 在教师和学生文本之间有一个切换/切换按钮
  • 当开关切换到相应的(教师/学生)时,我想更改文本的颜色(比如从绿色变为灰色)
  • 例如,如果我按下开关并且切换球向右移动意味着学生文本将为灰色并且另一侧相同

这是我的代码 index.html

.form-container{
            text-align: center;
            border-radius: 5px;
            background-color: #f6f6f6;
            padding: 30px 10px 30px 10px;
            margin: 0% 5% 0% 5%;
            font-family: 'Poppins', sans-serif;
            font-weight: 700;
            font-size: 30px;
            color: green;
            -webkit-box-shadow: 5px 5px 5px -1px rgba(0,0,0,0.41);
            -moz-box-shadow: 5px 5px 5px -1px rgba(0,0,0,0.41);
            box-shadow: 5px 5px 5px -1px rgba(0,0,0,0.41);
        }
        .input-design{
            margin: 0% 15% 0% 15%;
            margin-top: 50px;
        }
        .input-design input[type=text]{
            height: 50px;
            width: 100%;
            padding: 0px 0px 0px 10px;
            font-size: 20px;
            margin-bottom: 20px;
        }
        .input-design input[type=password]{
            height: 50px;
            width: 100%;
            padding: 0px 0px 0px 10px;
            font-size: 20px;
        }
        .button-design{
            padding: 15px 0px 0px 0px;
            height: 50px;
            font-weight: 600;
            font-size: 20px;
            margin: 5% 15% 5% 15%;
            background-color: green;
            color: white;
            border:1px solid white;
        }
        .button-design:hover{

            border:1px solid green;
            background-color: white;
            color: green;
        }

        .switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input { 
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
<body>
        <div class="form-container">
            <div class="role-selector">
                Teacher
                <label class="switch">
                        <input type="checkbox">
                        <span class="slider round"></span>
                      </label>
                Student
            </div>
            <div class="input-design">
                <input type="text" name="username" placeholder="Username">
                <input type="password" name="password" placeholder="Password">
            </div>
            <div class="button-design">
                Login
            </div>
        </div>
    </body>

最佳答案

如果您能够修改您的 HTML,那么这个纯 CSS 解决方案也是可行的:

/* CSS Additions */

/* Checkbox does not need to be displayed */
input {
  display: none;
}

/* Color labels green by default */
.role-selector input+span {
  color: green;
}
.role-selector input+*+*+span {
  color: grey;
}


/* Color labels when input is checked */
.role-selector input:checked+span {
  color: grey;
}
.role-selector input:checked+*+*+span {
  color: green;
}

/* --- */

.form-container {
  text-align: center;
  border-radius: 5px;
  background-color: #f6f6f6;
  padding: 30px 10px 30px 10px;
  margin: 0% 5% 0% 5%;
  font-family: 'Poppins', sans-serif;
  font-weight: 700;
  font-size: 30px;
  color: green;
  -webkit-box-shadow: 5px 5px 5px -1px rgba(0, 0, 0, 0.41);
  -moz-box-shadow: 5px 5px 5px -1px rgba(0, 0, 0, 0.41);
  box-shadow: 5px 5px 5px -1px rgba(0, 0, 0, 0.41);
}

.input-design {
  margin: 0% 15% 0% 15%;
  margin-top: 50px;
}

.input-design input[type=text] {
  height: 50px;
  width: 100%;
  padding: 0px 0px 0px 10px;
  font-size: 20px;
  margin-bottom: 20px;
}

.input-design input[type=password] {
  height: 50px;
  width: 100%;
  padding: 0px 0px 0px 10px;
  font-size: 20px;
}

.button-design {
  padding: 15px 0px 0px 0px;
  height: 50px;
  font-weight: 600;
  font-size: 20px;
  margin: 5% 15% 5% 15%;
  background-color: green;
  color: white;
  border: 1px solid white;
}

.button-design:hover {
  border: 1px solid green;
  background-color: white;
  color: green;
}

.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}



/*
Remove:
.switch input { 
  opacity: 0;
  width: 0;
  height: 0;
}
*/

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}



input:checked+span+.switch .slider {
  background-color: #2196F3;
}

input:focus+span+.switch .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked+span+.switch .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}


/* Rounded sliders */

.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
<body>
  <div class="form-container">
    <div class="role-selector">
      <!-- Move input outside of .switch label -->
      <input type="checkbox" checked id="role-checkbox">
      <span>Teacher</span>
      <!-- Add for attribute that matches id of check input -->
      <label class="switch" for="role-checkbox">
                  <span class="slider round"></span>
                </label>
      <span>Student</span>
    </div>
    <div class="input-design">
      <input type="text" name="username" placeholder="Username">
      <input type="password" name="password" placeholder="Password">
    </div>
    <div class="button-design">
      Login
    </div>
  </div>
</body>

关于javascript - 使用 Switch/Toggle Css/javascript 更改文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56746306/

相关文章:

javascript - 给每个元素一个唯一的ID

ios - 如何从AppGyver扫描仪清除类固醇应用程序

javascript - 隐藏最大高度溢出的 CSS 过渡 "simulate"过渡

javascript - 在 Bootstrap 中动态更改背景颜色

javascript - Ajax 调用以消耗本地主机上的剩余服务

jquery - jquery 中的一键多次点击处理程序

php - Web 应用程序 - 显示月份功能的 slider

html - 删除加载页面上的一些 <div>

javascript - 单击时更改图像和文本值

javascript - 在数组元素之间插入对象最优雅的方法是什么?