This article will share with you a piece of jquery code to achieve the effect of sliding button switch. The code is simple and easy to understand, very good, and has certain reference value. Friends who need it can refer to it
Everyone knows about sliding switch buttons. It can be seen on all major websites. Let me share with you an article about a sliding button switch effect based on jquery. Interested friends can refer to the implementation code.
Let me show you the renderings first:
Code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>jquery做的滑动按钮开关</title> <link rel="stylesheet" type="text/css" href="bootstrap/bootstrap.min.css"/> </head> <style> .switch{ width: 100px; margin: 100px 0px 0 100px; } .btn_fath{ margin-top: 10px; position: relative; border-radius: 20px; } .btn1{ float: left; } .btn2{ float: right; } .btnSwitch{ height: 40px; width: 50px; border:none; color: #fff; line-height: 40px; font-size: 16px; text-align: center; z-index: 1; } .move{ z-index: 100; width: 40px; border-radius: 20px; height: 40px; position: absolute; cursor: pointer; border: 1px solid #828282; background-color: #f1eff0; box-shadow: 1px 2px 2px 1px #fff inset,0 0 5px 1px #999; } .on .move{ left: 60px; } .on.btn_fath{ background-color: #5281cd; } .off.btn_fath{ background-color: #828282; } </style> <body> <p class="switch"> <p class="btn_fath clearfix on" onclick="toogle(this)"> <p class="move" data-state="on"></p> <p class="btnSwitch btn1">ON</p> <p class="btnSwitch btn2 ">OFF</p> </p> <p class="btn_fath clearfix off" onclick="toogle(this)"> <p class="move" data-state="off"></p> <p class="btnSwitch btn1">ON</p> <p class="btnSwitch btn2 ">OFF</p> </p> </p> <script type="text/javascript" src="jquery/jquery.min.js"></script> <script type="text/javascript" src="bootstrap/bootstrap.min.js"></script> <script type="text/javascript"> function toogle(th){ var ele = $(th).children(".move"); if(ele.attr("data-state") == "on"){ ele.animate({left: "0"}, 300, function(){ ele.attr("data-state", "off"); alert("关!"); }); $(th).removeClass("on").addClass("off"); }else if(ele.attr("data-state") == "off"){ ele.animate({left: '60px'}, 300, function(){ $(this).attr("data-state", "on"); alert("开!"); }); $(th).removeClass("off").addClass("on"); } } </script> </body> </html>
It should be noted that:
1. The speed used for sliding here is 300ms, which seems to be a constant speed, not linear speed; try to find a way to It cannot move linearly like ease in CSS3.
2. The callback function in the animate method is the function called after the movement ends.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
jQuery and CSS3 folding card drop-down list box to achieve the effect
jQuery to achieve the fixed top of the sliding page Displayed functions
The above is the detailed content of How to implement a sliding button switch with jQuery. For more information, please follow other related articles on the PHP Chinese website!