html左侧抽屉,js抽屉drawer插件

用作:

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue抽屉组件是一个常见的UI组件,用于实现侧边栏的展开和收起功能。在Vue中,可以通过使用slot来实现子组件的抽屉效果。 首先,在父组件中引入抽屉组件,并设置一个布尔类型的data属性来控制抽屉的展开和收起状态: ```vue <template> <div> <button @click="showDrawer = true">打开抽屉</button> <drawer v-model="showDrawer"> <!-- 抽屉内容 --> <div slot="content"> <!-- 这里是抽屉的内容 --> </div> </drawer> </div> </template> <script> import Drawer from '抽屉组件的路径' export default { components: { Drawer }, data() { return { showDrawer: false } } } </script> ``` 然后,在抽屉组件中定义好抽屉的样式和交互逻辑: ```vue <template> <transition name="drawer"> <div class="drawer" v-show="value"> <!-- 抽屉内容插槽 --> <slot name="content"></slot> </div> </transition> </template> <script> export default { props: { value: { type: Boolean, default: false } } } </script> <style scoped> .drawer { position: fixed; top: 0; left: 0; width: 300px; height: 100vh; background-color: #fff; transition: transform 0.3s ease-in-out; transform: translateX(-100%); } .drawer-enter-active, .drawer-leave-active { transition: transform 0.3s ease-in-out; } .drawer-enter, .drawer-leave-to { transform: translateX(0); } </style> ``` 通过以上代码,就可以实现一个简单的抽屉组件。当点击打开按钮时,抽屉会从左侧滑入屏幕,并展示出内容;再次点击关闭按钮时,抽屉会滑出屏幕。你可以根据实际需要,自定义抽屉的样式和交互效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值