首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >同一按钮上的开/关事件

同一按钮上的开/关事件
EN

Stack Overflow用户
提问于 2019-04-25 07:28:47
回答 1查看 41关注 0票数 0

我有一个与CSS一起工作的切换按钮,但当它打开和关闭时,我需要添加一些逻辑。问题是jQuery不能读取CSS上的:after和:before,因为它们实际上不是DOM的一部分。

-切换--

<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch">
    <label class="onoffswitch-label" for="myonoffswitch">
        <span id="switch-inner" class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>

--CSS--

.onoffswitch {
    position: relative; width: 78px;
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
    display: none;
}
.onoffswitch-label {
    display: block; overflow: hidden; cursor: pointer;
    border: 2px solid #C2C2C2; border-radius: 20px;
}
.onoffswitch-inner {
    display: block; width: 200%; margin-left: -100%;
    transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
    display: block; float: left; width: 50%; height: 28px; padding: 0; line-height: 28px;
    font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
    box-sizing: border-box;
}
.onoffswitch-inner:before {
    content: "ON";
    padding-left: 10px;
    background-color: #5C7FE3; color: #FFFFFF;
}
.onoffswitch-inner:after {
    content: "OFF";
    padding-right: 10px;
    background-color: #EEEEEE; color: #999999;
    text-align: right;
}
.onoffswitch-switch {
    display: block; width: 18px; margin: 5px;
    background: #FFFFFF;
    position: absolute; top: 0; bottom: 0;
    right: 46px;
    border: 2px solid #C2C2C2; border-radius: 20px;
    transition: all 0.3s ease-in 0s; 
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px; 
}

我尝试过onClick事件,但每次我单击它(逻辑上)它都会触发代码(如果多次单击,则会触发多次),最完美的情况是,一个事件将为真并触发代码,在第二次单击时,它将执行其他操作。

我不知道这是否会非常简单,我的大脑很疲惫,没有正确地思考,但任何帮助都会有帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-25 07:36:16

添加click事件处理程序是正确的方法。只需查看checkbox的checked属性,即可了解按钮的状态(开/关)。

document.getElementById("myonoffswitch").addEventListener("click", function(){
   // Test the state of the checkbox and act accordingly
   if(this.checked){
     console.log("ON");
   } else {
     console.log("OFF");
   }
});
.onoffswitch {
    position: relative; width: 78px;
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
}
.onoffswitch-checkbox {
    display: none;
}
.onoffswitch-label {
    display: block; overflow: hidden; cursor: pointer;
    border: 2px solid #C2C2C2; border-radius: 20px;
}
.onoffswitch-inner {
    display: block; width: 200%; margin-left: -100%;
    transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
    display: block; float: left; width: 50%; height: 28px; padding: 0; line-height: 28px;
    font-size: 14px; color: white; font-family: Trebuchet, Arial, sans-serif; font-weight: bold;
    box-sizing: border-box;
}
.onoffswitch-inner:before {
    content: "ON";
    padding-left: 10px;
    background-color: #5C7FE3; color: #FFFFFF;
}
.onoffswitch-inner:after {
    content: "OFF";
    padding-right: 10px;
    background-color: #EEEEEE; color: #999999;
    text-align: right;
}
.onoffswitch-switch {
    display: block; width: 18px; margin: 5px;
    background: #FFFFFF;
    position: absolute; top: 0; bottom: 0;
    right: 46px;
    border: 2px solid #C2C2C2; border-radius: 20px;
    transition: all 0.3s ease-in 0s; 
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px; 
}
<div class="onoffswitch">
    <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch">
    <label class="onoffswitch-label" for="myonoffswitch">
        <span id="switch-inner" class="onoffswitch-inner"></span>
        <span class="onoffswitch-switch"></span>
    </label>
</div>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55839866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档