效果展示:

代码展示:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>jquery实现开关按钮</title>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <style>
        section{
            width: 300px;
            height: 300px;
            margin:  auto;
            margin-top: 50px;
        }
        .Ellipse1 {
            width: 50px;
            height: 30px;
            border-radius: 16px;
            background-color: #ccc;
            transition: 0.3s;
            margin-bottom: 10px;
        }
        .circle1 {
            width: 29px;
            height: 29px;
            background-color: #fff;
            border-radius: 50%;
            box-shadow: 0px 1px 5px rgba(0, 0, 0, .5);
            transition: 0.3s;
            position: relative;
            left: 0px;
        }
        .circle1:hover {
            transform: scale(1.2);
            box-shadow: 0px 1px 8px rgba(0, 0, 0, .5);
        }
    </style>
</head>
<body>
<section>
    按钮效果
    <div class="Ellipse1">
        <div class="circle1"></div>
    </div>
</section>
 
<script>
    $(function () {
        $('.circle1').click(function () {
            var left = $(this).css('left');
            left = parseInt(left);
            if (left == 0) {
                $(this).css('left', '30px'),
                    $(this).css('background-color', '#FF0000'),
                    $('.Ellipse1').css('background-color', '#FF0000');
            }
            else {
                $(this).css('left', '0px'),
                    $(this).css('background-color', '#fff'),
                    $('.Ellipse1').css('background-color', '#ccc');
            }
        });
    });
 
</script>
 
</body>
</html>

与御风痕博客园,程序员博客园地的首选!
御风痕博客园 » jQuery实现开关按钮

发表评论