简体   繁体   中英

How to hide div a when toggled

I have used checkbox as toggle menu with the help of css. But now i have to hide a div when toggled. How can it be done.

 /*------- Toggle switch --------*/ .switch { position: relative; display: block; vertical-align: top; width: 70px; height: 30px; padding: 3px; margin: 0 10px 10px 0; background: linear-gradient(to bottom, #eeeeee, #FFFFFF 25px); background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF 25px); border-radius: 18px; box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05); cursor: pointer; box-sizing: content-box; } .switch-input { position: absolute; top: 0; left: 0; opacity: 0; box-sizing: content-box; } .switch-label { position: relative; display: block; height: inherit; font-size: 10px; text-transform: uppercase; background: #bfbfbf; border-radius: inherit; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15); box-sizing: content-box; } .switch-label:before, .switch-label:after { position: absolute; top: 50%; margin-top: -.5em; line-height: 1; -webkit-transition: inherit; -moz-transition: inherit; -o-transition: inherit; transition: inherit; box-sizing: content-box; } .switch-label:before { content: attr(data-off); right: 11px; color: #000; text-shadow: 0 1px rgba(255, 255, 255, 0.5); } .switch-label:after { content: attr(data-on); left: 11px; color: #FFFFFF; text-shadow: 0 1px rgba(0, 0, 0, 0.2); opacity: 0; } .switch-input:checked~.switch-label { background: #20c000; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2); } .switch-input:checked~.switch-label:before { opacity: 0; } .switch-input:checked~.switch-label:after { opacity: 1; } .switch-handle { position: absolute; top: 4px; left: 4px; width: 28px; height: 28px; background: linear-gradient(to bottom, #FFFFFF 40%, #f0f0f0); background-image: -webkit-linear-gradient(top, #FFFFFF 40%, #f0f0f0); border-radius: 100%; box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2); } .switch-handle:before { content: ""; position: absolute; top: 50%; left: 50%; margin: -6px 0 0 -6px; width: 12px; height: 12px; background: linear-gradient(to bottom, #eeeeee, #FFFFFF); background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF); border-radius: 6px; box-shadow: inset 0 1px rgba(0, 0, 0, 0.02); } .switch-input:checked~.switch-handle { left: 44px; box-shadow: -1px 1px 5px rgba(0, 0, 0, 0.2); } /* Transition */ .switch-label, .switch-handle { transition: All 0.3s ease; -webkit-transition: All 0.3s ease; -moz-transition: All 0.3s ease; -o-transition: All 0.3s ease; } 
 <div class="togg-switch-btn"> <label class="switch"> <input class="switch-input slide-toggle" type="checkbox" /> <span class="switch-label" data-on="Yes" data-off="No"></span> <span class="switch-handle"></span> </label> </div> <!-- Hiding Div --> <div class="col-md-8 col-sm-12"> <p align="end">Completed Date : <span style="color:#c54d2d;">27-05-2017</span></p> <p align="end" style="color:#c54d2d;">John Jose</p> </div> 

Like this you want

 $("#cbox").change(function() { $("#myDiv").toggle(); }); 
 /*------- Toggle switch --------*/ .switch { position: relative; display: block; vertical-align: top; width: 70px; height: 30px; padding: 3px; margin: 0 10px 10px 0; background: linear-gradient(to bottom, #eeeeee, #FFFFFF 25px); background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF 25px); border-radius: 18px; box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05); cursor: pointer; box-sizing: content-box; } .switch-input { position: absolute; top: 0; left: 0; opacity: 0; box-sizing: content-box; } .switch-label { position: relative; display: block; height: inherit; font-size: 10px; text-transform: uppercase; background: #bfbfbf; border-radius: inherit; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15); box-sizing: content-box; } .switch-label:before, .switch-label:after { position: absolute; top: 50%; margin-top: -.5em; line-height: 1; -webkit-transition: inherit; -moz-transition: inherit; -o-transition: inherit; transition: inherit; box-sizing: content-box; } .switch-label:before { content: attr(data-off); right: 11px; color: #000; text-shadow: 0 1px rgba(255, 255, 255, 0.5); } .switch-label:after { content: attr(data-on); left: 11px; color: #FFFFFF; text-shadow: 0 1px rgba(0, 0, 0, 0.2); opacity: 0; } .switch-input:checked~.switch-label { background: #20c000; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2); } .switch-input:checked~.switch-label:before { opacity: 0; } .switch-input:checked~.switch-label:after { opacity: 1; } .switch-handle { position: absolute; top: 4px; left: 4px; width: 28px; height: 28px; background: linear-gradient(to bottom, #FFFFFF 40%, #f0f0f0); background-image: -webkit-linear-gradient(top, #FFFFFF 40%, #f0f0f0); border-radius: 100%; box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2); } .switch-handle:before { content: ""; position: absolute; top: 50%; left: 50%; margin: -6px 0 0 -6px; width: 12px; height: 12px; background: linear-gradient(to bottom, #eeeeee, #FFFFFF); background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF); border-radius: 6px; box-shadow: inset 0 1px rgba(0, 0, 0, 0.02); } .switch-input:checked~.switch-handle { left: 44px; box-shadow: -1px 1px 5px rgba(0, 0, 0, 0.2); } /* Transition */ .switch-label, .switch-handle { transition: All 0.3s ease; -webkit-transition: All 0.3s ease; -moz-transition: All 0.3s ease; -o-transition: All 0.3s ease; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="togg-switch-btn"> <label class="switch"> <input class="switch-input slide-toggle" type="checkbox" id="cbox" /> <span class="switch-label" data-on="Yes" data-off="No"></span> <span class="switch-handle"></span> </label> </div> <!-- Hiding Div --> <div class="col-md-8 col-sm-12" id="myDiv"> <p align="end">Completed Date : <span style="color:#c54d2d;">27-05-2017</span></p> <p align="end" style="color:#c54d2d;">John Jose</p> </div> 

Here you go with solution https://jsfiddle.net/7sx5tavx/

 $('.switch-input').change(function() { $('.col-md-8').toggle(); }); 
 /*------- Toggle switch --------*/ .switch { position: relative; display: block; vertical-align: top; width: 70px; height: 30px; padding: 3px; margin: 0 10px 10px 0; background: linear-gradient(to bottom, #eeeeee, #FFFFFF 25px); background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF 25px); border-radius: 18px; box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05); cursor: pointer; box-sizing: content-box; } .switch-input { position: absolute; top: 0; left: 0; opacity: 0; box-sizing: content-box; } .switch-label { position: relative; display: block; height: inherit; font-size: 10px; text-transform: uppercase; background: #bfbfbf; border-radius: inherit; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15); box-sizing: content-box; } .switch-label:before, .switch-label:after { position: absolute; top: 50%; margin-top: -.5em; line-height: 1; -webkit-transition: inherit; -moz-transition: inherit; -o-transition: inherit; transition: inherit; box-sizing: content-box; } .switch-label:before { content: attr(data-off); right: 11px; color: #000; text-shadow: 0 1px rgba(255, 255, 255, 0.5); } .switch-label:after { content: attr(data-on); left: 11px; color: #FFFFFF; text-shadow: 0 1px rgba(0, 0, 0, 0.2); opacity: 0; } .switch-input:checked~.switch-label { background: #20c000; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2); } .switch-input:checked~.switch-label:before { opacity: 0; } .switch-input:checked~.switch-label:after { opacity: 1; } .switch-handle { position: absolute; top: 4px; left: 4px; width: 28px; height: 28px; background: linear-gradient(to bottom, #FFFFFF 40%, #f0f0f0); background-image: -webkit-linear-gradient(top, #FFFFFF 40%, #f0f0f0); border-radius: 100%; box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2); } .switch-handle:before { content: ""; position: absolute; top: 50%; left: 50%; margin: -6px 0 0 -6px; width: 12px; height: 12px; background: linear-gradient(to bottom, #eeeeee, #FFFFFF); background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF); border-radius: 6px; box-shadow: inset 0 1px rgba(0, 0, 0, 0.02); } .switch-input:checked~.switch-handle { left: 44px; box-shadow: -1px 1px 5px rgba(0, 0, 0, 0.2); } /* Transition */ .switch-label, .switch-handle { transition: All 0.3s ease; -webkit-transition: All 0.3s ease; -moz-transition: All 0.3s ease; -o-transition: All 0.3s ease; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="togg-switch-btn"> <label class="switch"> <input class="switch-input slide-toggle" type="checkbox" /> <span class="switch-label" data-on="Yes" data-off="No"></span> <span class="switch-handle"></span> </label> </div> <!-- Hiding Div --> <div class="col-md-8 col-sm-12"> <p align="end">Completed Date : <span style="color:#c54d2d;">27-05-2017</span></p> <p align="end" style="color:#c54d2d;">John Jose</p> </div> 

I guess this is what you are looking for.

 let x = document.querySelector('.x'); let checkbox = document.querySelector('.switch-input'); checkbox.onclick = () => { x.classList.toggle('hide'); } 
 /*------- Toggle switch --------*/ .switch { position: relative; display: block; vertical-align: top; width: 70px; height: 30px; padding: 3px; margin: 0 10px 10px 0; background: linear-gradient(to bottom, #eeeeee, #FFFFFF 25px); background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF 25px); border-radius: 18px; box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05); cursor: pointer; box-sizing: content-box; } .switch-input { position: absolute; top: 0; left: 0; opacity: 0; box-sizing: content-box; } .switch-label { position: relative; display: block; height: inherit; font-size: 10px; text-transform: uppercase; background: #bfbfbf; border-radius: inherit; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15); box-sizing: content-box; } .switch-label:before, .switch-label:after { position: absolute; top: 50%; margin-top: -.5em; line-height: 1; -webkit-transition: inherit; -moz-transition: inherit; -o-transition: inherit; transition: inherit; box-sizing: content-box; } .switch-label:before { content: attr(data-off); right: 11px; color: #000; text-shadow: 0 1px rgba(255, 255, 255, 0.5); } .switch-label:after { content: attr(data-on); left: 11px; color: #FFFFFF; text-shadow: 0 1px rgba(0, 0, 0, 0.2); opacity: 0; } .switch-input:checked~.switch-label { background: #20c000; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2); } .switch-input:checked~.switch-label:before { opacity: 0; } .switch-input:checked~.switch-label:after { opacity: 1; } .switch-handle { position: absolute; top: 4px; left: 4px; width: 28px; height: 28px; background: linear-gradient(to bottom, #FFFFFF 40%, #f0f0f0); background-image: -webkit-linear-gradient(top, #FFFFFF 40%, #f0f0f0); border-radius: 100%; box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2); } .switch-handle:before { content: ""; position: absolute; top: 50%; left: 50%; margin: -6px 0 0 -6px; width: 12px; height: 12px; background: linear-gradient(to bottom, #eeeeee, #FFFFFF); background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF); border-radius: 6px; box-shadow: inset 0 1px rgba(0, 0, 0, 0.02); } .switch-input:checked~.switch-handle { left: 44px; box-shadow: -1px 1px 5px rgba(0, 0, 0, 0.2); } /* Transition */ .switch-label, .switch-handle { transition: All 0.3s ease; -webkit-transition: All 0.3s ease; -moz-transition: All 0.3s ease; -o-transition: All 0.3s ease; } .hide { display: none; } 
 <div class="togg-switch-btn"> <label class="switch"> <input class="switch-input slide-toggle" type="checkbox" /> <span class="switch-label" data-on="Yes" data-off="No"></span> <span class="switch-handle"></span> </label> </div> <!-- Hiding Div --> <div class="col-md-8 col-sm-12 x"> <p align="end">Completed Date : <span style="color:#c54d2d;">27-05-2017</span></p> <p align="end" style="color:#c54d2d;">John Jose</p> </div> 


another solution

 let x = document.querySelector('.x'); let checkbox = document.querySelector('.switch-input'); checkbox.onclick = () => { if (checkbox.checked) { x.style.display = 'none'; } if (!checkbox.checked) { x.style.display = 'block'; } } 
 /*------- Toggle switch --------*/ .switch { position: relative; display: block; vertical-align: top; width: 70px; height: 30px; padding: 3px; margin: 0 10px 10px 0; background: linear-gradient(to bottom, #eeeeee, #FFFFFF 25px); background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF 25px); border-radius: 18px; box-shadow: inset 0 -1px white, inset 0 1px 1px rgba(0, 0, 0, 0.05); cursor: pointer; box-sizing: content-box; } .switch-input { position: absolute; top: 0; left: 0; opacity: 0; box-sizing: content-box; } .switch-label { position: relative; display: block; height: inherit; font-size: 10px; text-transform: uppercase; background: #bfbfbf; border-radius: inherit; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.12), inset 0 0 2px rgba(0, 0, 0, 0.15); box-sizing: content-box; } .switch-label:before, .switch-label:after { position: absolute; top: 50%; margin-top: -.5em; line-height: 1; -webkit-transition: inherit; -moz-transition: inherit; -o-transition: inherit; transition: inherit; box-sizing: content-box; } .switch-label:before { content: attr(data-off); right: 11px; color: #000; text-shadow: 0 1px rgba(255, 255, 255, 0.5); } .switch-label:after { content: attr(data-on); left: 11px; color: #FFFFFF; text-shadow: 0 1px rgba(0, 0, 0, 0.2); opacity: 0; } .switch-input:checked~.switch-label { background: #20c000; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15), inset 0 0 3px rgba(0, 0, 0, 0.2); } .switch-input:checked~.switch-label:before { opacity: 0; } .switch-input:checked~.switch-label:after { opacity: 1; } .switch-handle { position: absolute; top: 4px; left: 4px; width: 28px; height: 28px; background: linear-gradient(to bottom, #FFFFFF 40%, #f0f0f0); background-image: -webkit-linear-gradient(top, #FFFFFF 40%, #f0f0f0); border-radius: 100%; box-shadow: 1px 1px 5px rgba(0, 0, 0, 0.2); } .switch-handle:before { content: ""; position: absolute; top: 50%; left: 50%; margin: -6px 0 0 -6px; width: 12px; height: 12px; background: linear-gradient(to bottom, #eeeeee, #FFFFFF); background-image: -webkit-linear-gradient(top, #eeeeee, #FFFFFF); border-radius: 6px; box-shadow: inset 0 1px rgba(0, 0, 0, 0.02); } .switch-input:checked~.switch-handle { left: 44px; box-shadow: -1px 1px 5px rgba(0, 0, 0, 0.2); } /* Transition */ .switch-label, .switch-handle { transition: All 0.3s ease; -webkit-transition: All 0.3s ease; -moz-transition: All 0.3s ease; -o-transition: All 0.3s ease; } 
 <div class="togg-switch-btn"> <label class="switch"> <input class="switch-input slide-toggle" type="checkbox" /> <span class="switch-label" data-on="Yes" data-off="No"></span> <span class="switch-handle"></span> </label> </div> <!-- Hiding Div --> <div class="col-md-8 col-sm-12 x"> <p align="end">Completed Date : <span style="color:#c54d2d;">27-05-2017</span></p> <p align="end" style="color:#c54d2d;">John Jose</p> </div> 

added a class name x to the element to hide

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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