css布局之选择切换按钮

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<style type="text/css">
    body{background-color: #dedede;margin: 60% 35%;padding: 0;}
    .btn-change{
        width: 100px;
        height: 40px;
        border-radius: 20px;
        border: 1px solid #dedede;
        font-size: 1.3em;
        right: 4%;
        position: relative;
        overflow: hidden;
    }
    .btn-dot{
        height: 40px;
        width: 40px;
        background-color: #dedede;
        border-radius: 20px;
        position: absolute;
        z-index: 99;
    }

    .btn-left{
        background-color:#74B700; 
        position: absolute;
        width: 90px;
        height: 40px;
        line-height: 40px;
        left: -70px;
        color: #fff;
        text-align: left;
        padding-left: 30px;
    }
    .btn-move{position: absolute;}
    .btn-right{
        background-color: #ebebeb;
        position: absolute;
        width: 80px;
        height: 40px;
        line-height: 40px;
        text-align: left;
        padding-left: 20px;
        left: 20px;
        color: #333;
    }
</style>
<link rel="stylesheet" href="usercss/changebtn.css">
<script src="jquery-1.10.2.js"></script>
<body>
<div class="btn-change" data-switch="false">
     <div class="btn-move">
         <div class="btn-left">关注</div>
          <div class="btn-dot"></div>
          <div class="btn-right">未关注</div>
      </div>
</div>
    
</body>
</html>
<script>
    $(function(){
        $(".btn-change").click(function(){
            var switch1=$(this);
            var flag=switch1.attr("data-switch");
            if(flag=="false")
            {
                switch1.find(".btn-move").animate({ "left": "60px" }, 500);
                switch1.attr("data-switch","true");
            }else{
                switch1.find(".btn-move").animate({ "left": "0" }, 500);
                switch1.attr("data-switch","false");
            }
        })
    })
</script>

效果图:

技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。