Jquery-drawer抽屉插件

基于bootstrap和jquery,自定义抽屉样式,pc端web程序常用左右侧抽屉样式,因此本示例不考虑上下侧的抽屉。

效果:

插件 :

$.fn.extend({
    drawer: function (options) {
        var _this = $(this);
        var defaults = {
            title: "标题",//默认标题
            placement: "right",//left表示左侧滑出,right表示右侧滑出
            width: "300px" // drawer的样式
        }
        options = $.extend(defaults, options);
        options.style = `width:${options.width}; ${options.placement}:-${options.width};`;

        function showDrawer() {
            let css = {};
            css[options.placement] = "0";
            $("#" + _this.data("drawerid")).show().animate(css, "fast");
        }

        if (_this.data("drawerid")) {
            showDrawer();
            return;
        }

        let _guid = `drawere${guid()}`;
        _this.data("drawerid", _guid);//标记已经初始化,防止重复初始


        let aside = $("<aside>", {
            id: _guid,
            style: options.style,
            class: "drawer",
        });

        let head = $("<div>", {
            class: "clearfix",
            style: "font-size:20px"
        });
        head.append(`<div class="pull-left drawer-head">${options.title}</div>`);

        let btnClose = $("<i>", {
            class: "fa fa-remove",
            click: function () {
                let css = {};
                css[options.placement] = `-${options.width}`;
                $(this).parents('aside').animate(css, 'fast', function () { $(this).hide() });
            }
        });

        head.append($(`<div class="pull-right text-red pointer">`).append(btnClose));
        aside.append(head);
        let content = $("<div>", {
            class: "drawer-content"
        });
        content.append(_this);
        aside.append(content).appendTo("body");

        showDrawer();

    }
})
}(jQuery));

css:

.drawer {
    box-shadow: 
        0 8px 10px -5px rgba(0,0,0,.3),
        0 16px 24px 2px rgba(0,0,0,.3),
        0 6px 30px 5px rgba(0,0,0,.3);
    padding:10px;
    height: 100%;
    overflow: auto;
    position: fixed;
    top: 0;
    background-color:#fffee5;
    z-index: 1;
    display:none;
}
    .drawer .fa-remove {
        width: 24px;
        height: 24px;
        border: 1px solid #fff1a2;
        text-align: center;
        cursor: pointer;
    }
    .drawer .fa-remove:hover {
        color: #fff;
        background-color: #ff0000;
    }

用法:

//html
<div id="test"  >内容</div>

//js
$("#test").drawer({ title: "标题", width: "30%", placement: "left" });

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值