jQuery 如何?一個滑動按鈕開關_jquery

來源:互聯網
上載者:User

滑動開關按鈕大家在各大網站都能見到,下面小編給大家分享一篇基於jquery實現的一個滑動按鈕開關效果,感興趣的朋友可以參考下實現代碼。

先給大家展示下效果圖:

代碼:

<!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> <div class="switch"> <div class="btn_fath clearfix on" onclick="toogle(this)"> <div class="move" data-state="on"></div> <div class="btnSwitch btn1">ON</div> <div class="btnSwitch btn2 ">OFF</div> </div> <div class="btn_fath clearfix off" onclick="toogle(this)"> <div class="move" data-state="off"></div> <div class="btnSwitch btn1">ON</div> <div class="btnSwitch btn2 ">OFF</div> </div> </div> <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>

需要注意的是:

1、這邊滑動使用的速度是300ms,好像是勻速,沒有線性快慢那種;試著找下能不能像CSS3中ease那種線性運動的。

2、animate方法中的回呼函數,即運動結束後調用的function。

以上所述是小編給大家介紹的jQuery =實現一個滑動按鈕開關的功能,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.