温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

css3和jquery如何实现自定义checkbox和radiobox组件

发布时间:2021-06-25 09:20:58 阅读:179 作者:小新 栏目:web开发
前端开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章主要为大家展示了“css3和jquery如何实现自定义checkbox和radiobox组件”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“css3和jquery如何实现自定义checkbox和radiobox组件”这篇文章吧。

主要思路是利用隐藏原来的checkbox和radiobox,用一个div来模拟checkbox/radiobox,并使用jQuery来完成选择切换时的动画效果。

代码如下:

<ul>
        <li>
            <p>Gender:</p>
        </li>
        <li>
            <input type="radio" name="radio-btn" />Male</li>
        <li>
            <input type="radio" name="radio-btn" />Female</li>
    </ul>
    <ul>
        <li>
            <p>Favorite music:</p>
        </li>
        <li>
            <input type="checkbox" name="check-box" /> <span>Pop music</span></p>
<p>        </li>
        <li>
            <input type="checkbox" name="check-box" /> <span>Rock music</span></p>
<p>        </li>
        <li>
            <input type="checkbox" name="check-box" /> <span>Rap music</span></p>
<p>        </li>
        <li>
            <input type="checkbox" name="check-box" /> <span>Hiphop music</span></p>
<p>        </li>
    </ul>

然后我们用jQuery代码来为每一个checkbox和radiobox创建一个div,这个div的classname为check-box和radio-btn。

代码如下:

$('input[name="radio-btn"]').wrap('<div class="radio-btn"><i></i></div>');
$('input[name="check-box"]').wrap('<div class="check-box"><i></i></div>');

那么我们接下来要让原来的checkbox隐藏,同时设置模拟div的样式:

代码如下:

.radio-btn input[type="radio"].check-box input[type="checkbox"] {
    visibility: hidden;
}

代码如下:

.check-box {
    width22px;
    height22px;
    cursor: pointer;
    display: inline-block;
    margin2px 7px 0 0;
    position: relative;
    overflow: hidden;
    box-shadow0 0 1px #ccc;
    -webkit-border-radius3px;
    -moz-border-radius3px;
    border-radius3px;
    backgroundrgb(255255255);
    background-moz-linear-gradient(top, rgba(25525525510%rgba(246246246147%rgba(2372372371100%);
    background-webkit-gradient(linear, left top, left bottom, color-stop(0%rgba(2552552551)), color-stop(47%rgba(2462462461)), color-stop(100%rgba(2372372371)));
    background-webkit-linear-gradient(top, rgba(25525525510%rgba(246246246147%rgba(2372372371100%);
    background-o-linear-gradient(top, rgba(25525525510%rgba(246246246147%rgba(2372372371100%);
    background-ms-linear-gradient(top, rgba(25525525510%rgba(246246246147%rgba(2372372371100%);
    backgroundlinear-gradient(to bottom, rgba(25525525510%rgba(246246246147%rgba(2372372371100%);
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#ededed', GradientType=0);
    border1px solid #ccc;
}
.check-box i {
    backgroundurl('css/check_mark.png') no-repeat center center;
    position: absolute;
    left3px;
    bottom: -15px;
    width16px;
    height16px;
    opacity: .5;
    -webkit-transition: all 400ms ease-in-out;
    -moz-transition: all 400ms ease-in-out;
    -o-transition: all 400ms ease-in-out;
    transition: all 400ms ease-in-out;
    -webkit-transform:rotateZ(-180deg);
    -moz-transform:rotateZ(-180deg);
    -o-transform:rotateZ(-180deg);
    transform:rotateZ(-180deg);
}
.checkedBox {
    -moz-box-shadow: inset 0 0 5px 1px #ccc;
    -webkit-box-shadow: inset 0 0 5px 1px #ccc;
    box-shadow: inset 0 0 5px 1px #ccc;
    border-bottom-color#fff;
}
.checkedBox i {
    bottom2px;
    -webkit-transform:rotateZ(0deg);
    -moz-transform:rotateZ(0deg);
    -o-transform:rotateZ(0deg);
    transform:rotateZ(0deg);
}
/*Custom radio button*/
 .radio-btn {
    width20px;
    height20px;
    display: inline-block;
    float: left;
    margin3px 7px 0 0;
    cursor: pointer;
    position: relative;
    -webkit-border-radius100%;
    -moz-border-radius100%;
    border-radius100%;
    border1px solid #ccc;
    box-shadow0 0 1px #ccc;
    backgroundrgb(255255255);
    background-moz-linear-gradient(top, rgba(25525525510%rgba(246246246147%rgba(2372372371100%);
    background-webkit-gradient(linear, left top, left bottom, color-stop(0%rgba(2552552551)), color-stop(47%rgba(2462462461)), color-stop(100%rgba(2372372371)));
    background-webkit-linear-gradient(top, rgba(25525525510%rgba(246246246147%rgba(2372372371100%);
    background-o-linear-gradient(top, rgba(25525525510%rgba(246246246147%rgba(2372372371100%);
    background-ms-linear-gradient(top, rgba(25525525510%rgba(246246246147%rgba(2372372371100%);
    backgroundlinear-gradient(to bottom, rgba(25525525510%rgba(246246246147%rgba(2372372371100%);
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#ededed', GradientType=0);
}
.checkedRadio {
    -moz-box-shadow: inset 0 0 5px 1px #ccc;
    -webkit-box-shadow: inset 0 0 5px 1px #ccc;
    box-shadow: inset 0 0 5px 1px #ccc;
}
.radio-btn i {
    border1px solid #E1E2E4;
    width10px;
    height10px;
    position: absolute;
    left4px;
    top4px;
    -webkit-border-radius100%;
    -moz-border-radius100%;
    border-radius100%;
}
.checkedRadio i {
    background-color#898A8C;
}

上面这段CSS3代码就是用样式来自定义div,让div的样式和checkbox和radiobox一样。

最后我们来模拟点击选中和取消选中,这部分也是用jQuery来实现:

代码如下:

$(".radio-btn").on('click'function () {
    var _this = $(this),
        block = _this.parent().parent();
    block.find('input:radio').attr('checked'false);
    block.find(".radio-btn").removeClass('checkedRadio');
    _this.addClass('checkedRadio');
    _this.find('input:radio').attr('checked'true);
});

代码如下:

$.fn.toggleCheckbox = function () {
    this.attr('checked', !this.attr('checked'));
}
$('.check-box').on('click'function () {
    $(this).find(':checkbox').toggleCheckbox();
    $(this).toggleClass('checkedBox');
});

这段代码可以让选中和取消选中时产生一点小小的动画。

以上是“css3和jquery如何实现自定义checkbox和radiobox组件”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

原文链接:https://www.jb51.net/css/156959.html

AI

开发者交流群×