用作:

1. 从侧边栏滑入滑出需要展示的内容

介绍:

1. 插件遵循了UMD通用模块定义规范,能够在各种js环境下运行

2. 依赖jQuery

3. 插件可进行配置各项参数,具体如下注释部分

4. 插件暴露了open、close方法,用于打开和关闭抽屉

使用:

1. 在html中定义抽屉中需要展示的内容,并放置在最外层元素下

2. 页面加载完成之后,获取定义的元素,并调用drawer方法初始化插件,同时保存返回的drawer实例

3. 调用open方式展开抽屉

代码:

css(drawer.css)

1 .drawer {

2 height: 0%;

3 width: 100%;

4 position: fixed;

5 top: 0;

6 left: 0;

7 z-index: 1000;

8 }

9 .drawer.active {

10 height: 100%;

11 }

12 .drawer-mask {

13 position: absolute;

14 top: 0;

15 left: 0;

16 width: 100%;

17 height: 100%;

18 opacity: 0;

19 filter: alpha(opacity=45);

20 transition: opacity .3s linear;

21 }

22 .drawer-mask-show {

23 background-color: rgba(0,0,0,.45);

24 }

25 .drawer.active .drawer-mask {

26 opacity: 1;

27 }

28 .drawer-content {

29 height: 100%;

30 background-color: #fff;

31 box-shadow: none;

32 transition: transform .3s cubic-bezier(.7,.3,.1,1), box-shadow .3s cubic-bezier(.7,.3,.1,1);

33 position: absolute;

34 top: 0;

35 }

36 .drawer-content.left {

37 transform: translateX(-100%);

38 box-shadow: 2px 0 8px rgba(0,0,0,.15);

39 }

40 .drawer-content.right {

41 right: 0;

42 transform: translateX(100%);

43 box-shadow: -2px 0 8px rgba(0,0,0,.15);

44 }

45 .drawer.active .drawer-content{

46 transform: none;

47 }

48 .drawer-content-header {

49 height: 55px;

50 line-height: 55px;

51 border-bottom: 1px solid #e8e8e8;

52 }

53 .drawer-content-title {

54 color: rgba(0,0,0,.85);

55 font-weight: 600;

56 font-size: 16px;

57 margin: 0 20px;

58 }

59 .drawer-close {

60 position: absolute;

61 top: 0;

62 right: 20px;

63 font-size: 22px;

64 cursor: pointer;

65 }

66 .drawer-content-body {

67 padding: 24px;

68 max-height: calc(100% - 104px);

69 overflow: auto;

70 }

71 .drawer-content-body::-webkit-scrollbar {

72 width: 6px;

73 height: 6px;

74 background-color: hsla(0,0%,100%,0);

75 }

76 .drawer-content-body::-webkit-scrollbar-thumb {

77 border-radius: 3px;

78 background-color: #cfd6dd;

79 }

js(drawer.js)

1 /**

2 * 自定义drawer抽屉插件

3 * created by mengbing 2020/12/28

4 *

5 * 属性:

6 * placement: 抽屉方向,可选值'left'/'right',默认'left'

7 * width: drawer的宽度,类型String,默认250px

8 * closable: 是否显示右上角关闭按钮,类型Boolean,默认true

9 * mask: 是否展示遮罩,类型Boolean,默认true

10 * maskClosable: 点击遮罩是否允许关闭,类型Boolean,默认true

11 * title: 标题,类型String

12 * afterOpenCallback: 打开抽屉后回调方法,类型function

13 * afterCloseCallback: 关闭抽屉后回调方法,类型function

14 *

15 * 方法:

16 * open: 打开抽屉

17 * close: 关闭抽屉

18 */

19 ;(function (factory) {

20 if (typeof define === 'function' && define.amd) {

21 define(['jquery'], factory);

22 } else if (typeof exports === 'object') {

23 module.exports = factory(require('jquery'));

24 } else {

25 factory(jQuery);

26 }

27 }(function ($) {

28 var mDrawer = {

29 // 初始化drawer

30 init (options) {

31 var _this = $(this)

32 mDrawer.el = _this

33

34 var defaultOptions = {

35 placement: 'left',

36 width: '250px',

37 closable: true,

38 mask: true,

39 maskClosable: true

40 }

41 $.extend(true, defaultOptions, options || {})

42 mDrawer.options = defaultOptions

43

44 // 获取目标元素内容并移除

45 var contentBody = _this.addClass('drawer').children().remove()

46

47 // mask

48 var drawerMask = $('

', {

49 class: 'drawer-mask'

50 })

51 _this.append(drawerMask)

52 // 是否显示mask

53 if (defaultOptions.mask) {

54 drawerMask.addClass("drawer-mask-show")

55 }

56 // 点击mask是否允许关闭

57 if (defaultOptions.maskClosable) {

58 drawerMask.click(function() {

59 mDrawer.close()

60 })

61 }

62

63 // 构建drawer内容

64 var drawerContent = $('

', {

65 class: 'drawer-content ' + defaultOptions.placement,

66 style: 'width: ' + defaultOptions.width

67 })

68 _this.append(drawerContent)

69

70 if (defaultOptions.closable || defaultOptions.title) {

71 var drawerContentHeader = $('

', {

72 class: 'drawer-content-header'

73 })

74 drawerContent.append(drawerContentHeader)

75

76 if (defaultOptions.title) {

77 var drawerContentTitle = $('

', {

78 class: 'drawer-content-title',

79 text: defaultOptions.title

80 })

81 drawerContentHeader.append(drawerContentTitle)

82 }

83

84 if (defaultOptions.closable) {

85 var drawerClose = $('

', {

86 class: 'drawer-close',

87 text: '×'

88 })

89 drawerContentHeader.append(drawerClose)

90

91 drawerClose.click(function() {

92 mDrawer.close()

93 })

94 }

95 }

96

97 var drawerContentBody = $('

', {

98 class: 'drawer-content-body'

99 })

100 contentBody.appendTo(drawerContentBody)

101 drawerContent.append(drawerContentBody)

102

103 return mDrawer

104 },

105 // 打开抽屉

106 open () {

107 this.el.addClass("active")

108 this._runCallback(this.options.afterOpenCallback)

109 },

110 // 关闭抽屉

111 close () {

112 this.el.removeClass("active")

113 this._runCallback(this.options.afterCloseCallback)

114 },

115 // 调用回调函数

116 _runCallback (callback) {

117 if (!callback) return

118

119 if (typeof callback === 'function') {

120 try {

121 callback()

122 } catch(e) {

123 console.error('回调函数调用失败:', e)

124 }

125 } else {

126 console.error('callback is not a function')

127 }

128 }

129 }

130

131 $.fn.drawer = function (options) {

132 return mDrawer.init.call(this, options)

133 };

134 }))

html

1

2

3

4

5

抽屉

6

7

8

9 * {

10 padding: 0;

11 margin: 0;

12 }

13 html,body {

14 height: 100%;

15 overflow: hidden;

16 }

17

18

19

20

我是内容
我是内容
我是内容
我是内容
我是内容
我是内容

21

我是内容
我是内容
我是内容
我是内容
我是内容
我是内容

22

我是内容
我是内容
我是内容
我是内容
我是内容
我是内容

23

我是内容
我是内容
我是内容
我是内容
我是内容
我是内容

24

我是内容
我是内容
我是内容
我是内容
我是内容
我是内容

25

我是内容
我是内容
我是内容
我是内容
我是内容
我是内容

26

我是内容
我是内容
我是内容
我是内容
我是内容
我是内容

27

我是内容
我是内容
我是内容
我是内容
我是内容
我是内容

28

我是内容
我是内容
我是内容
我是内容
我是内容
我是内容

29

30 打开抽屉

31

32

33

34

35 var mDrawer

36 window.onload = function() {

37 mDrawer = $('#drawer').drawer({

38 placement: 'right',

39 width: '450px',

40 title: '标题标题',

41 afterOpenCallback: function () {

42 console.log("open了");

43 }

44 })

45 }

46

47 function opena() {

48 mDrawer.open()

49 }

50

51

标签:function,插件,defaultOptions,js,content,drawer,内容,var

来源: https://www.cnblogs.com/mengbing/p/14205348.html

html左侧抽屉,js抽屉drawer插件相关推荐

  1. js树形结构html源码,纯JS树形结构插件

    插件描述:纯JS树形结构插件,代码精简方便定制修改 更新时间:2020-05-20 22:30:39 更新说明:内含旧版.新版源码更新功能: 1.简化全部代码,从660行减少为430行 2.优化实现思 ...

  2. JS导出PDF插件(支持中文、图片使用路径)

    JS导出PDF插件(支持中文.图片使用路径) 原文:JS导出PDF插件(支持中文.图片使用路径) 在WEB上想做一个导出PDF的功能,发现jsPDF比较多人推荐,遗憾的是不支持中文,最后找到pdfma ...

  3. CheckCode.js 前端验证码插件

    CheckCode.js 前端验证码插件 效果截图 插件使用方法 CheckCode.js 本插件的参考示例 效果截图 插件使用方法 // 在html页面引入CheckCode.js <scri ...

  4. js Grid - 列表插件

     js Grid - 列表插件     1)Sponsor Flip Wall With jQuery & CSS一个非常不错的显示数据到网格里的插件. 点击后,缩略图会翻转,然后显示更多信息 ...

  5. sortable 拖拽时互换目标的位置_双端通用型JS拖拽插件的封装与应用

    最近工作中遇到一个需求,需要将一个元素从某位置拖动到另一固定位置后执行某一交互行为,具体效果如下: 这个看似简单的需求,然而实现起来却并不那么顺利.我首先想到的是如何通过哪个现有的插件来快速解决这个问 ...

  6. sublime text3安装js提示的插件

    今天安装Sublime Text3的js插件,在网上查了很多资料,为了方便以后看,写一个安装插件的总结和方法. 要安装js相关的插件,就要先安装一个Package Control(插件管理器)的插件, ...

  7. 直接拿来用!Vue.js 第三方常用插件盘点 | CSDN 博文精选

    作者 | 张猛 责编 | 伍杏玲 出品 | CSDN 博客 [CSDN 编者按]作者介绍几个Vue.js常用的插件和用法,简单实用,你可以直接拿来用! Vue.js DevTools 用于开发调试Vu ...

  8. Js与flash交互:在html页面中用js与MyReport插件交互

    <sdt id="89512093" sdtgroup="t" contentlocked="t" sdtlocked="t ...

  9. JS 之 图片编辑器插件

    今天跟大家分享下JS 之 图片编辑器插件的知识. 1 图片编辑器插件Filerobot Filerobot是一款js图片编辑器插件.Filerobot可以对图片进行修改尺寸,剪裁,旋转,以及使用内置的 ...

最新文章

  1. python re match groups_python re.match与re.search的区别
  2. 人群场景的属性--Deeply Learned Attributes for Crowded Scene Understandin
  3. C/C++语言中计算int,float,double,char四种数据类型所能表示的数据范围
  4. selenium 测试
  5. 白话Elasticsearch24- 深度探秘搜索技术之TFIDF算法/向量空间模型算法/lucene的相关度分数算法
  6. 前facebook产品技术leader徐玮:如何建立用户增长机制
  7. world scientific is the journal to follow
  8. 文件夹配置文件服务器,服务器文件夹配置文件
  9. MySQL 中 AUTO_INCREMENT 的“坑” --重复值问题
  10. python闭包(一分钟读懂)
  11. Python破解百度翻js代码
  12. Flask中制作博客首页的分类功能(一)
  13. 深度学习 --- 卷积神经网络CNN(LeNet-5网络详解)
  14. 关于12864显示器的SPI串行驱动问题的研究,AVR处理器(1)
  15. SWAT模型教程---土地利用、土壤数据、气象数据的处理
  16. 【柒】企业分析利器——强大企业模型
  17. MATLAB中取整函数一览表
  18. JAVA空间换时间以及时间换空间的例子
  19. matlab两矩阵乘除,MATLAB矩阵乘法
  20. delete不起作用 nsis_Delete键为什么不起作用了?

热门文章

  1. 火狗工房(注意哦,不是热狗)
  2. PB函数 Match()、MatchW()+PB字符串处理函数
  3. python写桌面软件如何实现自动升级
  4. ArcGIS基础:相同空间人口图层的人口数量字段转移至小区图层(核心:相交操作)及制作人口密度专题图
  5. MacBook Pro拓展坞失灵问题的解决建议
  6. 面对家里沉迷手机的学生,家长着急怎么办?
  7. 做云端数据有备无患的“杀手锏”
  8. 【计算机网络】如何避免Chrome/Firefox/Edge主页被搜狗/360/2345等流氓劫持
  9. uboot-uboot网络初始化分析
  10. Windows API Hooking with MS Detours